Search code examples
pythonimagesizepython-imaging-libraryjpeg

Image size shrunk out of nowhere


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?


Solution

  • 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)