How to use subprocess to extract image metadata In Python Pillow

Here we are using the subprocess module to extract image metadata using popen() method which opens a pipe from a command. And this pipe allows the command to send its output to another command. And we are storing the fetched metadata into a dictionary.

Python3




import subprocess
 
imgPath = "C:\\Users\\DELL\\Downloads\\output.jpg"
exeProcess = "hachoir-metadata"
process = subprocess.Popen([exeProcess,imgPath],
                           stdout=subprocess.PIPE,
                           stderr=subprocess.STDOUT,
                           universal_newlines=True)
Dic={}
 
for tag in process.stdout:
        line = tag.strip().split(':')
        Dic[line[0].strip()] = line[-1].strip()
 
for k,v in Dic.items():
    print(k,':', v)


Output:

 



How to extract image metadata in Python?

Prerequisites: PIL

Metadata stands for data about data. In case of images, metadata means details about the image and its production. Some metadata is generated automatically by the capturing device. 

Some details contained by image metadata is as follows:

  • Height
  • Width
  • Date and Time
  • Model etc.

Python has PIL library which makes the task of extracting metadata from an image extremely easy that too by using just a few lines.

Approach:

  • Import the pillow module.
  • Load the image
  • Get the metadata. The metadata so obtained
  • Convert it into human-readable form

There are many types of metadata but in this we are only focusing on Exif metadata

Similar Reads

Using Exif to extract image metadata

These metadata, often created by cameras and other capture devices, include technical information about an image and its capture method, such as exposure settings, capture time, GPS location information and camera model....

Using subprocess to extract image metadata

...