-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimg_meta.py
69 lines (46 loc) · 1.8 KB
/
img_meta.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
58
59
60
61
62
63
64
65
66
67
68
69
import argparse
import os
from PIL import Image
from PIL.ExifTags import TAGS
import PIL
def img_data(files):
for file in files:
print(f"------------------{file} META DATA------------------")
if os.path.isfile(file):
try:
# Read the image
image = Image.open(file)
# Extract EXIF data
exif_data = image.getexif()
# Iterating over EXIF data values
for tag_id in exif_data:
# get the tag name
tag = TAGS.get(tag_id, tag_id)
data = exif_data.get(tag_id)
# Try, except block
try:
# Check if instance is bytes
if isinstance(data, bytes):
# Decode bytes
data = data.decode()
print(f"{tag:25}: {data}")
except UnicodeDecodeError:
pass
print(f"----------------------------------------------------------")
except PIL.UnidentifiedImageError:
print(f"Cannot Identify Image: {file}")
print(f"----------------------------------------------------------")
else:
print(f"No File Found: {file}")
print(f"----------------------------------------------------------")
def main():
parser = argparse.ArgumentParser(description='EXTRACT METADATA IN IMAGES')
# Argument is for images files
parser.add_argument('-f', dest='files', nargs='+', type=str, help='Image File(s)', required=True)
args = parser.parse_args()
# Get the files
files = args.files
if files:
img_data(files)
if __name__ == '__main__':
main()