-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (65 loc) · 1.66 KB
/
main.py
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
from PIL import Image
from time import process_time
from os import path
inp = input('''Type "exit" to exit the program.
File path: ''')
if not inp:
inp = "upisidedown-'bar'.png"
elif inp.lower() == 'exit':
exit()
start = process_time()
im = Image.open(inp).convert('RGB')
img = im.load()
crop_pixels = []
crop_colors = ((255, 255, 255), (0, 0, 0))
xsize, ysize = im.size
top, bottom = 0, ysize
left, right = 0, xsize
lisx = list(range(xsize))
lisy = list(range(ysize))
y = 0
for _ in range(ysize):
crop_pixels.append([])
while y < ysize:
for x in range(xsize):
if img[x, y] in crop_colors:
crop_pixels[y].append(x)
else:
crop_pixels[y].clear()
if y < ysize//2:
y = ysize//2
break
y += 1
for y in range(ysize):
if y <= ysize//2:
if crop_pixels[y]:
top = y
elif y < bottom and crop_pixels[y]:
bottom = y
break
crop_pixels = []
for _ in range(xsize):
crop_pixels.append([])
x = 0
while x < xsize:
for y in range(ysize):
if img[x, y] in crop_colors:
crop_pixels[x].append(y)
else:
crop_pixels[x].clear()
if x < xsize//2:
x = xsize//2
break
x += 1
for x in range(xsize):
if x <= xsize//2:
if crop_pixels[x]:
left = x
elif x < right and crop_pixels[x]:
right = x
end = process_time()
print('Processing took:', round(end - start, 2), 'seconds', end='\n\n')
im = im.crop((left + 1, top + 1, right, bottom))
im.show()
if input('Want to save image? [Y/n] ').lower() in ('', 'y', 'yes'):
im.save("cropped.png")