我正在使用 Python 2.7.3 和 PIL 1.1.7 在 Windows 7 平台上进行开发。
我正在尝试编写一个 python 脚本来生成一组带有文本的图像。由于我需要包装文本并将其放入任意边界框中,因此我编写了一个方法,该方法将文本绘制到白色 RGBA 背景图像上,并打开 alpha 透明层。为了简化问题,我写了一个小python脚本来说明问题:
import Image,ImageDraw,ImageFont
import webbrowser
# sample text and font
text = "The text quality will degrade with the paste operation."
verdana_font = ImageFont.truetype("verdana.ttf", 20)
# get the line size
text_width, text_height = verdana_font.getsize(text)
# create a blank canvas with extra space between lines
blank_canvas = Image.new('RGB', (text_width + 10, text_height * 10 + 5 * 10), (255, 255, 255))
# create a blank RGBA canvas for the drawn text
text_canvas = Image.new('RGBA', (text_width, text_height), (255, 255, 255, 0))
draw = ImageDraw.Draw(text_canvas)
# draw the text onto the text canvas
draw.text((0,0), text, font = verdana_font, fill = "#000000")
# print 10 lines
for x in range(0,10):
# calculate the coordinates for the paste operation and debug
coordinates = (5, 5 + (x * (5 + text_height)))
print "x = %d | coordinates = %r" % (x, coordinates)
# paste the text onto the blank canvas
blank_canvas.paste(text_canvas, coordinates, text_canvas)
# create a temporary canvas
temp_canvas = Image.new('RGBA', (text_width, text_height), (0, 0, 0, 0))
# paste the text canvas onto the temp canvas using the png alpha layer for transparency
temp_canvas.paste(text_canvas, (0,0), text_canvas)
# swap the canvases
text_canvas = temp_canvas
# save the blank canvas to a file
blank_canvas.save("paste-degradation.png", "PNG")
# open the image up to see the paste operation's image degradation
webbrowser.open("paste-degradation.png")
每次将文本粘贴到新的“临时”画布上时,所绘制文本的图像质量都会越来越差。上面的代码生成的图像如下所示:
我的代码有问题吗?还是PIL中有错误?