保存枕头图像时如何正确设置DPI? [英] How do I properly set DPI when saving a pillow image?

查看:93
本文介绍了保存枕头图像时如何正确设置DPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用枕头库在Python上以编程方式创建图像,但是我在图片中的文字的图片质量上遇到了问题.

我想将生成的图像保存为PNG,所以我在保存时根据解决方案

DPI值仅是计算机映像上的元数据.它们提示如何显示或打印图像.

以360 dpi打印360×360图像将导致1×1英寸的打印输出.

一种简单的解释方式:DPI设置建议图像的缩放级别.

与其他DPI一起保存不会更改图像的内容.如果您想要更大的图像,请创建更大的画布并使用更大的字体.

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.

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.

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:

The high_quality.png is:

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?

解决方案

The DPI values are only metadata on computer images. 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.

A simplified way to explain it: The DPI setting recommends a zoom level for the image.

Saving with other DPIs will not change the content of the image. If you want a larger image create a larger canvas and use a larger font.

这篇关于保存枕头图像时如何正确设置DPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆