-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
78 lines (61 loc) · 1.78 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
__author__ = 'Marek'
import sys
from PIL import Image
from PIL import ImageOps
import matplotlib.pyplot as plt
from numpy import sqrt
image = Image.open("im.png", mode="r")
def histogram():
pix = list(image.getdata())
r = []
g = []
b = []
x = [0] * 768
for p in pix:
r.append(p[0])
g.append(p[1])
b.append(p[2])
for i in range(len(r)):
x[r[i]] += 1
x[g[i] + 256] += 1
x[b[i] + 512] += 1
plt.bar(range(len(x)), x)
plt.show()
def grey():
im_g = image.convert('L')
im_g.show()
im_g.save('grey.png', 'PNG')
def roberts():
out_img = Image.new('L', image.size, None)
img_data = image.load()
out_data = out_img.load()
matrix_x = [(-1, 0, 1), (-1, 0, 1), (-1, 0, 1)]
matrix_y = [(-1, -1, -1), (0, 0, 0), (1, 1, 1)]
matrix_size = len(matrix_x)
matrix_middle = matrix_size/2
rows, cols = image.size
for row in range(rows - matrix_size):
for col in range(cols - matrix_size):
pixel_x = 0
pixel_y = 0
for i in range(matrix_size):
for j in range(matrix_size):
val = sum(img_data[row+i, col+j])/3
pixel_x += matrix_x[i][j] * val
pixel_y += matrix_y[i][j] * val
new_pixel = sqrt(pixel_x**2 + pixel_y**2)
new_pixel = int(new_pixel)
out_data[row + matrix_middle, col + matrix_middle] = new_pixel
out_img = ImageOps.invert(out_img)
out_img.show('Roberts')
out_img.save('roberts.png', 'PNG')
def main():
ask = int(input("1. Histogram\n2. Greyscale\n3. Roberts\n"))
if ask == 1:
histogram()
elif ask == 2:
grey()
elif ask == 3:
roberts()
if __name__ == "__main__":
sys.exit(main())