Skip to content

Commit 8035d80

Browse files
try to solve 18
1 parent b15078e commit 8035d80

File tree

3 files changed

+4155
-60
lines changed

3 files changed

+4155
-60
lines changed

18.py

+8-60
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,9 @@
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-
481
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()
2+
file1 = open("swan/left.patch", "r")
3+
file2 = open("swan/liubei.txt", "w")
4+
while True:
5+
line = file1.readline()
6+
if not line:
7+
break
8+
if line.startswith("+") or line.startswith("-"):
9+
file2.write(line)

0 commit comments

Comments
 (0)