Guide to Extract Image Metadata in Python
This article subtleties how to separate picture metadata in Python. At the point when we take a picture utilizing a computerized camera, or a cell phone, the camera utilizes the Exchangeable Image File Format (EXIF) guidelines to store the picture. The EXIF principles use metatags to determine the data about the picture.
Assuming you need to get to that metadata for the EXIF standard pictures in Python, then, at that point, you want to utilize a picture taking care of the Python library. To deal with pictures in Python, we, by and large, utilize the most famous Python picture taking care of the library, Pillow.
Installing Dependencies
1) The Python pillow Library
Pillow is the true Python picture dealing with a library, and numerous Python picture handling and AI libraries are based on top of it. To introduce Pillow for your Python climate, run the accompanying pip introduce order on your order brief (Windows) or terminal (Linux and macOS):
pip install Pillow
2) The Python prettytable Library
prettytable is an open-source Python library that is used to print data in a tabular format. We will be using this library in this tutorial to print all the metadata information in a tabular format. To install the prettytable library for your Python environment, use the following pip install command:
pip install prettytable
Method to extract image metadata in Python?
Start with launching your best Python IDE or text editor and start with importing the required modules from the Pillow and pretty table libraries.
#load modules
from PIL import Image
from PIL.ExifTags import TAGS
from prettytable import PrettyTable
Next, set a Python identifier image_filename
that will hold the file name of the image, and also initialize the pretty table library’s PrettyTable() module object and set its fields.
image_filename = "image.jpg"
#initialiting prettytable object
table = PrettyTable()
#setting table feilds name
table.field_names = ["MetaTags", "Values"]
Python script using the Image module.
#load image
my_image = Image.open(image_filename)
The getexif() strategy just returns the labels IDs and their qualities, and not the label names. That is the reason we have likewise imported the PIL.ExifTags labels that can recover the EXIF label name dependent on its ID. Presently we will circle through the IDs present in img_exif_data and get their label names utilizing the TAGS.get() strategy and qualities utilizing the img_exif_data.get() technique.
for id in img_exif_data:
tag_name = TAGS.get(id, id)
data = img_exif_data.get(id)
#if data in bytes
if isinstance(data, bytes):
data = data.decode()
#add tag name and data into table
table.add_row([tag_name, data])
#display data
print(table)
Program
#load modules
from PIL import Image
from PIL.ExifTags import TAGS
from prettytable import PrettyTable
image_filename = "image.jpg"
#initialiting prettytable object
table = PrettyTable()
#setting table feilds name
table.field_names = ["MetaTags", "Values"]
#load image
my_image = Image.open(image_filename)
#get EXIF standared Data of the image
img_exif_data = my_image.getexif()
for id in img_exif_data:
tag_name = TAGS.get(id, id)
data = img_exif_data.get(id)
#if data in bytes
if isinstance(data, bytes):
data = data.decode()
table.add_row([tag_name, data])
print(table)
Output
+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| MetaTags | Values |
+----------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ExifVersion | 0220 |
| ComponentsConfiguration | |
| ShutterSpeedValue | 9.965 |
| DateTimeOriginal | 2020:08:19 10:49:13 |
| DateTimeDigitized | 2020:08:19 10:49:13 |
| ApertureValue | 1.44 |
| BrightnessValue | 5.5 |
| ExposureBiasValue | nan |
| MaxApertureValue | 1.44 |
| MeteringMode | 2 |
| LightSource | 21 |
| Flash | 16 |
| FocalLength | 4.755 |
| ColorSpace | 1 |
| ExifImageWidth | 8000 |
| SceneCaptureType | 0 |
| SubsecTime | 750682 |
| SubsecTimeOriginal | 750682 |
| SubsecTimeDigitized | 750682 |
| ExifImageHeight | 6000 |
| ImageLength | 6000 |
| Make | OnePlus |
| Model | HD1901 |
| SensingMethod | 1 |
| Orientation | 1 |
| YCbCrPositioning | 1 |
| ExposureTime | 0.001 |
| ExifInteroperabilityOffset | 791 |
| XResolution | 72.0 |
| FNumber | 1.65 |
| SceneType | |
| YResolution | 72.0 |
| ExposureProgram | 1 |
| GPSInfo | {1: 'N', 2: (29.0, 52.0, 46.6535), 3: 'E', 4: (79.0, 20.0, 54.5711), 5: b'\x00', 6: 0.0, 7: (5.0, 19.0, 13.0), 27: b'ASCII\x00\x00\x00CELLID\x00', 29: '2020:08:19'} |
| ISOSpeedRatings | 200 |
| ResolutionUnit | 2 |
| ExposureMode | 1 |
| FlashPixVersion | 0100 |
| ImageWidth | 8000 |
| WhiteBalance | 0 |
| DateTime | 2020:08:19 10:49:13 |
| FocalLengthIn35mmFilm | 27 |
| ExifOffset | 210 |
| MakerNote | MM * |
+----------------------------+------------------------------------------------------------------------------------------------------------------------------------------
Conclusion
In this Python instructional exercise, you figured out how to extricate picture metadata in Python. Metadata contains all the data-dependent on the EXIF Standards. We would propose you utilize a picture caught utilizing a cell phone when you are removing the EXIF metadata data.