Programmatically change image resolution

╄→гoц情女王★ 提交于 2020-07-16 16:38:32

问题


I have calculated that if I want my generated image to be A4 size @ 600dpi for print purpose, it needs to be 7016x4961px @ 72dpi. So, I generate it programmatically, then test it in Photoshop and it seems to be fine so if I resize it, it gets proper size and resolution

Image size dialog in Photoshop.

What I wonder about is if it's possible to make this resizing programmatically, preferably with PIL, but not necessarily with it. I need to make it higher DPI.


回答1:


If you have generated your image 7016 x 4961 px, it is already A4 at 600 dpi. So you don't need to resize it, you just have to set resolution information in file.

You can do it with PIL:

from PIL import Image

im = Image.open("test.png")
im.save("test-600.png", dpi=(600,600))



回答2:


This code will resize a PNG image into 7016x4961 with PIL:

size = 7016, 4961
im = Image.open("my_image.png")
im_resized = im.resize(size, Image.ANTIALIAS)
im_resized.save("my_image_resized.png", "PNG")

Perhaps a better approach would be to make your canvas x times bigger prior to printing, where x is a factor you have to figure out (7016x4961 in size for this particular image).



来源:https://stackoverflow.com/questions/9174338/programmatically-change-image-resolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!