我正在处理python脚本以创建输入数据到预先创建的图像。 为此,我使用的是Python的PILlon,我正在开发Windows 7。
我正在努力使用函数draw.text(),其中我输入的位置是(0,0)。不幸的是,文本被放置了一堆像素。
首先,我认为这是我的错误,我搜索网页并找到了页面http://tools.jedutils.com/tools/center-text-image,其中(居中,但问题是相同的)代码是:
def create_image(font_name, font_size, font_color, width, height, back_ground_color, text, image_filter=None):
font = ImageFont.truetype ( font_path + font_name + ".ttf", font_size )
im = Image.new ( "RGB", (width,height), back_ground_color )
draw = ImageDraw.Draw ( im )
text_x, text_y = font.getsize(text)
x = (width - text_x)/2
y = (height - text_y)/2
draw.text ( (x,y), text, font=font, fill=font_color )
#if filter: #i don't need given filter
# real_filter = filter_dict[image_filter]
# im = im.filter(real_filter)
im.save ( "runaway_emboss.jpg" )
执行此代码后,我鼓励出现与我自己的脚本相同的问题。后来我觉得它可能是字体的问题,所以我将一个从Windows Fonts文件夹复制到我的* .py文件位置,我尝试从网上下载字体。一切都有相同的结果。
问题图片:
不好: 好:
你有什么想法,可能是问题的根源?