-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyPixelate.py
50 lines (37 loc) · 1.66 KB
/
pyPixelate.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
import cv2
import numpy as np
from multiprocessing import Pool
from sys import argv
PADDING = 2000
GRID_LINES = True
def makePixelate(img, output_img, imheight, imwidth, width, height, block_size, fileName):
for i in range(0, imheight // block_size):
for j in range(0, imwidth // block_size):
x, x2 = j * width, (j + 1) * width
y, y2 = i * height, (i + 1) * height
a = img[y:y2, x:x2]
a = np.array(a)
mean_color = np.mean(a, axis=(0, 1))
mean_color = tuple([int(mean_color[0]), int(mean_color[1]), int(mean_color[2])])
if GRID_LINES:
a = np.ones((height-2, width-2, 3), np.uint8)*mean_color
a = cv2.copyMakeBorder(a, 1, 1, 1, 1, borderType=cv2.BORDER_CONSTANT,value=0)
else:
a = np.ones((height, width, 3), np.uint8)*mean_color
output_img[y:y2, x:x2] = a
cv2.imwrite(fileName, output_img)
def pixelate(img, block_size, fileName):
img = cv2.imread(img)
img = cv2.resize(img, (PADDING,PADDING), interpolation = cv2.INTER_LINEAR)
imwidth, imheight = img.shape[:2]
height = imheight // (imheight // block_size)
width = imwidth // (imwidth // block_size)
output_img = np.ones((PADDING,PADDING,3), np.uint8)*255
pool = Pool(16)
pool.starmap(makePixelate, [(img, output_img, imheight, imwidth, width, height, block_size, fileName)])
pool.close()
if __name__ == "__main__":
if len(argv) != 4:
print("Usage: python3 pyPixelate.py <image> <block size (can be divided with PADDING)> <output image (e.g. 1.jpg)>")
exit(1)
pixelate(argv[1], int(argv[2]), argv[3])