← All Articles A Product of Kinsa Creative

Remove Meta Data from an Image in Python with PIL

EXIF meta data can be stripped from an image with the Pillow library by opening the image, creating a new image without copying the meta data, and saving. Doing this requires loading the entire image into memory before it is written.

Install the Pillow library

$ pip install pillow

Remove EXIF Data by Replicating the Image via PIL

from PIL import Image

image = Image.open('/path/to/file.jpg')
data = list(image.getdata())
image2 = Image.new(image.mode, image.size)
image2.putdata(data)
image2.save('/path/to/newfile.jpg')

Retaining or Reading EXIF Data from an Image Using PIL

If you wish to retain EXIF data from the original to the copied image, you need to do so explicitly by reading the EXIF data from the image and setting it via the exif kwarg on save:

exif = image.info['exif']
image2.save('/path/to/newfile.jpg', exif=exif)

To print EXIF data from an image:

from PIL import Image
from PIL.ExifTags import TAGS

image = Image.open('/path/to/file.jpg')
for tag, value in image._getexif().items():
    print(TAGS.get(tag), value)

Feedback?

Email us at enquiries@kinsa.cc.