-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL8_18_image_filters.py
406 lines (324 loc) · 12.2 KB
/
L8_18_image_filters.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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
from Cimpl import *
# L8-18-P2-RED.py
def red_channel(image: Image) -> Image:
"""Author: Teshwar Tarachand
Returns a red channel copy of a given image. In other words, this
function will filter out the blues and greens of the image.
>>>image = load_image(choose_file())
>>>red_channel(image)
"""
red_image = copy(image)
for x, y, (r, g, b) in image:
rouge = create_color(r, g == 0, b == 0)
set_color(red_image, x, y, rouge)
show(red_image)
return(red_image)
# L8-18-P2-GREEN.py
def green_channel(image: Image) -> Image:
"""Author: Sebastien Grondin
Returns a green channel copy of a given image. In other words, this
function will filter out the blues and reds of the image.
>>>image = load_image(choose_file())
>>>green_channel(image)
"""
green_image = copy(image)
for x, y, (r, g, b) in image:
vert = create_color(r == 0, g, b == 0)
set_color(green_image, x, y, vert)
show(green_image)
return(green_image)
# L8-18-P2-BLUE.py
def blue_channel(image: Image) -> Image:
"""Author: Dylan Taylor
Returns a blue channel copy of a given image. In other words, this
function will filter out the green and reds of the image.
>>>image = load_image(choose_file())
>>>blue_channel(image)
"""
blue_image = copy(image)
for x, y, (r, g, b) in image:
bleu = create_color(r == 0, g == 0, b)
set_color(blue_image, x, y, bleu)
show(blue_image)
return(blue_image)
# L8-18-P5-Vertical_Flip.py
def flip_vertical(image: Image) -> Image:
"""Author: Sebastien Grondin
Returns and shows a flipped, on the vertical axis, copy of a given image.
>>>image = load_image(choose_file())
>>>flip_vertical(image)
"""
new_image = copy(image)
width_pixels = get_width(image)
height_pixels = get_height(image)
for y in range(height_pixels):
for x in range(width_pixels):
pix_color = get_color(image, x, y)
set_color(new_image, width_pixels - 1 - x, y, pix_color)
show(new_image)
return new_image
# L8-18-P5-Improved_Edge_Detection.py
def detect_edges_better(original: Image, threshold: float) -> Image:
"""Author: Teshwar Tarachand
Returns and shows the improved edge detected version of a given image and
threshhold value.
>>> image = load_image(choose_file())
>>> detect_edges_better(image, 10)
"""
image = copy(original)
height = get_height(image)
width = get_width(image)
for x, y, (r, g, b) in image:
pixel1 = get_color(original, x, y)
brightness1 = (pixel1[0] + pixel1[1] + pixel1[2]) // 3
if y != (height-1):
pixel2 = get_color(original, x, y + 1)
brightness2 = (pixel2[0] + pixel2[1] + pixel2[2]) // 3
contrast_Below = abs(brightness1 - brightness2)
if x != (width - 1):
pixel3 = get_color(original, x + 1, y)
brightness3 = (pixel3[0] + pixel3[1] + pixel3[2]) // 3
contrast_Beside = abs(brightness1 - brightness3)
if contrast_Below > threshold or contrast_Beside > threshold:
high_image = create_color(0, 0, 0)
set_color(image, x, y, high_image)
else:
low_image = create_color(255, 255, 255)
set_color(image, x, y, low_image)
show(image)
return image
# L8-18-P5-Horizontal_Flip.py
def flip_horizontal(image: Image) -> Image:
"""Author: Tarachand Teshwar
Takes in an image, and flips it along the horizontal axis. Returns and
shows the flipped image.
>>>image = load_image(choose_file())
>>>flip_horizontal(image)
"""
new_image = copy(image)
width = get_width(image)
height = get_height(image)
for y in range(height // 2):
for x in range(width):
left = get_color(image, x, y)
right = get_color(image, x, height - 1 - y)
set_color(new_image, x, height - 1 - y, left)
set_color(new_image, x, y, right)
show(new_image)
return new_image
# L8-18-P5-Edge_Detection.py
def detect_edges(image: Image, threshold: float) -> Image:
"""Author: Dylan Taylor
Takes in an image and a threshold value. Returns and shows an image that
looks like it has been drawn as a pencil sketch.
>>>image = load_image(choose_file())
>>>detect_edges(image, 10)
"""
height = get_height(image)
for x, y, (r, g, b) in image:
if y != (height-1):
pixel1 = get_color(image, x, y)
brightness1 = (pixel1[0] + pixel1[1] + pixel1[2]) / 3
pixel2 = get_color(image, x, y + 1)
brightness2 = (pixel2[0] + pixel2[1] + pixel2[2]) / 3
contrast = (abs(brightness1 - brightness2))
if contrast > threshold:
high_image = create_color(0, 0, 0)
set_color(image, x, y, high_image)
elif contrast < threshold:
low_image = create_color(255, 255, 255)
set_color(image, x, y, low_image)
show(image)
return(image)
# L8-18-P4-Two_Tone_Filter.py
colours = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255),
(255, 255, 0), (0, 255, 255), (255, 0, 255), (128, 128, 128)]
list1 = [("black", 1), ("white", 2), ("red", 3), ("lime", 4), ("blue", 5),
("yellow", 6), ("cyan", 7), ("magenta", 8), ("grey", 9)]
def two_tone_filter(image: Image, colour1: str, colour2: str) -> Image:
"""Author: Dylan Taylor
Returns and shows a two toned varient of a given image and two selected
colours. It will iterate through every pixel altering it according to its
brightness.
>>>image = load_image(choose_file())
>>>two_tone_filter(image, "black", "white")
"""
for i in range(len(list1)):
colour, number = list1[i]
if colour1 == colour:
colour1 = number
for i in range(len(list1)):
colour, number = list1[i]
if colour2 == colour:
colour2 = number
new_image = copy(image)
c1 = int(colour1) - 1
c2 = int(colour2) - 1
r1 = colours[c1][0]
g1 = colours[c1][1]
b1 = colours[c1][2]
r2 = colours[c2][0]
g2 = colours[c2][1]
b2 = colours[c2][2]
for x, y, (r, g, b) in image:
brightness = (r + g + b) / 3
if brightness <= 127:
(r, g, b) = (r1, g1, b1)
toned_image = create_color(r, g, b)
set_color(new_image, x, y, toned_image)
else:
(r, g, b) = (r2, g2, b2)
toned_image = create_color(r, g, b)
set_color(new_image, x, y, toned_image)
show(new_image)
return new_image
# L8-18-P4-Three_Tone_Filter.py
colours = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255),
(255, 255, 0), (0, 255, 255), (255, 0, 255), (128, 128, 128)]
list1 = [("black", 1), ("white", 2), ("red", 3), ("lime", 4), ("blue", 5),
("yellow", 6), ("cyan", 7), ("magenta", 8), ("grey", 9)]
def three_tone_filter(image: Image, colour1: str, colour2: str,
colour3: str) -> Image:
"""Author: Dylan Taylor
Returns and shows a two toned varient of a given image and three selected
colours. It will iterate through every pixel altering it according to its
brightness.
>>>image = load_image(choose_file())
>>>three_tone_filter(image, "black", "white", "red")
"""
for i in range(len(list1)):
colour, number = list1[i]
if colour1 == colour:
colour1 = number
for i in range(len(list1)):
colour, number = list1[i]
if colour2 == colour:
colour2 = number
for i in range(len(list1)):
colour, number = list1[i]
if colour3 == colour:
colour3 = number
new_image = copy(image)
c1 = int(colour1) - 1
c2 = int(colour2) - 1
c3 = int(colour3) - 1
r1 = colours[c1][0]
g1 = colours[c1][1]
b1 = colours[c1][2]
r2 = colours[c2][0]
g2 = colours[c2][1]
b2 = colours[c2][2]
r3 = colours[c3][0]
g3 = colours[c3][1]
b3 = colours[c3][2]
for x, y, (r, g, b) in image:
brightness = (r + g + b) / 3
if brightness <= 84:
(r, g, b) = (r1, g1, b1)
toned_image = create_color(r, g, b)
set_color(new_image, x, y, toned_image)
elif brightness >= 85 and brightness <= 170:
(r, g, b) = (r2, g2, b2)
toned_image = create_color(r, g, b)
set_color(new_image, x, y, toned_image)
elif brightness >= 171 and brightness <= 255:
(r, g, b) = (r3, g3, b3)
toned_image = create_color(r, g, b)
set_color(new_image, x, y, toned_image)
show(new_image)
return new_image
# L8-18-P4-Sepia.py
def grayscale(image: Image) -> Image:
"""
Return a grayscale copy of image.
>>> image = load_image(choose_file())
>>> gray_image = grayscale(image)
>>> show(gray_image)
"""
new_image = copy(image)
for x, y, (r, g, b) in image:
brightness = (r + g + b) // 3
gray = create_color(brightness, brightness, brightness)
set_color(new_image, x, y, gray)
return new_image
def sepia(image: Image) -> Image:
"""Author: Teshwar Tarachand, Dylan Taylor, Sebastien Grondin
Returns and shows an image with a sepia tinting filter applied to it.
>>>image = load_image(choose_file())
>>>sepia(image)
"""
new_image = copy(image)
gray_image = grayscale(new_image)
for x, y, (r, g, b) in gray_image:
ReGrBl = get_color(gray_image, x, y)
RGB = ReGrBl[1]
if RGB < 63:
R = RGB * 1.1
B = RGB * 0.9
tint = create_color(R, RGB, B)
set_color(gray_image, x, y, tint)
elif RGB >= 63 and RGB <= 191:
R = RGB * 1.15
B = RGB * 0.85
tint = create_color(R, RGB, B)
set_color(gray_image, x, y, tint)
elif RGB > 191:
R = RGB * 1.08
B = RGB * 0.93
tint = create_color(R, RGB, B)
set_color(gray_image, x, y, tint)
show(gray_image)
return gray_image
# L8-18-P4-Posterizing.py
def _adjust_component(quad: int) -> int:
"""Author: Teshwar Tarachand
Finds the range and returns the mid point of a given pixel's RGB values.
Meant to be used within other functions.
"""
if quad <= 63:
quad = 31
return quad
elif quad >= 64 and quad <= 127:
quad = 95
return quad
elif quad >= 128 and quad <= 191:
quad = 159
return quad
elif quad >= 192 and quad <= 255:
quad = 233
return quad
def posterize(image: Image) -> Image:
"""Author: Teshwar Tarachand
Returns and shows a posterized copy of a given image.
>>>image = load_image(choose_file())
>>>posterize(image)
"""
image = copy(image)
for x, y, (r, g, b) in image:
final_colour = create_color(_adjust_component(r), _adjust_component(g),
_adjust_component(b))
set_color(image, x, y, final_colour)
show(image)
return(image)
# L8-18-P4-Extreme_Contrast.py
def extreme_contrast(image:Image)->Image:
"""Author: Sebastien Grondin
Returns and shows the extreme contrast varient of a given image.
>>> image = load_image(choose_file())
>>> extreme_contrast(image)
"""
new_image = copy(image)
for x, y, (r, g, b) in new_image:
new_red = 0
new_green = 0
new_blue = 0
if r >= 128:
new_red = 255
if g >= 128:
new_green = 255
if b >= 128:
new_blue = 255
final_colour = create_color(new_red, new_green, new_blue)
set_color(new_image, x, y, final_colour)
show(new_image)
return(new_image)