How to set DPI correctly when saving pillow image?

I am trying to create images programmatically in Python using the Pillow library , but I am having problems with the image quality of the text inside the image.

I want to save the image that I generate in PNG, so I set 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 this is as follows:

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)) 

Low_quality.png:

Image dpi = 72

High_quality.png:

Image dpi = 600

As you can see in the images, the quality has not changed. What am I doing wrong here?

Where to set DPI so that the image really has dpi = 600?

+6
source share
1 answer

DPI values ​​are computer image metadata only. They give tips on how to display or print the image.

Printing a 360 Γ— 360 image with a resolution of 360 dpi will result in a 1 Γ— 1 inch printout.

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

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

+12
source

All Articles