问题
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