-
Notifications
You must be signed in to change notification settings - Fork 0
/
pngTools.py
113 lines (80 loc) · 2.58 KB
/
pngTools.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from matplotlib import pyplot
import os
import math
def read(filename):
""" Reads an image with pyplot """
img = pyplot.imread(filename)
return img
def save(img):
""" Saves an image to the output folder. Call BEFORE show """
filename = createFilename()
pyplot.imsave(filename, img)
print('Saved as ' + filename)
def show(img):
""" Shows an image with pyplot """
pyplot.imshow(img)
pyplot.show()
def divider():
""" Prints a divider """
print('-----------------------------')
def colour2bw(img):
for row_index in range(0, len(img)):
for pixel_index, pixel in enumerate(img[row_index]):
for rgb_index, rgbval in enumerate(pixel):
img[row_index][pixel_index][rgb_index] = round(rgbval)
return img
def colour2gray(img):
return grayVector2rgbImage(rgbImage2grayVector(img))
def rgbImage2grayVector(img):
""" Turns a row and column rgb image into a 1D grayscale vector """
gray = []
for row_index in range(0, len(img)):
for pixel_index, pixel in enumerate(img[row_index]):
gray.append(rgbPixel2grayscaleValue(pixel))
return gray
def grayVector2rgbImage(gray):
""" Turns a 1D grayscale vector into an rgb image. Only works for square images """
img = []
# get sidelength
length = math.sqrt(len(gray))
gray_index = 0
for row_index in range(0, int(length)):
img.append([])
for column_index in range (0, int(length)):
value = gray[gray_index]
gray_index += 1
img[row_index].append([value, value, value])
return img
def rgbPixel2grayscaleValue(rgb):
r, g, b = rgb[0], rgb[1], rgb[2]
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
return gray
def createFilename():
"""
Returns an unused name for an output
"""
for i in range(0, 1000):
if (i<10):
filename = 'output/output_00' + str(i) + '.png'
elif (i<100):
filename = 'output/output_0' + str(i) + '.png'
else:
filename = 'output/output_' + str(i) + '.png'
if (not os.path.exists(filename)):
return filename
# file = open(filename, 'w+')
# file.close()
# filename = 'Resources/homer.png'
# filename = 'Resources/homer.png'
# # get colour image
# img = read(filename)
# # turn to gray vector
# grayVector = rgbImage2grayVector(img)
# img2 = grayVector2rgbImage(grayVector)
# show(img)
# show(img2)
# img = read(filename)
# gray = rgbImage2grayVector(img)
# # print(gray)
# save(img)
# # show(img)