Image size shrunk out of nowhere

允我心安 提交于 2019-12-20 03:22:21

问题


I simply copied my image and saved it to another temp folder in the current directory, nothing is modified, but the image size is somehow reduced... why?

from PIL import Image
import os

image_path = "/Users/moomoochen/Desktop/XXXXX.jpg"
img = Image.open(image_path)
pathname, filename = os.path.split(image_path)

new_pathname = (pathname + "/temp")

if not os.path.exists(new_pathname):
  os.makedirs(new_pathname)
  img.save(os.path.join(new_pathname, filename))

The image size is reduced quite a bit, from 3.2 MB down to 350 KB, what did I miss?


回答1:


When PIL/Pillow saves your image as a JPEG, it uses a default quality of 75 and this is likely lower than the quality at which your original image was saved, hence the file is smaller.

You can readily check the quality of your input and output files with jhead like this:

jhead image.jpg

Sample Output

File name    : image.jpg
File size    : 199131 bytes
File date    : 2018:11:13 09:42:59
Resolution   : 1374 x 1182
JPEG Quality : 75

If you wish to retain more quality, you can specify a different value from 75 when saving. It is not recommended to go above 95 as it increases file size with no benefit:

img.save('result.jpg', quality=90)


来源:https://stackoverflow.com/questions/53149385/image-size-shrunk-out-of-nowhere

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