In Python, how do I read the exif data for an image?
You can use the _getexif()
protected method of a PIL Image.
import PIL.Image
img = PIL.Image.open('img.jpg')
exif_data = img._getexif()
This should give you a dictionary indexed by EXIF numeric tags. If you want the dictionary indexed by the actual EXIF tag name strings, try something like:
import PIL.ExifTags
exif = {
PIL.ExifTags.TAGS[k]: v
for k, v in img._getexif().items()
if k in PIL.ExifTags.TAGS
}
Getting exif data from all images in a directory using Python
It's easier if you write this:
if files.endswith (('jpg','JPG','png','PNG','tiff','TIFF')):
file_path = os.path.join(dircont, files)
print file_path
#Exifread magic
...
How to show all the metadata about images?
The properties that you've mentioned - focal length, metering mode and exposure time - are within the EXIF IFD - https://www.awaresystems.be/imaging/tiff/tifftags/privateifd/exif.html
Since Pillow 8.2.0, getexif()
only returns the top level tags. To get the data within the EXIF IFD, you need to use get_ifd()
.
To take some code from the other answer here and modify it,
from PIL import Image
from PIL.ExifTags import TAGS
def print_exif_data(exif_data):
for tag_id in exif_data:
tag = TAGS.get(tag_id, tag_id)
content = exif_data.get(tag_id)
print(f'{tag:25}: {content}')
with Image.open("Tests/images/flower.jpg") as im:
exif = im.getexif()
print_exif_data(exif)
print()
print_exif_data(exif.get_ifd(0x8769))
I get
ResolutionUnit : 2
ExifOffset : 196
Make : Canon
Model : Canon PowerShot S40
Orientation : 1
DateTime : 2003:12:14 12:01:44
YCbCrPositioning : 1
XResolution : 180.0
YResolution : 180.0
ExifVersion : b'0220'
ComponentsConfiguration : b'\x01\x02\x03\x00'
CompressedBitsPerPixel : 5.0
DateTimeOriginal : 2003:12:14 12:01:44
DateTimeDigitized : 2003:12:14 12:01:44
ShutterSpeedValue : 8.96875
ApertureValue : 4.65625
ExposureBiasValue : 0.0
MaxApertureValue : 2.970855712890625
MeteringMode : 2
Flash : 24
FocalLength : 21.3125
UserComment : b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
ColorSpace : 1
ExifImageWidth : 2272
FocalPlaneXResolution : 8114.285714285715
ExifImageHeight : 1704
FocalPlaneYResolution : 8114.285714285715
FocalPlaneResolutionUnit : 2
SensingMethod : 2
FileSource : b'\x03'
ExposureTime : 0.002
ExifInteroperabilityOffset: 1416
FNumber : 4.9
CustomRendered : 0
ExposureMode : 0
FlashPixVersion : b'0100'
WhiteBalance : 0
DigitalZoomRatio : 1.0
MakerNote : b'\x0c\x00\x01\x00\x03\x00(\x00\x00\x00D\x04\x00\x00\x02\x00\x03\x00\x04\x00\x00\x00\x94\x04\x00\x00\x03\x00\x03\x00\x04\x00\x00\x00\x9c\x04\x00\x00\x04\x00\x03\x00\x1b\x00\x00\x00\xa4\x04\x00\x00\x00\x00\x03\x00\x06\x00\x00\x00\xda\x04\x00\x00\x00\x00\x03\x00\x04\x00\x00\x00\xe6\x04\x00\x00\x06\x00\x02\x00 \x00\x00\x00\xee\x04\x00\x00\x07\x00\x02\x00\x18\x00\x00\x00\x0e\x05\x00\x00\x08\x00\x04\x00\x01\x00\x00\x00;\xe1\x11\x00\t\x00\x02\x00 \x00\x00\x00&\x05\x00\x00\x10\x00\x04\x00\x01\x00\x00\x00\x00\x00\x11\x01\r\x00\x03\x00\x15\x00\x00\x00F\x05\x00\x00\x00\x00\x00\x00P\x00\x02\x00\x00\x00\x05\x00\x01\x00\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x00\x05\x00\x01\x00\x030\x01\x00\xff\xff\xff\xff\xaa\x02\xe3\x00 \x00\x95\x00\xc0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff1\x00\xe0\x08\xe0\x08\x00\x00\x01\x00\x02\x00\xaa\x02\x1e\x01\xd7\x00\x00\x00\x00\x00\x00\x00\x00\x006\x00\x00\x00\xa0\x00\x14\x01\x95\x00\x1f\x01\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x020\x00\x00\x00\x00\x00\x00\x01\x00\x0e\x03\x00\x00\x95\x00!\x01\x00\x00\x00\x00\x00\x00\xfa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00IMG:PowerShot S40 JPEG\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00Firmware Version 1.10\x00\x00\x00Andreas Huggel\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00*\x00\x03\x00\x01\x80z\x01\x01\x80\x00\x00\x00\x00\x00\x00\x03\x01\x02\x00\x00\x00\n\x00\x00\x00\x00\x00\x00\x009\x00\xc6\x00\x05\x00\x00\x00\x00\x00\x00\x00'
SceneCaptureType : 0
Retrieve Image description with Python (not returned in EXIF data via PIL or exifread)
Thanks to @Mark Serchells answer, I found that the description is no EXIF data but IPTC data (exiftool
shows all the fields).
In Python I now can use IPTCInfo3: https://pypi.org/project/IPTCInfo3/ and extract the description via:
info = IPTCInfo('img.jpg')
info['caption/abstract']
Which prints:
b'Gel\xe4ndewagen auf den Salzformationen im Salzsee Ass-Ale in der Danakil-Senke, Danakil-Senke, Hamed Ela, Wollo, \xc4thiopien, Afrika
How do i read EXIF data from an image without the use of external scripts in python?
You can use exif.py. If you don't want it to be an external module (it wouldn't necessarily be used as a script anyway), you can just copy all 1767 lines right inside your own module. If your objection is to "prewritten", you can study and then rewrite the 1767 lines in question, if you have a few days to waste, but I don't understand why you'd want to do that.
Related Topics
How to Access the Query String in Flask Routes
How to Check If a Process Is Still Running Using Python on Linux
Sending Messages with Telegram - APIs or Cli
Why Is a List Comprehension So Much Faster Than Appending to a List
Send File Using Post from a Python Script
Convert Pandas.Series from Dtype Object to Float, and Errors to Nans
How to Read First N Lines of a File
Removing Item from List Causes the List to Become Nonetype
What Does Preceding a String Literal with "R" Mean
Keras Not Training on Entire Dataset
Threading in a Pyqt Application: Use Qt Threads or Python Threads
What Is Different Between Makedirs and Mkdir of Os
Differencebetween Installing a Package Using Pip VS. Apt-Get
How Is the 'Is' Keyword Implemented in Python
Python Date String to Date Object
Why Is Tkinter Entry's Get Function Returning Nothing
How to Allow or Deny Notification Geo-Location Microphone Camera Pop Up