-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpictdiff.py
More file actions
executable file
·76 lines (61 loc) · 1.95 KB
/
pictdiff.py
File metadata and controls
executable file
·76 lines (61 loc) · 1.95 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
#!/usr/bin/env python3
MINUTE = 5 # less than "x" points of difference
INCREASE_MINUTE = 2 # boost of minute differences
import sys
from PIL import Image
if len(sys.argv) < 4:
print("Usage: %s <picture A> <picture B> <diff map>" % sys.argv[0], file=sys.stderr)
sys.exit(2)
try:
img1 = Image.open(sys.argv[1]).convert('RGBA')
except FileNotFoundError:
print("First file could not be opened or it is not a picture.", file=sys.stderr)
sys.exit(2)
try:
img2 = Image.open(sys.argv[2]).convert('RGBA')
except FileNotFoundError:
print("Second file could not be opened or it is not a picture.", file=sys.stderr)
sys.exit(2)
i1 = img1.load()
i2 = img2.load()
if img1.size != img2.size:
print("Pictures %s and %s have different sizes, cannot compare" \
% (sys.argv[1], sys.argv[2]), file=sys.stderr)
sys.exit(1)
imgmap = Image.new('RGB', img1.size, "white")
imap = imgmap.load()
totaldiff = 0
# Premultiply alpha, so color differences are mitigated by transparency
# Difference in alpha itself is accumulated separately in absdiff, so this
# will not mask any differenct
def pre_mult_alpha(old, new, channel):
# simulate a black background
old = old[channel] * (old[3] / 255.0)
new = new[channel] * (new[3] / 255.0)
return int(new) - int(old)
for y in range(img1.size[1]):
for x in range(img1.size[0]):
p1 = i1[x, y]
p2 = i2[x, y]
diffpixel = [255, 255, 255]
absdiff = abs(p2[3] - p1[3])
diffs = [0, 0, 0]
totplus = 0
for i in range(0, 3):
diffs[i] = pre_mult_alpha(p1, p2, i)
absdiff += abs(diffs[i])
diffpixel[i] += diffs[i]
totplus += max(0, diffs[i])
totaldiff += absdiff
for i in range(0, 3):
diffpixel[i] -= totplus
if absdiff > 0 and absdiff < MINUTE:
# Increase contrast of minute differences
diffpixel[i] -= INCREASE_MINUTE
diffpixel[i] = max(0, diffpixel[i])
imap[x, y] = tuple(diffpixel)
try:
imgmap.save(sys.argv[3])
except PermissionError:
print("Could not write to diff map.", file=sys.stderr)
print(totaldiff)