-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
265 lines (241 loc) · 9.51 KB
/
app.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
# image library
from PIL import Image, ImageFilter, ImageOps
from PIL.ImageMorph import LutBuilder, MorphOp
import cv2
# cache
from functools import lru_cache
# TO DO resize to nearest positive multiple of pixel_size before pixellate and then resize back
import numpy as np
import matplotlib.pyplot as plt
class Pixie:
@staticmethod
def resize ( img:Image.Image, width, height) -> Image.Image:
"""
Resize image
:param img: Image
"""
img = img.resize((width, height))
return img
@staticmethod
def scale ( img:Image.Image, factor) -> Image.Image:
"""
Scale image
:param img: Image
"""
img = img.resize((int(img.width * factor), int(img.height * factor)))
return img
@staticmethod
def blur( img:Image.Image, pixel_size) -> Image.Image:
"""
Blur image
:param img: Image
"""
img = Pixie.scale(img,1 / pixel_size)
img = Pixie.scale(img,pixel_size)
return img
@staticmethod
def pixellate( img:Image.Image, pixel_size) -> Image.Image:
"""
Pixellate image
:param img: Image
"""
np_img = np.array(img)
h,w,c = np_img.shape
new_h = h // pixel_size
new_w = w // pixel_size
new_np_img = np.zeros((new_h*pixel_size,new_w*pixel_size,c),dtype = np.uint8)
# loop through pixels each channel at the time
for c in range(np_img.shape[-1]):
for i in range(new_h):
for j in range(new_w):
# get pixel value as mean of pixel_size x pixel_size square
pixel = np_img[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size,c].mean()
# set pixel value for pixel_size x pixel_size square
new_np_img[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size,c] = pixel
new_img = Image.fromarray(new_np_img)
return new_img
@staticmethod
def edge_detection( img:Image.Image) -> Image.Image:
"""
Edge detection
:param img: Image
"""
img = img.convert('L')
img = img.filter(ImageFilter.FIND_EDGES)
return img
@staticmethod
def sovraimpose( img:Image.Image, img2:Image.Image) -> Image.Image:
"""
Sovraimpose image
:param img: Image
"""
img = img.convert('RGBA')
img2 = img2.convert('RGBA')
img = Image.blend(img, img2, 0.5)
return img
@staticmethod
def outline( img:Image.Image, outline:Image.Image) -> Image.Image:
"""
follows the edges of the image in order to outline them with 1 pixel of contour
:param img: Image
"""
np_img = np.array(img, dtype = np.uint8)
np_outline = np.array(outline, dtype = np.uint8)
res = np.copy(np_img)
res[:,:,-1] -= np_outline
outlined_img = Image.fromarray(res)
return outlined_img
@staticmethod
def recolour( img:Image.Image, colors:int) -> Image.Image:
"""
Recolour image
:param img: Image
"""
img = img.convert('P', palette=Image.ADAPTIVE, colors=colors)
img = img.convert('RGBA')
return img
@staticmethod
def recolour_palette( img:Image.Image, palette:list[tuple[int,int,int]], pixel_size) -> Image.Image:
"""
Recolour image with custom palette of colors in RGB format
:param img: Image
"""
# pal:list = np.array(palette, dtype = np.uint8).tolist()
img = img.convert('RGB')
np_img = np.array(img)
h,w,c = np_img.shape
new_np_img = np.zeros((h,w,c),dtype = np.uint8)
# loop through pixels each channel at the time
for i in range(0,h-1, pixel_size):
for j in range(0,w-1, pixel_size):
# get pixel value as mean of pixel_size x pixel_size square
pixel = Pixie.most_similar_colour(tuple(np_img[i,j,:]), palette)
# set pixel value for pixel_size x pixel_size square
new_np_img[i:i+pixel_size,j:j+pixel_size,:] = pixel
return Image.fromarray(new_np_img)
@staticmethod
@lru_cache(maxsize=1024, typed=True)
def most_similar_colour(colour:tuple[int,int,int], palette:list[tuple[int,int,int]]) -> tuple[int,int,int]:
"""
Find the most similar colour in the palette
:param colour: RGB colour
"""
best = palette[0]
# euclidean distance
best_dist = np.linalg.norm(np.array(colour) - np.array(best))
for c in palette:
dist = np.linalg.norm(np.array(colour) - np.array(c))
if dist < best_dist:
best = c
best_dist = dist
return best
@staticmethod
def outline_pixels( img:Image.Image, outline:Image.Image, pixel_size) -> Image.Image:
"""
follows the edges of the image in order to outline them with 1 pixel of contour
:param img: Image
"""
np_img = np.array(img)
np_outline = np.array(outline)
h,w,c = np_img.shape
new_h = h // pixel_size
new_w = w // pixel_size
res = np.copy(np_img) # non clippare!!! aggiungo nero in base alla maschera
# outline_threshold = 50 # threshold for the outline, 0 is min and 255 is max
# outline_mask = np_outline > outline_threshold
for i in range(new_h):
for j in range(new_w):
# get pixel value as mean of pixel_size x pixel_size square
res[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size,-1] -= np_outline[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size].max()
# set pixel value for pixel_size x pixel_size square
# if color:
# res[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size,:] = 0
outlined_img = Image.fromarray(res)
return outlined_img
@staticmethod
def outline_edges( img:Image.Image) -> Image.Image:
lb = LutBuilder(op_name='dilation')
dil = MorphOp(lb.build_lut())
lb = LutBuilder(op_name='erosion')
ero = MorphOp(lb.build_lut())
_,img = dil.apply(img)
_,img = ero.apply(img)
return img
@staticmethod
def pixellate_resize( img:Image.Image, pixel_size) -> Image.Image:
"""
Pixellate image
:param img: Image
"""
np_img = np.array(img)
h,w,c = np_img.shape
new_np_img = np.zeros((h*pixel_size,w*pixel_size,c),dtype = np.uint8)
# loop through pixels each channel at the time
for c in range(np_img.shape[-1]):
for i in range(h):
for j in range(w):
# get pixel value as mean of pixel_size x pixel_size square
pixel = np_img[i:i+1,j:j+1,c]
# set pixel value for pixel_size x pixel_size square
new_np_img[i*pixel_size:(i+1)*pixel_size,j*pixel_size:(j+1)*pixel_size,c] = pixel
new_img = Image.fromarray(new_np_img)
return new_img
@staticmethod
def colorImage(img:Image.Image) -> list[tuple[int,int,int]]:
"""
function to automatically take all the colours in RGB from an image
:param img: Image
"""
img = img.convert('RGB')
np_img = np.array(img)
h,w,_ = np_img.shape
colors = {}
for i in range(h):
for j in range(w):
colors[tuple(np_img[i,j,:])] = 1
return list(colors.keys())
if __name__ == '__main__':
# open image
# img = Image.open('assets/luffy-chibi-2.png')
# img = Image.open('luffy.png')
# img = Image.open('zoro.webp')
# img = Image.open('sanji.webp')
img = Image.open('assets/img.png')
# pixellated = Pixie.pixellate(img_rc, 10)
# pixellated.show()
# img = Pixie.recolour(img, 24)
# small = Pixie.scale(img_rc, 0.1)
new_widht = img.width // 5
new_height = img.height // 5
small = Pixie.resize(img, new_widht, new_height)
lg = Pixie.resize(small, small.width*5, small.height*5)
lg.show()
# lg.save('assets/luffy-blurry.png')
pysharp = small.filter(ImageFilter.EDGE_ENHANCE_MORE)
# pysharp = small.filter(ImageFilter.EDGE_ENHANCE_MORE)
# pysharp = small.filter(ImageFilter.SHARPEN)
# small.show()
pix = Pixie.pixellate_resize(pysharp, 5)
pix.show()
pix.save('assets/luffy-pixellated.png')
# k = np.array([[-2,-2,-2], [-2,32,-2], [-2,-2,-2]], dtype=np.int8)/16
k = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]], dtype=np.float32)
# k = np.array([[-1,-1,-1], [-1,10,-1], [-1,-1,-1]], dtype=np.float32)/2
print(k)
cv_filter = cv2.filter2D(np.array(small), -1, k)
print(cv_filter)
palette = [[255,0,0],[0,255,0],[0,0,255],[255,255,0],[255,0,255],[0,255,255],[255,255,255],[0,0,0]]
# turn list of lists into tuples of tuples
tuple_palette = tuple([tuple(l) for l in palette])
rc = Pixie.recolour_palette(small, tuple_palette, pixel_size=1)
rc.show()
print(np.array(rc))
cv_filter = Image.fromarray(cv_filter)
cv2_resize = cv2.resize(np.array(cv_filter), (small.width*5, small.height*5), interpolation=cv2.INTER_NEAREST)
cv2_resize = Image.fromarray(cv2_resize)
cv2_resize.show()
pix = Pixie.pixellate_resize(cv_filter, 5)
pix.show()
palette = Image.open('assets/palette.jpg')
palette = Pixie.colorImage(small)
print(palette)