字体输出成字符(pil应用)
时间:2009-04-03 来源:stlaw
就是将一些各种字体渲染过的字符,用单独的字母显示,不知咋说。
今天突然想起,咋以前没想到呢。
下面的示例用pil, 字体用的是隶书。windows下环境。
输出到1个文件,想得出清晰点的就靠改变fontsize了;
下面有空看看如何折腾图像
import Image,ImageFont
import random
def char2txt(inputchar,outfile='char2txt.out',fontsize=30,truetypefile='STLITI.TTF',
randstr='#$%*'):
font = ImageFont.truetype(truetypefile,fontsize)
w, h = font.getsize(inputchar)
im = Image.new('RGBA',(w,h))
draw = ImageDraw.Draw(im)
draw.text((0,0),inputchar.decode('gb2312'),font=font,fill='rgb(110,0,255)')
imgdata = list(im.getdata())
outfh=open(outfile,'wb')
for i in range(len(imgdata)):
if imgdata[i] == (0,0,0,0): outfh.write(' ')
else: outfh.write(random.choice(randstr))
if ((i+1) % w) == 0: outfh.write('\n')
outfh.close()
char2txt('清明')
_________________#$___________________________________________
_________________*#*__________________________________________
___*#$$*$__$#*$$$#%##*#***______#$*%$#%*$*$%#_$#%%$**#%%$_____
___%$%$$%#_%#**$#$$$$%*#$_______$*%###%$**#*%_$##**$*#$%%$____
___****##*______$%%#%#$#*_______$#*%$%##%%$$%_$##%*#%%%$*_____
___%%*#$%__%$%#*%######%$________$*%%#__#*$$$_$%%##%*$%**_____
___________*%$#*$#$##*#*___%_____##$**%%***%__%%*___$$$$*_____
____#*$_____$$%###$$%#%*%%##_____$$###%$*#$%_*#%$*$%$##*______
__%$**%$__%$#$*%*%$*#*$#$*$$_____$$##%###$$*_%#*******$*______
__$$%$$%__$%#%*$%____$$*%$*#_____$$#$*%#*%#*_%*#$$$#*$#$______
__*#$$#______$%#$**#*$$*_________*$%___*$%*$_#%%$*#$*#$#______
_____________$#$#****$%#_________%$#%%%$*$$*$%$*##%#%%%#______
______*$*____#*#$%#%*%*#_________*#*$%$%%%#_**#*%%*%%***______
_____*##%___$##%$$%*%#%#_________#$%$%##$$%_*$$$#%#*$%*%______
___$*$%*____$%$$%*#_#**__________%$#*%%%___#%*##$*%#**%%______
__$$#%*_____*$$#*#$*%%%#________##$$______%$$$%_____%%%_______
__$$%$______*%$##*##**#$_________________*%$#$______%$%_______
__%$#______$$$##*$##*##*____________$%####%$*#______#$*_______
_________***$$$______%%*____________*#*%$%*#$_______*#$_______
________%#$###_______**$_____________*$$%$%*________#%*_______
________%*%##*_______#%*____________________________#$#_______
_____________________##%____________________________#%#_______
(再无聊,web显示很诡异,明显是发胖的味)
今天突然想起,咋以前没想到呢。
下面的示例用pil, 字体用的是隶书。windows下环境。
输出到1个文件,想得出清晰点的就靠改变fontsize了;
下面有空看看如何折腾图像
import Image,ImageFont
import random
def char2txt(inputchar,outfile='char2txt.out',fontsize=30,truetypefile='STLITI.TTF',
randstr='#$%*'):
font = ImageFont.truetype(truetypefile,fontsize)
w, h = font.getsize(inputchar)
im = Image.new('RGBA',(w,h))
draw = ImageDraw.Draw(im)
draw.text((0,0),inputchar.decode('gb2312'),font=font,fill='rgb(110,0,255)')
imgdata = list(im.getdata())
outfh=open(outfile,'wb')
for i in range(len(imgdata)):
if imgdata[i] == (0,0,0,0): outfh.write(' ')
else: outfh.write(random.choice(randstr))
if ((i+1) % w) == 0: outfh.write('\n')
outfh.close()
char2txt('清明')
_________________#$___________________________________________
_________________*#*__________________________________________
___*#$$*$__$#*$$$#%##*#***______#$*%$#%*$*$%#_$#%%$**#%%$_____
___%$%$$%#_%#**$#$$$$%*#$_______$*%###%$**#*%_$##**$*#$%%$____
___****##*______$%%#%#$#*_______$#*%$%##%%$$%_$##%*#%%%$*_____
___%%*#$%__%$%#*%######%$________$*%%#__#*$$$_$%%##%*$%**_____
___________*%$#*$#$##*#*___%_____##$**%%***%__%%*___$$$$*_____
____#*$_____$$%###$$%#%*%%##_____$$###%$*#$%_*#%$*$%$##*______
__%$**%$__%$#$*%*%$*#*$#$*$$_____$$##%###$$*_%#*******$*______
__$$%$$%__$%#%*$%____$$*%$*#_____$$#$*%#*%#*_%*#$$$#*$#$______
__*#$$#______$%#$**#*$$*_________*$%___*$%*$_#%%$*#$*#$#______
_____________$#$#****$%#_________%$#%%%$*$$*$%$*##%#%%%#______
______*$*____#*#$%#%*%*#_________*#*$%$%%%#_**#*%%*%%***______
_____*##%___$##%$$%*%#%#_________#$%$%##$$%_*$$$#%#*$%*%______
___$*$%*____$%$$%*#_#**__________%$#*%%%___#%*##$*%#**%%______
__$$#%*_____*$$#*#$*%%%#________##$$______%$$$%_____%%%_______
__$$%$______*%$##*##**#$_________________*%$#$______%$%_______
__%$#______$$$##*$##*##*____________$%####%$*#______#$*_______
_________***$$$______%%*____________*#*%$%*#$_______*#$_______
________%#$###_______**$_____________*$$%$%*________#%*_______
________%*%##*_______#%*____________________________#$#_______
_____________________##%____________________________#%#_______
(再无聊,web显示很诡异,明显是发胖的味)
相关阅读 更多 +