forked from james-see/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exif-reader.py
57 lines (53 loc) · 1.58 KB
/
exif-reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from PIL import Image, ExifTags
import sys
try:
import exifread
except:
exit('install exifread via pip to have this work properly')
# pip install iptcinfo
try:
import iptcinfo
except:
exit('install iptcinfo via pip to have this work properly')
try:
import py3exiv2
except:
exit('install py3exiv2 via pip to have this work properly')
# Open image file for reading (binary mode)
path_name = 'assets/cat.jpg'
try:
f = sys.argv[1] # check to see if image in command line
f = open(f, 'rb')
except:
try:
f = open(path_name, 'rb')
except:
exit('failed to open file, check path settings or put full path and image')
# Return Exif tags
tags = exifread.process_file(f)
totaltags = len(tags)
print ('-------EXIF DATA FOUND-------')
print ('Total EXIF tags found: %s' % (totaltags,))
for tag in tags.keys():
print ("Key: %s, value %s" % (tag, tags[tag]))
print ('-----------------END EXIF DATA-------')
im = Image.open(sys.argv[1])
try:
iptc = iptcinfo.IPTCInfo(sys.argv[1])
image_title = iptc.data.get('object name', '') or iptc.data.get('headline', '')
image_description = iptc.data.get('caption/abstract', '')
image_tags = iptc.keywords
print (image_description)
print (image_tags)
print(image_title)
except Exception as e:
if str(e) != "No IPTC data found.":
raise
print ('--------START OF PY3exiv2 DATA----------')
data = py3exiv2.metadata.ImageMetadata(sys.argv[1])
data.read()
for key in data.exif_keys:
tag = data[key]
print(' %-40s%s' %(key, tag.value))
print ('-------END PY3exiv2 DATA-----------')
sys.exit()