考虑以下代码: from PIL import Image, ImageDraw, ImageFont
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')
这是输入和输出文件 这是输入
输入 http://img16.imageshack.us/img16/8229/73936270.png
这是输出
输出 http://img94.imageshack.us/img94/531/outj.png
如您所见,输出图像在文本周围包含大量红色噪点。我怎样才能消除这种抖动?