利用python pil 实现给图片上添加文字
图片中添加文字
#-*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont ttfont = ImageFont.truetype("D:Python目录msyh.ttc",20) #//这里我之前使用Arial.ttf时不能打出中文,用华文细黑就可以 im = Image.open("D:客户程序\1.jpg") draw = ImageDraw.Draw(im) draw.text((10,10),u'韩寒', fill=(0,0,0),font=ttfont) draw.text((40,40),u'杨利伟', fill=(0,0,0),font=ttfont) im.show()
text=u'杨利伟' Font = ImageFont.truetype("D:Python目录msyh.ttc",20) Font.getsize(text) >>>(60, 25)
Python用Pillow(PIL)进行简单的图像操作
用Python来合并图片(SoEasy)
python PIL合并半透明的png图片
转换通道
https://blog.csdn.net/icamera0/article/details/50843172
重新设置图片大小
a=font_background.resize((10,10),Image.ANTIALIAS)
半成品
#-*- coding: utf-8 -*- from PIL import Image,ImageDraw,ImageFont 字号=30 字体="C:WindowsFontssimkai.ttf" 文字和背景向上偏移=300 单独文字上下偏移=0 文字左右偏移=0 黑背景左右偏移=0 文字颜色=(255,255,255) 水印文字='小哈nizaig你在干嘛'.replace(' ','') 方背景宽度增加=-int(字号/1.3) ttfont = ImageFont.truetype(字体,字号)#字体,和字体大小 im = Image.open("D:客户程序\1.jpg")#要加文字的图 im=im.convert('RGBA') draw = ImageDraw.Draw(im,mode='RGBA') font_background=Image.open("D:Python目录方.png")#水印背景圆角图 round=Image.open("D:Python目录圆.png") font=u'%s'%水印文字 im_size=im.size#原图大小 font_size=ttfont.getsize(font)#文字尺寸 font_background_a=font_background.resize((font_size[0]+方背景宽度增加,font_size[1]),Image.ANTIALIAS)#方背景大小尺寸 round_W_H=round.resize((font_size[1],font_size[1]),Image.ANTIALIAS)#圆的大小 pos_ads_img=(int(im_size[0]/2-font_background_a.size[0]/2+黑背景左右偏移),文字和背景向上偏移)#方图片位置 round_ads_left=(int(pos_ads_img[0]-round_W_H.size[0]/2),pos_ads_img[1])#圆图1的位置 font_ads=(int(im_size[0]/2-font_size[0]/2+文字左右偏移),文字和背景向上偏移+单独文字上下偏移) round_ads_right=(int(pos_ads_img[0]+font_background_a.size[0]-round_W_H.size[0]/2),pos_ads_img[1]) im.paste(round_W_H,round_ads_left,mask=round_W_H)#圆背景摆放位置 im.paste(round_W_H,round_ads_right,mask=round_W_H)#圆背景摆放位置 im.paste(font_background_a,pos_ads_img)#方形摆放位置 draw.text(font_ads,font, fill=(文字颜色),font=ttfont)#文字摆放位置,文字,颜色 im.show()