How to get image size in python-pillow after resize?

混江龙づ霸主 提交于 2019-12-11 02:17:39

问题


resized_image = Image.resize((100,200));

Image is Python-Pillow Image class, and i've used the resize function to resize the original image,

How do i find the new file-size (in bytes) of the resized_image without having to save to disk and then reading it again


回答1:


The file doesn't have to be written to disk. A file like object does the trick:

from io import BytesIO

# do something that defines `image`...   
img_file = BytesIO()
image.save(img_file, 'png')
print(img_file.tell())

This prints the size in bytes of the image saved in PNG format without saving to disk.




回答2:


You can't. PIL deals with image manipulations in memory. There's no way of knowing the size it will have on disk in a specific format.

You can save it to a temp file and read the size using os.stat('/tmp/tempfile.jpg').st_size



来源:https://stackoverflow.com/questions/29368155/how-to-get-image-size-in-python-pillow-after-resize

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