- 论坛徽章:
- 0
|
大家好,我想生成一个数字的图片,图片生成了,但是稍微放大后就失真了,思索了一下,想了个办法,生成一个大的图片,然后压缩成小,但是还是不行
下面是代码,麻烦大家帮忙看看- # -*- coding: utf-8 -*-
- '''
- Created on Jun 6, 2016
- @author: ada-h
- '''
- from PIL import Image, ImageFont, ImageDraw
- from matplotlib.delaunay.testfuncs import quality
- class GenCommodityImg(object):
- '''
- classdocs
- '''
- def __init__(self, width, height, fontsize):
- '''
- Constructor
- '''
- self.width = width
- self.height = height
- self.fontsize = fontsize
- self.bgcolor = (255,255,255)
- self.font = ImageFont.truetype('arial.ttf', fontsize)
- self.fontcolor = (0,0,0)
- self.image = Image.new('RGB',(self.width, self.height), self.bgcolor)
- self.draw = ImageDraw.Draw(self.image)
-
- def setData(self, text):
- textWidth,textHeight = self.font.getsize(text)
- x =y = 0
- if textWidth < self.width:
- x = (self.width - textWidth) / 2
- if textHeight < self.height:
- y = (self.height - textHeight) / 2
- self.draw.text((x, y), text, fill = self.fontcolor, font = self.font)
-
- if __name__ == "__main__":
- genImg = GenCommodityImg(1920,768, 200)
- genImg.setData("123321321")
- # genImg.image.resize((100,20), Image.ANTIALIAS)
- genImg.image.save("e://321321.png", "PNG")
- img = Image.open("e://321321.png")
- w, h = img.size
- img.resize((w/2, h/2)).save("e:/1111.png","PNG")
- print img.size
- print genImg.image.format
复制代码 |
|