pil写扭曲中文示例

软件和网站开发以及相关技术探讨
回复
头像
oneleaf
论坛管理员
帖子: 10441
注册时间: 2005-03-27 0:06
系统: Ubuntu 12.04

pil写扭曲中文示例

#1

帖子 oneleaf » 2009-01-18 21:56

来源: http://blog.chinaunix.net/u1/57278/showart_1422657.html

代码: 全选

#-*-  encoding: utf-8  -*-
import Image
import ImageFont, ImageDraw

chinacha = '世界'
im = Image.new('RGBA',(400,300))
draw = ImageDraw.Draw(im)
font = ImageFont.truetype('msyh.ttf',30)
draw.text((10,25),'world',font=font,fill='rgb(110,0,255)')
draw.text((100,25),chinacha.decode('gb2312'),font=font,fill='rgb(110,0,255)')
# 隶书
font1 = ImageFont.truetype('STLITI.TTF',50)
draw.text((100,100),chinacha.decode('gb2312'),font=font1,fill='green')
#transfomrdata = (1.1,-0.5,0,-0.5,1.3,0,)
#im = im.transform((400,300),Image.AFFINE,transfomrdata)
im.save('e:\piltest2.png')


import math,random
im = Image.open('e:\piltest2.png','r')
newim = Image.new('RGBA',(400,300))

pix = im.load()
newpix = newim.load()
w, h = im.size
#offx = random.randint(5,10) # x方向位移
#offy = 2  #振幅
#angley = random.random() #   recommend: 0 ~ 1,  摆动频率
#测试下来,修改angley效果最佳,可在0~1之间,或者<50, >50之类出来的效果都还是有的,
#具体里面的函数关系折腾不出来了,琢磨着和字体大小也有关系
offx = 10
offy = 2
angley = 10
for x in range(0,w):
    for y in range(0,h):
        x1 = int(x -offx + offy * math.cos(angley * y))
        if x1 < w and x1 > 0:  newpix[x1, y] = pix[x, y]

newim.save('e:\piltest5.png')
回复