


In this method we now get all of the file’s data as bytes using default_storage.open() to open the file, which should work no matter where it’s stored.Also as before, when saving, if the file has changed, and there is an image, we call the sanitize_cover_exif_data() method.As before, we store the path ( name) of the image file so that, when saving the object we can tell if it’s changed.save ( filename, ContentFile ( temp_file. delete ( save = False ) # Finally, save the temporary file as the new cover, with same name: self. insert ( exif_bytes, file_data, temp_file ) # Remove existing image from disk (or else, when we save the new one, # we'll end up with both files, the new one with a different name): filename = os. and replace its Exif data with our updated data: piexif. dump ( exif_dict ) # Create a temporary file with existing data. load ( "foo.jpg" ) if "GPS" in exif_dict and len ( exif_dict ) > 0 : exif_dict = exif_bytes = piexif.
Edit exif metadata in python install#
Outside of Django, the way we’d remove the GPS info from a file’s Exif data using Piexif is like this, after installing it ( pip install piexif):
Edit exif metadata in python how to#
So, if we never display the original image on our site, why do we need to bother editing its Exif data? Because if we’re keeping the original file around on a publicly-readable server someone could still work out how to view it and so read its data. Often we generate smaller versions for display and these probably don’t contain any of the original’s Exif data. One note: In many cases we wouldn’t use the original uploaded file on our website. This doesn’t seem encouraging but it’s been OK for my purposes so far. On GitHub it hasn’t had any updates for a couple of years and has a bunch of outstanding pull requests and forks containing bug fixes. I ruled out (1) and (2) because I didn’t want to re-save the file, and (3) and (4) because I didn’t want to rely on command-line tools or other libraries. Use the Piexif module, which allows for writing modified Exif tags back to the original file and has no dependencies. It looks like it’s possible to write modified Exif tags back to the original file. Use python3-exiv2, a python binding to exiv2, which is a C++ library. Obviously, this requires exiftool to be available on your system. Pipe out to the well-regarded exiftool command-line application, as described in this blog post, to modify the file. If I’ve understood the module’s documentation correctly this also requires a new, lossy, image to be saved. Use the exif python module as described in this blog post. This seems a popular choice on Stack Overflow (such as this answer), but it results in a re-saved image – more lossy compression. Use PIL or Pillow to open the image and save a copy the copy won’t contain any Exif data. Leaving Django aside for the moment, I found several ways to remove Exif data from images using Python: Ideally, I didn’t want to affect the content of the image itself – re-saving it as a new JPEG image would add another round of compression – but solely edit the Exif data in the file. I wanted to automatically strip out any location information from the uploaded file’s Exif data. I wondered how to do this on a Django website and this is what I came up with.Ī Django model could have an ImageField, and an image file can be uploaded to this when creating or editing the object. The data could, for example, include the latitude and longitude where the user was when they took that selfie they’re using for their avatar. If you have a website that allows users to upload images it might be a good idea to strip some or all of the Exif data embedded in the image files.
