Repository navigation
Expand file tree
/
Copy pathimage_processing.py
More file actions
302 lines (254 loc) · 9.64 KB
/
Copy pathimage_processing.py
File metadata and controls
302 lines (254 loc) · 9.64 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import math
from collections import Counter
from pylab import savefig
import cv2
def grayscale():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
r = img_arr[:, :, 0]
g = img_arr[:, :, 1]
b = img_arr[:, :, 2]
new_arr = r.astype(int) + g.astype(int) + b.astype(int)
new_arr = (new_arr/3).astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def is_grey_scale(img_path):
im = Image.open(img_path).convert('RGB')
w, h = im.size
for i in range(w):
for j in range(h):
r, g, b = im.getpixel((i, j))
if r != g != b:
return False
return True
def zoomin():
img = Image.open("static/img/img_now.jpg")
img = img.convert("RGB")
img_arr = np.asarray(img)
new_size = ((img_arr.shape[0] * 2),
(img_arr.shape[1] * 2), img_arr.shape[2])
new_arr = np.full(new_size, 255)
new_arr.setflags(write=1)
r = img_arr[:, :, 0]
g = img_arr[:, :, 1]
b = img_arr[:, :, 2]
new_r = []
new_g = []
new_b = []
for row in range(len(r)):
temp_r = []
temp_g = []
temp_b = []
for i in r[row]:
temp_r.extend([i, i])
for j in g[row]:
temp_g.extend([j, j])
for k in b[row]:
temp_b.extend([k, k])
for _ in (0, 1):
new_r.append(temp_r)
new_g.append(temp_g)
new_b.append(temp_b)
for i in range(len(new_arr)):
for j in range(len(new_arr[i])):
new_arr[i, j, 0] = new_r[i][j]
new_arr[i, j, 1] = new_g[i][j]
new_arr[i, j, 2] = new_b[i][j]
new_arr = new_arr.astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def zoomout():
img = Image.open("static/img/img_now.jpg")
img = img.convert("RGB")
x, y = img.size
new_arr = Image.new("RGB", (int(x / 2), int(y / 2)))
r = [0, 0, 0, 0]
g = [0, 0, 0, 0]
b = [0, 0, 0, 0]
for i in range(0, int(x/2)):
for j in range(0, int(y/2)):
r[0], g[0], b[0] = img.getpixel((2 * i, 2 * j))
r[1], g[1], b[1] = img.getpixel((2 * i + 1, 2 * j))
r[2], g[2], b[2] = img.getpixel((2 * i, 2 * j + 1))
r[3], g[3], b[3] = img.getpixel((2 * i + 1, 2 * j + 1))
new_arr.putpixel((int(i), int(j)), (int((r[0] + r[1] + r[2] + r[3]) / 4), int(
(g[0] + g[1] + g[2] + g[3]) / 4), int((b[0] + b[1] + b[2] + b[3]) / 4)))
new_arr = np.uint8(new_arr)
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def move_left():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
r = np.pad(r, ((0, 0), (0, 50)), 'constant')[:, 50:]
g = np.pad(g, ((0, 0), (0, 50)), 'constant')[:, 50:]
b = np.pad(b, ((0, 0), (0, 50)), 'constant')[:, 50:]
new_arr = np.dstack((r, g, b))
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def move_right():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
r = np.pad(r, ((0, 0), (50, 0)), 'constant')[:, :-50]
g = np.pad(g, ((0, 0), (50, 0)), 'constant')[:, :-50]
b = np.pad(b, ((0, 0), (50, 0)), 'constant')[:, :-50]
new_arr = np.dstack((r, g, b))
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def move_up():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
r = np.pad(r, ((0, 50), (0, 0)), 'constant')[50:, :]
g = np.pad(g, ((0, 50), (0, 0)), 'constant')[50:, :]
b = np.pad(b, ((0, 50), (0, 0)), 'constant')[50:, :]
new_arr = np.dstack((r, g, b))
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def move_down():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
r, g, b = img_arr[:, :, 0], img_arr[:, :, 1], img_arr[:, :, 2]
r = np.pad(r, ((50, 0), (0, 0)), 'constant')[0:-50, :]
g = np.pad(g, ((50, 0), (0, 0)), 'constant')[0:-50, :]
b = np.pad(b, ((50, 0), (0, 0)), 'constant')[0:-50, :]
new_arr = np.dstack((r, g, b))
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def brightness_addition():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img).astype('uint16')
img_arr = img_arr+100
img_arr = np.clip(img_arr, 0, 255)
new_arr = img_arr.astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def brightness_substraction():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img).astype('int16')
img_arr = img_arr-100
img_arr = np.clip(img_arr, 0, 255)
new_arr = img_arr.astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def brightness_multiplication():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
img_arr = img_arr*1.25
img_arr = np.clip(img_arr, 0, 255)
new_arr = img_arr.astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def brightness_division():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
img_arr = img_arr/1.25
img_arr = np.clip(img_arr, 0, 255)
new_arr = img_arr.astype('uint8')
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def convolution(img, kernel):
h_img, w_img, _ = img.shape
out = np.zeros((h_img-2, w_img-2), dtype=np.float)
new_img = np.zeros((h_img-2, w_img-2, 3))
if np.array_equal((img[:, :, 1], img[:, :, 0]), img[:, :, 2]) == True:
array = img[:, :, 0]
for h in range(h_img-2):
for w in range(w_img-2):
S = np.multiply(array[h:h+3, w:w+3], kernel)
out[h, w] = np.sum(S)
out_ = np.clip(out, 0, 255)
for channel in range(3):
new_img[:, :, channel] = out_
else:
for channel in range(3):
array = img[:, :, channel]
for h in range(h_img-2):
for w in range(w_img-2):
S = np.multiply(array[h:h+3, w:w+3], kernel)
out[h, w] = np.sum(S)
out_ = np.clip(out, 0, 255)
new_img[:, :, channel] = out_
new_img = np.uint8(new_img)
return new_img
def edge_detection():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img, dtype=np.int)
kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
new_arr = convolution(img_arr, kernel)
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def blur():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img, dtype=np.int)
kernel = np.array(
[[0.0625, 0.125, 0.0625], [0.125, 0.25, 0.125], [0.0625, 0.125, 0.0625]])
new_arr = convolution(img_arr, kernel)
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def sharpening():
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img, dtype=np.int)
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
new_arr = convolution(img_arr, kernel)
new_img = Image.fromarray(new_arr)
new_img.save("static/img/img_now.jpg")
def histogram_rgb():
img_path = "static/img/img_now.jpg"
img = Image.open(img_path)
img_arr = np.asarray(img)
if is_grey_scale(img_path):
g = img_arr[:, :, 0].flatten()
data_g = Counter(g)
plt.bar(list(data_g.keys()), data_g.values(), color='black')
plt.savefig(f'static/img/grey_histogram.jpg', dpi=300)
plt.clf()
else:
r = img_arr[:, :, 0].flatten()
g = img_arr[:, :, 1].flatten()
b = img_arr[:, :, 2].flatten()
data_r = Counter(r)
data_g = Counter(g)
data_b = Counter(b)
data_rgb = [data_r, data_g, data_b]
warna = ['red', 'green', 'blue']
data_hist = list(zip(warna, data_rgb))
for data in data_hist:
plt.bar(list(data[1].keys()), data[1].values(), color=f'{data[0]}')
plt.savefig(f'static/img/{data[0]}_histogram.jpg', dpi=300)
plt.clf()
def df(img): # to make a histogram (count distribution frequency)
values = [0]*256
for i in range(img.shape[0]):
for j in range(img.shape[1]):
values[img[i, j]] += 1
return values
def cdf(hist): # cumulative distribution frequency
cdf = [0] * len(hist) # len(hist) is 256
cdf[0] = hist[0]
for i in range(1, len(hist)):
cdf[i] = cdf[i-1]+hist[i]
# Now we normalize the histogram
# What your function h was doing before
cdf = [ele*255/cdf[-1] for ele in cdf]
return cdf
def histogram_equalizer():
img = cv2.imread('static\img\img_now.jpg', 0)
my_cdf = cdf(df(img))
# use linear interpolation of cdf to find new pixel values. Scipy alternative exists
image_equalized = np.interp(img, range(0, 256), my_cdf)
cv2.imwrite('static/img/img_now.jpg', image_equalized)
def threshold(lower_thres, upper_thres):
img = Image.open("static/img/img_now.jpg")
img_arr = np.asarray(img)
condition = np.logical_and(np.greater_equal(img_arr, lower_thres),
np.less_equal(img_arr, upper_thres))
print(lower_thres, upper_thres)
img_arr.setflags(write=1)
img_arr[condition] = 255
new_img = Image.fromarray(img_arr)
new_img.save("static/img/img_now.jpg")