使用Pillow(PIL)库实现中文字符画

上班摸鱼写的,不多说了,直接上脚本

 1 #coding=utf-8
 2 from PIL import Image
 3 from PIL import ImageDraw
 4 from PIL import ImageFont
 5 txt = '给我也整一个'
 6 font = ImageFont.truetype('simsun.ttc',9)  #9为字体大小
 7 im_path = 'gei.png'  #原图路径
 8 im = Image.open(im_path)
 9 width, height = im.size 
10 newImg = Image.new("RGBA",(width, height),(10,10,10))  #背景色rgb,偏黑显示好一些
11 x=0
12 for i in range(0,height,9):   #需要与字体大小一致
13     for j in range(0,width,9): #需要与字体大小一致
14         a,b,c=im.getpixel((j,i))
15         draw = ImageDraw.Draw(newImg)
16         draw.text( (j,i), unicode(txt[x%(len(txt)):x%(len(txt))+3],'UTF-8'), fill=(a,b,c),font=font)
17         x+=3
18         del draw
19 newImg.save('00.png','PNG')

使用Pillow(PIL)库实现中文字符画_第1张图片

使用Pillow(PIL)库实现中文字符画_第2张图片

效果图以及字体文件:

下载地址:字符画.zip

转载于:https://www.cnblogs.com/kagari/p/10286247.html

你可能感兴趣的:(使用Pillow(PIL)库实现中文字符画)