0
考慮下面的代碼在文本抖動: 從PIL進口圖片,ImageDraw,ImageFont使用PIL和TrueType字體
def addText(img, lTxt):
FONT_SIZE = 10
INTERLINE_DISTANCE = FONT_SIZE + 1
font = ImageFont.truetype('arial.ttf', FONT_SIZE)
lTxtImageHeight = INTERLINE_DISTANCE * len(lTxt)
# create text image
lTxtImg = Image.new('RGBA', (img.size[1], lTxtImageHeight), 255)
lTxtImgDraw = ImageDraw.Draw(lTxtImg,)
for (i, line) in enumerate(lTxt):
lTxtImgDraw.text((5, i * INTERLINE_DISTANCE), line, font=font, fill='#000000')
# rotate text image
lTxtImg = lTxtImg.rotate(90)
# create new transparent image ret
ret = Image.new('RGBA', (img.size[0] + lTxtImageHeight, img.size[1]), 255)
# paste the image to ret
ret.paste(img, (0,0))
# paste the text to ret
ret.paste(lTxtImg, (img.size[0], 0), lTxtImg)
return ret
img = Image.open('in.png')
addText(img, ['lorem', 'ipsum', 'dolores']).save('out.png')
Here are the input and the output files 這是輸入
input http://img16.imageshack.us/img16/8229/73936270.png
,這是輸出
output http://img94.imageshack.us/img94/531/outj.png
如您所見,輸出圖像在文本週圍包含許多微紅的噪音。我怎樣才能消除這種抖動?
首先,在旋轉之前和之後保存'lTxtImg'。比較圖像:他們都有紅色的「噪音」? 順便說一句,這個字體是*不*宋體,這是一個襯線字體。你的操作系統是什麼? – tzot 2010-02-01 23:51:11