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
}
Exif reading library
Option 1. Use pyexiv2. See: pyexiv2 Bug #824440: Python 3 support You need boost-python for py3k and also to manually apply the patch posted at the end of the bug above, but aside from that it works. Probably easiest to get up and running under latest Ubuntu.
Option 2. Use PIL Downside: this branch/fork doesn't seem to be actively developed.
from PIL import Image
from PIL.ExifTags import TAGS
image = Image.open("test.jpg")
exif = image._getexif()
# decode exif using TAGS
Option 3. Use PythonMagick
from PythonMagick import Image
img = Image("image.jpg")
print img.attribute("EXIF:Orientation")
See also: Exif manipulation library for python
Python: Remove Exif info from images
You can try loading the image with the Python Image Lirbary (PIL) and then save it again to a different file. That should remove the meta data.
Reading long exif tags in python
Here is the hilariously slow and inefficient method of calling exiftool on the command line using subprocess.check_output. Not my finest hour, but it works:
import matplotlib.pyplot as plt
import subprocess, glob, re
def get_magnification(filename):
p = subprocess.check_output('exiftool -tab %s'%filename,shell=True)
xpix = float(re.findall('XpixCal=\d*.\d*',p)[0][8:])
ypix = float(re.findall('YpixCal=\d*.\d*',p)[0][8:])
mag = int(re.findall('p.\d+',p)[0][2:])
return xpix,ypix,mag
xpix,ypix,mag = get_magnification('E3-9.tif')
print 'X pixels per nm: %.3f'%(xpix)
print 'Y pixels per nm: %.3f'%(ypix)
print 'Magnification: %ix'%(mag)
How to modify EXIF data in python
import piexif
from PIL import Image
img = Image.open(fname)
exif_dict = piexif.load(img.info['exif'])
altitude = exif_dict['GPS'][piexif.GPSIFD.GPSAltitude]
print(altitude)
(550, 1) % some values are saved in a fractional format. This means 550m, (51, 2) would be 25,5m.
exif_dict['GPS'][piexif.GPSIFD.GPSAltitude] = (140, 1)
This sets the altitude to 140m
exif_bytes = piexif.dump(exif_dict)
img.save('_%s' % fname, "jpeg", exif=exif_bytes)
Related Topics
Tkinter Vanishing Photoimage Issue
Why Does This Not Work as an Array Membership Test
Differencebetween .Quit and .Quit in Pygame
How to Get the Row Count of a Pandas Dataframe
How to Count the Nan Values in a Column in Pandas Dataframe
How to Break a Long Line to Multiple Lines in Python
A Tool to Convert Matlab Code to Python
How Does Multiplication Differ for Numpy Matrix VS Array Classes
Using Requests with Tls Doesn't Give Sni Support
Download Image with Selenium Python
Check If a File Is Open in Python
Multiple Inputs and Outputs in Python Subprocess Communicate
Different Ways of Clearing Lists