forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_metadata_extractor.py
38 lines (31 loc) · 966 Bytes
/
image_metadata_extractor.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
from PIL import Image
from PIL.ExifTags import TAGS
import sys
# path to the image or video
imagename = sys.argv[1]
# read the image data using PIL
image = Image.open(imagename)
# extract other basic metadata
info_dict = {
"Filename": image.filename,
"Image Size": image.size,
"Image Height": image.height,
"Image Width": image.width,
"Image Format": image.format,
"Image Mode": image.mode,
"Image is Animated": getattr(image, "is_animated", False),
"Frames in Image": getattr(image, "n_frames", 1)
}
for label,value in info_dict.items():
print(f"{label:25}: {value}")
# extract EXIF data
exifdata = image.getexif()
# iterating over all EXIF data fields
for tag_id in exifdata:
# get the tag name, instead of human unreadable tag id
tag = TAGS.get(tag_id, tag_id)
data = exifdata.get(tag_id)
# decode bytes
if isinstance(data, bytes):
data = data.decode()
print(f"{tag:25}: {data}")