-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblack_white_matrix.py
41 lines (25 loc) · 956 Bytes
/
black_white_matrix.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
def bw_pixels_to_matrix():
'Returns matrix with 0s for white and 1s for black pixels.'
from PIL import Image
from rotate_matrix import rotate90Clockwise
im = Image.open(r'C:\Users\jmsie\Dev\Projects\normalized.png')
# coordinates (x, y)
counter = 0
matrix = []
row = []
black = 1
# loops through pixels in image and creates matrix of black(1) and white(0) values for pixel
for xi in range(im.size[0]):
for yi in range(im.size[1]):
counter += 1
if counter == im.size[0]:
counter = 0
matrix.append(row)
row = []
pixel = im.getpixel((yi, xi))
if pixel[:3] == (255, 255, 255):
row.append(0)
elif pixel[:3] == (0, 0, 0):
row.append(1)
matrix[0].append(0)
return matrix