This repository was archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmapper.py
More file actions
79 lines (73 loc) · 2.71 KB
/
Copy pathmapper.py
File metadata and controls
79 lines (73 loc) · 2.71 KB
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
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
from PIL import Image, ImageChops, ImageEnhance, ExifTags
import sys, os, os.path
import json
import uuid
import base64
from subprocess import call
import cStringIO as sio
reload(sys)
sys.setdefaultencoding('utf8')
# input comes from STDIN
for line in sys.stdin:
# remove leading and trailing whitespace
line = line.strip()
if line:
img_dict = json.loads(line)
img_name = img_dict.get('name')
img_data = img_dict.get('content')
if img_name and img_data:
del img_dict['content']
data = base64.b64decode(img_data)
filename = '/tmp/%s' % str(uuid.uuid4())
im = None
'''
f = open(filename, 'wa')
f.write(data)
f.flush()
f.close()
im = Image.open(filename)
'''
stream = sio.StringIO(data)
im = Image.open(stream)
edited = False
try:
exif_data = im._getexif()
if exif_data:
for k, v in exif_data.items():
if k in ExifTags.TAGS:
if "MakerNote" != ExifTags.TAGS.get(k):
if v and ("photoshop" in str(v).lower() or "microsoft" in str(v).lower()):
img_dict['edited'] = 'yes'
img_dict['reason'] = 'software'
img_dict['software'] = v
edited = True
break
if not edited:
bands = im.getbands()
if bands and bands != ('R', 'G', 'B'):
img_dict['edited'] = 'yes'
img_dict['reason'] = 'not_rgb'
edited = True
'''
if not edited:
im_info = im.info
if im_info and ("jfif" in im_info or "jfif_unit" in im_info):
img_dict['edited'] = 'yes'
img_dict['reason'] = 'jfif'
edited = True
'''
except:
pass
#if not edited:
resaved = filename + '.resaved.jpg'
im.save(resaved, 'JPEG', quality=95)
resaved_im = Image.open(resaved)
ela_im = ImageChops.difference(im, resaved_im)
extrema = ela_im.getextrema()
max_diff = max([ex[1] for ex in extrema])
img_dict['edited'] = 'notsure'
img_dict['ela'] = max_diff
if os.path.exists(resaved):
os.remove(resaved)
print json.dumps(img_dict)