-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform_empty.py
More file actions
76 lines (70 loc) · 3.65 KB
/
Copy pathtransform_empty.py
File metadata and controls
76 lines (70 loc) · 3.65 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
from image import Image
import numpy as np
def brighten(image, factor):
# when we brighten, we just want to make each channel higher by some amount
# factor is a value > 0, how much you want to brighten the image by (< 1 = darken, > 1 = brighten)
x_pixels, y_pixels, num_channels = image.array.shape
new_im = Image(x_pixels=x_pixels, y_pixels=y_pixels, num_channels=num_channels)
new_im.array = image.array * factor
return new_im
def adjust_contrast(image, factor, mid):
# adjust the contrast by increasing the difference from the user-defined midpoint by factor amount
x_pixels, y_pixels, num_channels = image.array.shape
new_im = Image(x_pixels=x_pixels, y_pixels=y_pixels, num_channels=num_channels)
for x in range(x_pixels):
for y in range(y_pixels):
for c in range(num_channels):
new_im.array[x, y, c] = (image.array[x, y, c] - mid) * factor + mid
return new_im
def blur(image, kernel_size):
# kernel size is the number of pixels to take into account when applying the blur
# (ie kernel_size = 3 would be neighbors to the left/right, top/bottom, and diagonals)
# kernel size should always be an *odd* number
x_pixels, y_pixels, num_channels = image.array.shape
new_im = Image(x_pixels=x_pixels, y_pixels=y_pixels, num_channels=num_channels)
neighbor_range = kernel_size // 2
for x in range(x_pixels):
for y in range(y_pixels):
for c in range(num_channels):
total = 0
count = 0
for x_i in range(max(0, x - neighbor_range), min(new_im.x_pixels - 1, x + neighbor_range) + 1):
for y_i in range(max(0, y - neighbor_range), min(new_im.y_pixels - 1, y + neighbor_range) + 1):
total += image.array[x_i, y_i, c]
count += 1
new_im.array[x, y, c] = total / count
return new_im
def apply_kernel(image, kernel):
# the kernel should be a 2D array that represents the kernel we'll use!
# for the sake of simiplicity of this implementation, let's assume that the kernel is SQUARE
# for example the sobel x kernel (detecting horizontal edges) is as follows:
# [1 0 -1]
# [2 0 -2]
# [1 0 -1]
x_pixels, y_pixels, num_channels = image.array.shape
new_im = Image(x_pixels=x_pixels, y_pixels=y_pixels, num_channels=num_channels)
neighbor_range = kernel.shape[0] // 2
for x in range(x_pixels):
for y in range(y_pixels):
for c in range(num_channels):
total = 0
for x_i in range(max(0, x - neighbor_range), min(new_im.x_pixels - 1, x + neighbor_range) + 1):
for y_i in range(max(0, y - neighbor_range), min(new_im.y_pixels - 1, y + neighbor_range) + 1):
x_k = x_i + neighbor_range - x
y_k = y_i + neighbor_range - y
total += image.array[x_i, y_i, c] * kernel[x_k, y_k]
new_im.array[x, y, c] = total
return new_im
def combine_images(image1, image2):
# let's combine two images using the squared sum of squares: value = sqrt(value_1**2, value_2**2)
# size of image1 and image2 MUST be the same
x_pixels, y_pixels, num_channels = image1.array.shape
new_im = Image(x_pixels=x_pixels, y_pixels=y_pixels, num_channels=num_channels)
for x in range(x_pixels):
for y in range(y_pixels):
for c in range(num_channels):
new_im.array[x, y, c] = (image1.array[x, y, c] ** 2 + image2.array[x, y, c] ** 2) ** 0.5
return new_im
if __name__ == '__main__':
lake = Image(filename='lake.png')
city = Image(filename='city.png')