How to extract metadata from a image using python? How to extract metadata from a image using python? python python

How to extract metadata from a image using python?


Use Pillow, it's a fork of PIL that is still in active development, and supports python3. Here I use a dict generator to map the exif data to a dict

from PIL import Image, ExifTagsimg = Image.open("/path/to/file.jpg")exif = { ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS }


There is couple of ways by which you can get the data from the file.

1. using Exif tool :

for Windows and mac: it is available atExifToolfor linux :You can install ExifTool on Ubuntu using the apt utilitysudo apt install libimage-exiftool-perl

You can now run ExifTool anywhere in your terminal by typing exiftool.This is recommended approach to get the meta data as it gives more tags than any other way. It is a command line tool and to use it in Python user have to create a subprocess and pass the tool and image file path as an argument. Please refer the below code snippet to get the meta data using exif tool. (

infoDict = {} #Creating the dict to get the metadata tagsexifToolPath = 'D:/ExifTool/exifTool.exe' #for Windows user have to specify the Exif tool exe path for metadata extraction. #For mac and linux user it is just  """exifToolPath = exiftool"""imgPath = 'D:/Images/12.jpg'''' use Exif tool to get the metadata '''process = subprocess.Popen([exifToolPath,imgPath],stdout=subprocess.PIPE, stderr=subprocess.STDOUT,universal_newlines=True) ''' get the tags in dict '''for tag in process.stdout:    line = tag.strip().split(':')    infoDict[line[0].strip()] = line[-1].strip()for k,v in infoDict.items():    print(k,':', v)

Metadata Result for below image:

enter image description here

enter image description here

Full Tag list is here:

""" ExifTool Version Number : 11.63File Name : imgMeta.jpgDirectory : /Projects/ImageMetaData/ImagesFile Size : 32 kBFile Modification Date/Time : 30File Access Date/Time : 30File Creation Date/Time : 30File Permissions : rw-rw-rw-File Type : JPEGFile Type Extension : jpgMIME Type : image/jpegJFIF Version : 1.01Exif Byte Order : Little-endian (Intel, II)Make : CanonCamera Model Name : Canon PowerShot S40Orientation : Horizontal (normal)X Resolution : 180Y Resolution : 180Resolution Unit : inchesModify Date : 44Y Cb Cr Positioning : CenteredExposure Time : 1/500F Number : 4.9Exif Version : 0220Date/Time Original : 44Create Date : 44Components Configuration : Y, Cb, Cr, -Compressed Bits Per Pixel : 5Shutter Speed Value : 1/501Aperture Value : 5.0Max Aperture Value : 2.8Flash : Auto, Did not fireFocal Length : 103.7 mm)Macro Mode : NormalSelf Timer : OffQuality : SuperfineCanon Flash Mode : AutoContinuous Drive : SingleFocus Mode : SingleRecord Mode : JPEGCanon Image Size : LargeEasy Mode : ManualDigital Zoom : NoneContrast : NormalSaturation : NormalSharpness : 0Camera ISO : 100Metering Mode : Center-weighted averageFocus Range : AutoAF Point : CenterCanon Exposure Mode : Program AELens Type : n/aMax Focal Length : 21.3125 mmMin Focal Length : 7.09375 mmFocal Units : 32/mmMax Aperture : 5Min Aperture : 8Flash Activity : 0Flash Bits : (none)Focus Continuous : SingleAE Setting : Normal AEDisplay Aperture : 4.9Zoom Source Width : 2272Zoom Target Width : 2272Spot Metering Mode : AF PointFocal Type : ZoomFocal Plane X Size : 7.26 mmFocal Plane Y Size : 5.46 mmAuto ISO : 100Base ISO : 100Measured EV : 13.63Target Aperture : 5Target Exposure Time : 1/501Exposure Compensation : 0White Balance : AutoSlow Shutter : OffShot Number In Continuous Burst : 0Optical Zoom Code : 6Flash Guide Number : 0AF Points In Focus : CenterFlash Exposure Compensation : 0Auto Exposure Bracketing : OffAEB Bracket Value : 0Control Mode : Camera Local ControlFocus Distance Upper : 7.82 mFocus Distance Lower : 0 mBulb Duration : 0Camera Type : CompactCanon Image Type : PowerShot S40 JPEGCanon Firmware Version : Firmware Version 1.10File Number : 117-1771Owner Name : Andreas HuggelCanon Model ID : PowerShot S40User Comment : Flashpix Version : 0100Color Space : sRGBExif Image Width : 2272Exif Image Height : 1704Interoperability Index : R98 - DCF basic file (sRGB)Interoperability Version : 0100Related Image Width : 2272Related Image Height : 1704Focal Plane X Resolution : 8114.285714Focal Plane Y Resolution : 8114.285714Focal Plane Resolution Unit : inchesSensing Method : One-chip color areaFile Source : Digital CameraCustom Rendered : NormalExposure Mode : AutoDigital Zoom Ratio : 1Scene Capture Type : StandardCompression : JPEG (old-style)Thumbnail Offset : 2066Thumbnail Length : 5448Image Width : 480Image Height : 360Encoding Process : Baseline DCT, Huffman codingBits Per Sample : 8Color Components : 3Y Cb Cr Sub Sampling : 0 (2 2)Drive Mode : Single-frame ShootingISO : 100Lens : 34.5 - 103.7 mm)Shooting Mode : Program AEAperture : 4.9Image Size : 480x360Lens ID : Unknown 7-21mmMegapixels : 0.173Scale Factor To 35 mm Equivalent : 4.9Shutter Speed : 1/500Thumbnail Image : (Binary data 5448 bytes, use -b option to extract)Circle Of Confusion : 0.006 mmDepth Of Field : 2.17 m (3.11 - 5.28 m)Field Of View : 19.7 degHyperfocal Distance : 15.02 mLight Value : 13.6 """

2. using PIL package :

Refer any of the other post for this method. However the drawback of using this method is it doesn't works with all the images.

3. using hachoir package :Similar to exif tool, it is also a command line tool except that it's a python package
and user can install it using pip install hachoir. It can give metadata for most of the file formats but gives less information than Exif tool.

imgPath = 'D:/Images/12.jpg'exeProcess = "hachoir-metadata"process = subprocess.Popen([exeProcess,imgPath],                                           stdout=subprocess.PIPE, stderr=subprocess.STDOUT,                                           universal_newlines=True)for tag in process.stdout:        line = tag.strip().split(':')        infoDict[line[0].strip()] = line[-1].strip()for k,v in infoDict.items():    print(k,':', v)"""for same image : only 31 tags are identified by hachoir Method however Exif method is giving 131 tags """

enter image description here

Full tag list extracted by hachoir is below :

""" Metadata : Image width : 480 pixelsImage height : 360 pixelsImage orientation : Horizontal (normal)Bits/pixel : 24Pixel format : YCbCrImage DPI width : 72 DPIImage DPI height : 72 DPICreation date : 44Camera aperture : 2.97Camera focal : 4.9Camera exposure : 1/500Camera model : Canon PowerShot S40Camera manufacturer : CanonCompression : JPEG (Baseline)Thumbnail size : 5448 bytesEXIF version : 0220Date-time original : 44Date-time digitized : 44Compressed bits per pixel : 5Shutter speed : 8.97Aperture : 4.66Exposure bias : 0Focal length : 21.3Flashpix version : 0100Focal plane width : 8.11e+03Focal plane height : 8.11e+03Comment : 75%Format version : JFIF 1.01MIME type : image/jpegEndianness : Big endian"""


You can use following python code for this.

#!/bin/pythonimport osimport sysfrom PIL import Imagefrom PIL.ExifTags import TAGSimage = sys.argv[1]for (tag,value) in Image.open(image)._getexif().iteritems():        print '%s = %s' % (TAGS.get(tag), value)

Here is the sample output.enter image description here