About Optimizing Images

In Django projects, you can remove the EXIF information from a photo by resaving the image with Pillow (or PIL):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from PIL import Image

with open('image.jpg', 'rb') as fp:
    image = Image.open(fp)

    # next 3 lines strip the EXIF
    data = list(image.getdata())
    image_without_exif = Image.new(image.mode, image.size)
    image_without_exif.putdata(data)

    # save new image
    image_without_exif.save('image_without_exif.jpg')

That makes the images much smaller in size, but keeps the quality good.

Tips and Tricks Programming Python 3