Skip to content

Commit 3e00e87

Browse files
try to solve 18
1 parent 2bbacc2 commit 3e00e87

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

18.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from PIL import Image
2+
from PIL import ImageDraw
3+
import logging
4+
5+
6+
def load_a_picture(path):
7+
im = Image.open(path)
8+
size = im.size
9+
logging.info("size is {}, mode is: {}".format(size, im.mode))
10+
return im
11+
12+
13+
def get_pixel_array(im, left_x, right_x, up_y, down_y):
14+
ary = []
15+
for x in range(left_x, right_x):
16+
for y in range(up_y, down_y):
17+
ary.append(im.getpixel((x, y)))
18+
return ary
19+
20+
21+
def create_new_ary(ary1, ary2):
22+
ary = []
23+
for i in range(len(ary1)):
24+
r1, g1, b1 = ary1[i]
25+
r2, g2, b2 = ary2[i]
26+
r, g, b = r1 - r2, g1 - g2, b1 - b2
27+
t = (r, g ,b)
28+
ary.append(t)
29+
return ary
30+
31+
32+
def analysis_ary(rgbs):
33+
for i in range(len(rgbs)):
34+
print(rgbs[i])
35+
36+
37+
def create_new_picture(pixels, width, height):
38+
result = Image.new(mode="RGB", size=(width, height))
39+
drawer = ImageDraw.Draw(result)
40+
count = 0
41+
for x in range(width):
42+
for y in range(height):
43+
drawer.point((x, y), fill = pixels[count])
44+
count += 1
45+
return result
46+
47+
48+
if __name__ == "__main__":
49+
logging.basicConfig(level=logging.DEBUG)
50+
im = load_a_picture("swan/balloons.jpg")
51+
size = im.size
52+
width = size[0]
53+
height = size[1]
54+
ary1 = get_pixel_array(im, 0, int(width / 2), 0, height)
55+
ary2 = get_pixel_array(im, int(width / 2), width, 0 , height)
56+
logging.info("len(ary1) is: {}".format(len(ary1)))
57+
logging.info("len(ary2) is: {}".format(len(ary2)))
58+
ary = create_new_ary(ary1, ary2)
59+
analysis_ary(ary)
60+
result = create_new_picture(ary, int(width / 2), height)
61+
result.show()

swan/balloons.jpg

55.7 KB
Loading

0 commit comments

Comments
 (0)