Search code examples
pythonpython-2.7python-imaging-libraryfilesize

File size went up instead of down when exif removed with PIL?


Not a huge deal, but for the sake of understanding what's going on to avoid future problems...

I'd like to know why the file size of a jpg is going up after the exif data is removed. I thought it was supposed to go down?

from PIL import Image

image = Image.open(fname)

name, ext = get_fname_ext(fname)

out_name = name + '_cleaned' + ext
out_abs = output_dir + "\\" + out_name

image.save(out_abs)

Filesize before: 192.65 KB

Filesize after: 202.46 KB

Difference: +9.82 KB


Solution

  • What happens here is that PIL recompresses the image (the source is a JPG, but it doesn't have to, so it's treated as image data). It would be safer/easier to rely on an external tool like exiftool, imagemagick or jpegtran. The answers on this related SO question might be a good resource.

    As an PIL-only alternative, you might try if the python snippet from this answer works for you:

    from PIL import Image
    
    image = Image.open('image_file.jpeg')
    
    # next 3 lines strip exif
    data = list(image.getdata())
    image_without_exif = Image.new(image.mode, image.size)
    image_without_exif.putdata(data)
    
    image_without_exif.save('image_file_without_exif.jpeg')