-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
187 lines (142 loc) · 5.57 KB
/
main.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
import numpy as np
from math import floor, ceil
from PIL import Image
from numpy import asarray
def get_array_value(x: int, y: int, array: np.ndarray):
"""Returns the value of the array at position x,y."""
return array[y, x]
def convert_to_red(img: np.ndarray):
# with np.nditer(img, op_flags=['readwrite']) as it:
# for x in it:
# x[...] = []
img.setflags(write=1)
for i, label_i in enumerate(img):
for j, label_j in enumerate(img):
#can i modify it now?
img[i][j] = [0, 0, 0]
print(img[i][j])
'''
A function to convert from an ndarray and save
it to the specified location
'''
def save_image(img: np.ndarray, address):
image = Image.fromarray(img)
return image.save(address)
def bilinear_interpolation2(x: float, y: float, img: np.ndarray) -> float:
"""Returns the bilinear interpolation of a pixel in the image.
:param x: x-position to interpolate
:param y: y-position to interpolate
:param img: image, where the pixel should be interpolated
:returns: value of the interpolated pixel
"""
if x < 0 or y < 0:
raise ValueError("x and y pixel position have to be positive!")
if img.shape[1] - 1 < x or img.shape[0] - 1 < y:
raise ValueError(
"x and y pixel position have to be smaller than image" "dimensions."
)
x_rounded_up = int(ceil(x))
x_rounded_down = int(floor(x))
y_rounded_up = int(ceil(y))
y_rounded_down = int(floor(y))
ratio_x = x - x_rounded_down
ratio_y = y - y_rounded_down
interpolate_x1 = interpolate(
get_array_value(x_rounded_down, y_rounded_down, img),
get_array_value(x_rounded_up, y_rounded_down, img),
ratio_x,
)
interpolate_x2 = interpolate(
get_array_value(x_rounded_down, y_rounded_up, img),
get_array_value(x_rounded_up, y_rounded_up, img),
ratio_x,
)
interpolate_y = interpolate(interpolate_x1, interpolate_x2, ratio_y)
return interpolate_y
def generate_image():
array = np.zeros([100, 200], dtype=np.uint8)
for x in range(200):
for y in range(100):
if (x % 16) // 8 == (y % 16) // 8:
array[y, x] = 0
else:
array[y, x] = 255
return array
def nearest_neighbour_interpolation(img: np.ndarray, scale: float):
# Generate blank image with new dimention
# Project original the newly generated image on
# on the original image to locate pixels
# Assign the RGB value to the new image
row, col, _ = img.shape
new_row = row * scale
new_col = col * scale
print("original size: ", (row, col))
print("new size: ", (new_row, new_col))
new_image = np.zeros([new_row , new_col, 3], dtype=np.uint8)
for x in range(new_row):
for y in range(new_col):
proj_x = round(x / scale)
proj_y = round(y / scale)
if proj_x < row and proj_y < col:
new_image[x, y] = img[proj_x, proj_y]
# print((proj_x, proj_y))
save_image(new_image, "./img/omg.jpeg")
# for x in range(row):
# for y in range(col):
# print(img[x, y])
def bilinear_interpolation(img: np.ndarray, scale: float):
row, col, _ = img.shape
new_row = row * scale
new_col = col * scale
print("original size: ", (row, col))
print("new size: ", (new_row, new_col))
new_image = np.zeros([new_row , new_col, 3], dtype=np.uint8)
for x in range(new_row):
for y in range(new_col):
proj_x = round(x / scale)
proj_y = round(y / scale)
if proj_x < row and proj_y < col:
x_l = floor(proj_x)
x_u = ceil(proj_x)
ratio_x = proj_x - x_l
y_l = floor(proj_y)
y_u = ceil(proj_y)
ratio_y = proj_y - y_l
interpolate_x1 = interpolate_rgb(img[x_l, y_l], img[x_u, y_l], ratio_x)
interpolate_x2 = interpolate_rgb(img[x_l, y_u], img[x_u, y_u], ratio_x)
interpolate_y = interpolate_rgb(interpolate_x1, interpolate_x2, ratio_y)
new_image[x, y] = interpolate_y
save_image(new_image, "./img/ohh_my_god_it_finally_worked.jpeg")
def interpolate_rgb(rgb1, rgb2, ratio):
r = interpolate(rgb1[0], rgb2[0], ratio)
g = interpolate(rgb1[1], rgb2[1], ratio)
b = interpolate(rgb1[2], rgb2[2], ratio)
return np.ndarray((3,), buffer=np.array([r, g, b]), dtype=int)
def interpolate(first_value: float, second_value: float, ratio: float) -> float:
"""Interpolate with a linear weighted sum."""
return first_value * (1 - ratio) + second_value * ratio
if __name__ == "__main__":
# GENERATE A GRID FOR TESTING PURPOSES
# generated_image = generate_image()
image = Image.open('./img/sample_1.jpg')
data = asarray(image)
# nearest_neighbour_interpolation(data, 10)
bilinear_interpolation(data, 20)
# save_image(generated_image, "./img/generated_image.jpeg")
# image = np.arange(0, 9).reshape((3, 3))
# print(bilinear_interpolation(0.5, 1.5, image))
# image = Image.open('./img/img.jpeg')
# data = asarray(image)
# convert_to_red(data)
# print(image.format)
# print(image.size)
# print(image.mode)
# convert image to numpy array
# print(type(data))
# # summarize shape
# print(data.shape)
# create Pillow image
# save_image(data, './img/new_image.jpeg')
# #summarize image details
# print(image2.mode)
# print(image2.size)