简体   繁体   English

以编程方式更改图像分辨率

[英]Programmatically change image resolution

I have calculated that if I want my generated image to be A4 size @ 600dpi for print purpose, it needs to be 7016x4961px @ 72dpi.我已经计算过,如果我希望生成的图像为 A4 大小 @ 600dpi 以进行打印,则它需要为 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所以,我以编程方式生成它,然后在 Photoshop 中测试它,它似乎没问题,所以如果我调整它的大小,它会得到正确的大小和分辨率

Photoshop 中的图像大小对话框 . .

What I wonder about is if it's possible to make this resizing programmatically, preferably with PIL, but not necessarily with it.我想知道是否有可能以编程方式调整大小,最好使用 PIL,但不一定使用它。 I need to make it higher DPI.我需要让它更高的 DPI。

If you have generated your image 7016 x 4961 px, it is already A4 at 600 dpi.如果您生成的图像为 7016 x 4961 像素,则它已经是 600 dpi 的 A4。 So you don't need to resize it, you just have to set resolution information in file.所以你不需要调整它的大小,你只需要在文件中设置分辨率信息。

You can do it with PIL:你可以用 PIL 做到这一点:

from PIL import Image

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

This code will resize a PNG image into 7016x4961 with PIL:此代码将使用 PIL 将 PNG 图像大小调整为 7016x4961:

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).也许更好的方法是在打印之前将画布放大x倍,其中x是您必须弄清楚的一个因素(此特定图像的大小为 7016x4961)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM