Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove sensitive EXIF metadata #174

Open
epou opened this issue Nov 21, 2023 · 3 comments
Open

Remove sensitive EXIF metadata #174

epou opened this issue Nov 21, 2023 · 3 comments

Comments

@epou
Copy link
Member

epou commented Nov 21, 2023

Example

import piexif
from PIL import Image

def remove_sensitive_data(input_image_path, output_image_path):
    # Open the image using Pillow (PIL)
    image = Image.open(input_image_path)

    # Get the Exif data from the image
    exif_dict = piexif.load(image.info["exif"])

    # Remove sensitive fields from the Exif data
    sensitive_tags = [
        piexif.ExifIFD.Artist,  # Artist/Author information
        piexif.ExifIFD.DateTimeOriginal,  # Date and time when the photo was taken
        piexif.GPSIFD.GPSLatitude,  # GPS Latitude
        piexif.GPSIFD.GPSLongitude,  # GPS Longitude
        piexif.ImageIFD.Make,  # Camera make
        piexif.ImageIFD.Model,  # Camera model
    ]

    for ifd in ("0th", "Exif", "GPS", "1st"):
        for tag in sensitive_tags:
            if tag in exif_dict[ifd]:
                exif_dict[ifd].pop(tag)

    # Save the modified Exif data back to the image
    exif_bytes = piexif.dump(exif_dict)
    image.save(output_image_path, exif=exif_bytes)

# Example usage
input_image_path = "input.jpg"
output_image_path = "output.jpg"
remove_sensitive_data(input_image_path, output_image_path)

Also see: #52

@epou epou self-assigned this Nov 21, 2023
@epou epou added this to the v2 prod release milestone Nov 21, 2023
@epou
Copy link
Member Author

epou commented Dec 18, 2023

Be aware of the EXIF.Orientation tag.

@nikaxx proposed two possible solutions:

  1. retain the original EXIF orientation metadata.
  2. rotate the image based on the EXIF orientation metadata value, save the already rotated image to the server, and remove the tag completely or reset the EXIF orientation tag to "0".

A value of "0" in EXIF orientation tag signifies that the tag should be ignored according to the documentation.

@nikaxx
Copy link

nikaxx commented Dec 18, 2023

Please also note to transform previous images that are already on the server, after choosing one of the two proposed solutions to keep the consistency.

@nikaxx
Copy link

nikaxx commented Dec 18, 2023

Example of how to find EXIF orientation tag:

from PIL import Image, ExifTags, ImageOps
from PIL.ExifTags import TAGS 



# find exif orientation metadata of an image
def find_image_exif_orientation(original_image_file):
    orientation = None
    with Image.open(original_image_file) as pilImg:
        img_exif = pilImg.getexif()
        if img_exif is not None:
            for key, val in img_exif.items():
                if key in ExifTags.TAGS and key == 274:
                    orientation = val
                #print('{}: {} key number:{}'.format(TAGS[key], val, key))
    return orientation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants