-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathl18_image_diffmethods.py
More file actions
91 lines (71 loc) · 2 KB
/
l18_image_diffmethods.py
File metadata and controls
91 lines (71 loc) · 2 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
80
81
82
83
84
85
86
87
88
89
90
import PIL
from PIL import Image
fn = "C:/balloons.jpg"
im = Image.open(fn)
width, height = im.size
imData = list(im.getdata())
lleft = []
lright = []
for y in range(height):
lleft.extend(imData[y*width : y*width + width/2])
lright.extend(imData[y*width + width/2 : y*width + width])
def tup_add(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1+r2, g1+g2, b1+b2)
def tup_sub_l(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1-r2, g1-g2, b1-b2)
def tup_sub_r(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r2-r1, g2-g1, b2-b1)
def tup_sub_abs(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (abs(r1-r2), abs(g1-g2), abs(b1-b2))
def tup_xor(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1^r2, g1^g2, b1^b2)
def tup_and(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1&r2, g1&g2, b1&b2)
def tup_or(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1|r2, g1|g2, b1|b2)
def tup_avg(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return ((r1+r2)/2, (g1+g2)/2, (b1+b2)/2)
def tup_sub_l_red(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (r1-r2,0,0)
def tup_sub_l_green(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (0,g1-g2,0)
def tup_sub_l_blue(p1, p2):
r1,g1,b1 = p1
r2,g2,b2 = p2
return (0,0,b1-b2)
funcs = [tup_add, tup_sub_l, tup_sub_r, tup_sub_abs, tup_xor, \
tup_and, tup_or, tup_avg, \
tup_sub_l_red, tup_sub_l_green, tup_sub_l_blue]
nrTypes = len(funcs)
newsize = (width/2, height)
images = [Image.new("RGB", newsize) for i in range(nrTypes)]
lists = [[0] * len(lleft) for i in range(nrTypes)]
## walk through pixels of left and right images
for i, p in enumerate(zip(lleft, lright)):
## for each pixel : perform each operation (add, sub, xor, ...)
## and store in corresponding result list
for f, currlist in zip(funcs, lists):
currlist[i] = f(p[0], p[1])
for i in range(len(images)):
images[i].putdata(lists[i])
images[i].save("C:/im_" + funcs[i].func_name + ".jpg")