-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.py
41 lines (33 loc) · 888 Bytes
/
metadata.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
import sys
import pikepdf
from PIL import Image
from PIL.ExifTags import TAGS
def meta_pdf(pdf_file):
pdf = pikepdf.Pdf.open(pdf_file)
return dict(pdf.docinfo)
def image_meta(image):
img = Image.open(image)
info = {
"Image name": img.filename,
"Image size": img.size,
"Image height": img.height,
"Image width": img.width,
"Image format": img.format,
"Image mode": img.mode
}
exif = img.getexif()
for id_tag in exif:
tag = TAGS.get(id_tag, id_tag)
data = exif.get(id_tag)
if isinstance(data, bytes):
data = data.decode()
info[tag] = data
return info
if __name__ == "__main__":
file = sys.argv[1]
if file.endswith(".pdf"):
print(meta_pdf(file))
elif file.endswith(".jpg"):
print(image_meta(file))
else:
print("ERROR")