效果图
图文海报类
class myCard:
def __init__(self):
self.img_url='img/moban.jpg' #800*1280
self.icon_url = 'img/icons.png' #200*400
#纵向间距
self.magrinImgTop=150
#横向间距
self.magrinImgLeft=30
self.background_height=0
#加载底图
def loadMoban(self):
self.img = Image.open(self.img_url)
self.width,self.height=self.img.size
#加载图片
def loadBackground(self,background_url):
self.background=Image.open(background_url)
#加载小图
def loadIcon(self):
self.icon=Image.open(self.icon_url)
#高斯模糊图片作为背景
def drawBlur(self):
backflur = self.background.resize((self.width,self.height), resample=3).filter(MyGaussianBlur(radius=30))
self.img.paste(backflur,(0, 0))
#添加标题
def drawTitle(self):
draw = ImageDraw.Draw(self.img)
#文字行距
magrinTop=20
size=150
title_font = ImageFont.truetype('fzqk.TTF', size)
x=self.width/4-size+self.magrinImgLeft
y=self.magrinImgTop
draw.text((x,y), '早', font=title_font, fill='#ffffff')
size=150
title_font = ImageFont.truetype('fzqk.TTF', size)
x=x
y=y+size+magrinTop
draw.text((x,y), '安', font=title_font, fill='#ffffff')
#添加图片
def drawBackground(self):
x=0
y=self.magrinImgTop*2
srcwidth,srcheight=self.background.size
height=int(srcheight*self.width/srcwidth)
#重设图片尺寸
background = self.background.resize((self.width, height),Image.ANTIALIAS)
#创建圆形遮罩
alpha_layer = Image.new('L', (self.width, height), 0)
draw = ImageDraw.Draw(alpha_layer)
draw.ellipse((self.width/2-100,0, self.width-30, height), fill=255)
self.img.paste(background,(x,y),alpha_layer)
self.background_height=y+height
#添加文字
def drawText(self,tmp,weather,week):
draw = ImageDraw.Draw(self.img)
#文字行距
magrinTop=180
x=int(self.width/2)-self.magrinImgLeft
y=self.background_height+self.magrinImgTop
font = ImageFont.truetype('wryh.ttf', 150)
draw.text((x,y), tmp, font=font, fill='#ffffff')
x=x
y=y+magrinTop
font = ImageFont.truetype('wryh.ttf', 50)
draw.text((x,y), weather+' '+week, font=font, fill='#ffffff')
#添加小图
def drawIcon(self):
x_magrinImgLeft_add=30
x=self.magrinImgLeft+x_magrinImgLeft_add
y=int(self.height/2)+self.magrinImgTop
self.img.paste(self.icon,(x,y),self.icon)
#保存到本地
def saveCard(self):
save_name = 'img/'+str(datetime.today()).split()[0]+'.jpg'
self.img.save(save_name)
return save_name
def drawCard(self):
self.loadMoban()
background_url=getBingBackground()
self.loadBackground(background_url)
self.loadIcon()
self.drawBlur()
self.drawTitle()
self.drawBackground()
self.drawIcon()