Read and Edit Image Metadata with Python | by Kenneth Leung | Towards Data Science

For every photo, there is more than meets the eye. The images taken with digital cameras and smartphones contain rich information (known as metadata) beyond the visible pixels.
This metadata can be helpful in many business cases. For instance, fraud detection systems for insurance claims analyze metadata of submitted photographs to check whether the claimant took them before the accident.
In this article, we explore how to use the exif library to read and edit metadata of digital images.
(1) What are Metadata and Exif?(2) Read Image Metadata(3) Modify Image Metadata(4) Save Modified Image Metadata(5) Wrapping It Up
Feel free to check out all the codes in the accompanying GitHub repo.
Metadata refers to the set of data describing the data, and you can think of it as data about the data. The metadata of photos consists of information such as camera model and date of capture.
This metadata is stored in Exif (Exchangeable image file format), a format standard for the various types of media (e.g. images, videos, audio) taken by devices such as digital cameras and smartphones.
The Python library used in this project is exif, which happens to be the namesake of the Exif format.
We start by installing exif with this command:
pip install exif
For this demo, here is the photo that we will be working on:
We instantiate an exif Image Class by reading the image in a binary format before checking whether it contains any metadata. If so, the has_exif method will return True.
Different photos have varying metadata attributes (aka Exif tags) due to the different devices used to capture them. We can view the existing attributes for an image with the list_all() method.
To read values of specific attributes, we can use the get() method. While there are other methods, I prefer get() as it gracefully handles cases where attributes do not exist by returning None (instead of throwing an error).
P.S. Check out the Image_Metadata_Extraction_EXIF.ipynb notebook for the function to extract all metadata of an image into a Pandas DataFrame.
Besides reading metadata, we can perform a series of modifications such as adding, updating, and deleting attributes.
New attributes not currently present can be added to further enrich the metadata.
One important thing to note is that the attribute added must be a recognized EXIF tag. Otherwise, the addition will not take place. You can find the complete list of recognized image attributes here.
For example, we can add the recognized copyright attribute. After assigning a value (Kenneth Leung 2021) to the copyright attribute, the get() method will give us this new value instead of None.
We can also update the existing values of the image metadata attributes.
If we want to delete specific attributes instead of updating them, we can do so with .delete().
After all the modifications, the final step is to save the image with the modified metadata as a new file.
With the above, we have saved the modified image using a filename with the prefix of ‘modified_’ so that the original image is not overwritten.
There are many other interesting attributes to explore, and you can find more details on the exif documentation page.
What we have done so far is process a single image. The value of the exif package is realized through batch processing, where the extraction and modification of metadata are done on a large set of images. To see batch processing in action, have a look at the batch_process_metadata.py script in the GitHub repo.
An important thing to keep in mind is to back up your photos before using this library to prevent any unexpected data loss.
I welcome you to join me on a data science learning journey! Follow this Medium page and check out my GitHub to stay in the loop of more exciting data science content. Meanwhile, have fun reading and modifying image metadata!