[英]How do I properly set DPI when saving a pillow image?
I am trying to create images programatically on Python using Pillow library but I'm having problems with the image quality of the text inside the image. 我正在尝试使用Pillow库以编程方式在Python上创建图像,但我在图像内部的文本图像质量方面存在问题。
I want to save the Image the I generate to PNG, so I'm setting the DPI when saving according to this , but whether I save with dpi=(72,72) or dpi=(600,600) it visually looks the same. 我想将我生成的图像保存到PNG,所以我在保存时根据此设置DPI,但是无论我用dpi =(72,72)还是dpi =(600,600)保存,它在视觉上看起来都是一样的。
My code for doing it is the following: 我的代码如下:
from PIL import Image, ImageDraw, ImageFont
def generate_empty_canvas(width, height, color='white'):
size = (width, height)
return Image.new('RGB', size, color=color)
def draw_text(text, canvas):
font = ImageFont.truetype('Verdana.ttf', 10)
draw = ImageDraw.Draw(canvas)
if '\n' not in text:
draw.text((0, 0), text, font=font, fill='black')
else:
draw.multiline_text((0, 0), text, font=font, fill='black')
def create_sample():
text = 'aaaaaaaaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbb\nccccccccccccccccccccc'
canvas = generate_empty_canvas(200, 50)
draw_text(text, canvas)
canvas.save('low_quality.png', dpi=(72, 72))
canvas.save('high_quality.png', dpi=(600, 600))
The low_quality.png is: low_quality.png是:
The high_quality.png is: high_quality.png是:
As it's visible by the images the quality didn't change. 由于图像可见,质量没有变化。 What am I doing wrong here? 我在这做错了什么?
Where do I set the DPI so that the image really has dpi=600? 我在哪里设置DPI,使图像真的有dpi = 600?
The DPI values are only metadata on computer images. DPI值仅是计算机图像上的元数据。 They give hints on how to display or print an image. 它们提供了有关如何显示或打印图像的提示。
Printing a 360×360 image with 360 dpi will result in a 1×1 inches printout. 使用360 dpi打印360×360图像将导致1×1英寸的打印输出。
A simplified way to explain it: The DPI setting recommends a zoom level for the image. 解释它的简化方法:DPI设置建议图像的缩放级别。
Saving with other DPIs will not change the content of the image. 使用其他DPI保存不会更改图像的内容。 If you want a larger image create a larger canvas and use a larger font. 如果您想要更大的图像,请创建更大的画布并使用更大的字体。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.