Last modified: Apr 12, 2025 By Alexander Williams

Python Save Images with Metadata Guide

Working with images in Python often involves handling metadata. Metadata includes details like EXIF data, creation date, and camera settings. This guide shows how to save images while preserving metadata.

Why Save Images with Metadata?

Metadata provides valuable information about images. It helps in organizing, searching, and processing images efficiently. Python makes it easy to work with image metadata.

Using PIL to Save Images with Metadata

The Python Imaging Library (PIL), or Pillow, is a popular choice for image processing. It supports basic metadata handling. Here's how to use it.


from PIL import Image

# Open an image
img = Image.open('input.jpg')

# Save with metadata
img.save('output.jpg', quality=95, exif=img.info.get('exif'))

The save method preserves EXIF data if present. For more advanced metadata handling, check our Python PIL Image Handling Guide.

Working with EXIF Data

EXIF data stores camera-specific information. You can read and modify it before saving the image.


from PIL import Image, ExifTags

img = Image.open('photo.jpg')
exif_data = img._getexif()

if exif_data:
    for tag_id, value in exif_data.items():
        tag = ExifTags.TAGS.get(tag_id, tag_id)
        print(f"{tag}: {value}")

This code prints all available EXIF tags. You can modify the data before saving.

Using piexif Library

For more advanced EXIF operations, use the piexif library. It provides better control over metadata.


import piexif
from PIL import Image

img = Image.open('input.jpg')
exif_dict = piexif.load(img.info['exif'])

# Modify metadata
exif_dict['0th'][piexif.ImageIFD.DateTime] = "2023:01:01 12:00:00"

# Save with new metadata
exif_bytes = piexif.dump(exif_dict)
img.save('output.jpg', exif=exif_bytes)

This example changes the image timestamp. The piexif library offers precise metadata control.

Saving PNG Metadata

PNG files use different metadata formats. The PIL can handle some PNG metadata.


from PIL import Image, PngImagePlugin

img = Image.open('input.png')
meta = PngImagePlugin.PngInfo()

# Add custom metadata
meta.add_text("Author", "John Doe")
meta.add_text("Description", "Sample image")

img.save('output.png', pnginfo=meta)

This adds custom text metadata to PNG files. For more image formats, see our Python Image Libraries Guide.

Verifying Saved Metadata

Always verify that metadata was saved correctly. Use image viewers or Python code to check.


from PIL import Image

img = Image.open('output.jpg')
print(img.info.get('exif', 'No EXIF data found'))


b'Exif data in binary format...'

This confirms metadata preservation. For pixel-level operations, refer to our Python Get Image Pixels Guide.

Common Issues and Solutions

Some image operations strip metadata. Always preserve the original metadata when processing images.

Problem: Resizing removes metadata.
Solution: Copy metadata before processing and reapply it.


from PIL import Image

original = Image.open('input.jpg')
exif_data = original.info.get('exif')

# Process image
resized = original.resize((800, 600))

# Save with original metadata
resized.save('resized.jpg', exif=exif_data)

Conclusion

Python provides multiple ways to save images with metadata. The PIL library handles basic cases, while piexif offers advanced control. Always verify metadata preservation after processing.

For more image processing techniques, explore our comprehensive guides. Proper metadata handling ensures your images remain informative and organized.