简体   繁体   English

调整大小时的Python图像库图像分辨率

[英]Python Image Library Image Resolution when Resizing

I am trying to shrink some jpeg images from 24X36 inches to 11X16.5 inches using the python image library. 我试图使用python图像库将一些jpeg图像从24X36英寸缩小到11X16.5英寸。 Since PIL deals in pixels this should mean scaling from 7200X 4800 pixels to 3300 X2200 pixels, with my resolution set at 200 pixels/inch, however when I run my script PIL changes the resolution to 72 pixels/inch and I end up with a larger image than i had before. 由于PIL处理像素,这应该意味着从7200X 4800像素缩放到3300 X2200像素,我的分辨率设置为200像素/英寸,但是当我运行我的脚本PIL时将分辨率更改为72像素/英寸,我最终得到更大比我以前的形象。

import Image

im = Image.open("image.jpg")

if im.size == (7200, 4800):
    out = im.resize((3300,2200), Image.ANTIALIAS)
elif im.size == (4800,7200):
    out = im.resize((2200,3300), Image.ANTIALIAS)

out.show()

Is there a way to mantain my image resolution when I'm resizing my images? 当我调整图像大小时,有没有办法保持我的图像分辨率?

thanks for any help! 谢谢你的帮助!

To preserve the DPI, you need to specify it when saving; 要保留DPI,您需要在保存时指定它; the info attribute is not always preserved across image manipulations: 在图像处理中并不总是保留info属性:

dpi = im.info['dpi']  # Warning, throws KeyError if no DPI was set to begin with

# resize, etc.

out.save("out.jpg", dpi=dpi)

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

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