Spaces:
Running
Running
Update util/img2pixl.py
Browse filesThe pixera algorithm has been improved.
- util/img2pixl.py +62 -62
util/img2pixl.py
CHANGED
|
@@ -6,48 +6,48 @@ from PIL import Image
|
|
| 6 |
|
| 7 |
class pixL:
|
| 8 |
|
| 9 |
-
def __init__(self,numOfSquaresW = None, numOfSquaresH= None, size = [True, (512,512)],square = 6,ImgH = None,ImgW = None,
|
| 10 |
-
self.images = images
|
| 11 |
self.size = size
|
| 12 |
self.ImgH = ImgH
|
| 13 |
self.ImgW = ImgW
|
|
|
|
| 14 |
self.square = square
|
|
|
|
|
|
|
| 15 |
self.numOfSquaresW = numOfSquaresW
|
| 16 |
self.numOfSquaresH = numOfSquaresH
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
for image in self.images:
|
| 20 |
-
|
| 21 |
-
size = (image.shape[0] - (image.shape[0] % 4), image.shape[1] - (image.shape[1] % 4))
|
| 22 |
-
image = cv2.resize(image, size)
|
| 23 |
-
return self.images
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def toThePixL(self,images, pixel_size):
|
| 27 |
-
self.images = []
|
| 28 |
self.square = pixel_size
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def numOfSquaresFunc(self):
|
| 38 |
self.numOfSquaresW = round((self.ImgW / self.square) + 1)
|
| 39 |
self.numOfSquaresH = round((self.ImgH / self.square) + 1)
|
| 40 |
|
| 41 |
def optimizer(RGB):
|
| 42 |
-
|
| 43 |
R_ = RGB[2]
|
| 44 |
G_ = RGB[1]
|
| 45 |
B_ = RGB[0]
|
| 46 |
|
| 47 |
-
if R_ < 50 and G_ < 50 and B_ < 50:
|
| 48 |
-
|
| 49 |
return (R_, G_, B_)
|
| 50 |
|
|
|
|
|
|
|
|
|
|
| 51 |
else:
|
| 52 |
sign = lambda x, y: random.choice([x,y])
|
| 53 |
|
|
@@ -61,52 +61,52 @@ class pixL:
|
|
| 61 |
|
| 62 |
return (R_, G_, B_)
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
pixValues = []
|
| 66 |
pixL.numOfSquaresFunc(self)
|
| 67 |
|
| 68 |
for j in range(1,self.numOfSquaresH):
|
| 69 |
|
| 70 |
for i in range(1,self.numOfSquaresW):
|
| 71 |
-
|
| 72 |
-
pixValues.append((image.getpixel((
|
| 73 |
i * self.square - self.square//2,
|
| 74 |
j * self.square - self.square//2)),
|
| 75 |
(i * self.square - self.square//2,
|
| 76 |
j * self.square - self.square//2)))
|
| 77 |
-
|
| 78 |
-
background = 255 * np.ones(shape=[self.ImgH - self.square,
|
| 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 |
-
|
|
|
|
|
|
| 6 |
|
| 7 |
class pixL:
|
| 8 |
|
| 9 |
+
def __init__(self,numOfSquaresW = None, numOfSquaresH= None, size = [True, (512,512)],square = 6,ImgH = None,ImgW = None,image = None,background = None, pixValues = []):
|
|
|
|
| 10 |
self.size = size
|
| 11 |
self.ImgH = ImgH
|
| 12 |
self.ImgW = ImgW
|
| 13 |
+
self.image = image
|
| 14 |
self.square = square
|
| 15 |
+
self.pixValues = pixValues
|
| 16 |
+
self.background = background
|
| 17 |
self.numOfSquaresW = numOfSquaresW
|
| 18 |
self.numOfSquaresH = numOfSquaresH
|
| 19 |
|
| 20 |
+
def toThePixL(self,image, pixel_size, segMode= False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
self.square = pixel_size
|
| 22 |
+
self.image = Image.fromarray(image).convert("RGB").resize((512,512))
|
| 23 |
+
self.ImgW, self.ImgH = self.image.size
|
| 24 |
+
self.image = pixL.colorPicker(self)
|
| 25 |
+
pixL.complier(self)
|
| 26 |
+
return pixL.postprocess(self)
|
| 27 |
+
|
| 28 |
+
def postprocess(self):
|
| 29 |
+
image = self.background
|
| 30 |
+
size = (image.shape[0] - (image.shape[0] % 4), image.shape[1] - (image.shape[1] % 4))
|
| 31 |
+
image = cv2.resize(image, size)
|
| 32 |
+
return image
|
| 33 |
|
| 34 |
def numOfSquaresFunc(self):
|
| 35 |
self.numOfSquaresW = round((self.ImgW / self.square) + 1)
|
| 36 |
self.numOfSquaresH = round((self.ImgH / self.square) + 1)
|
| 37 |
|
| 38 |
def optimizer(RGB):
|
| 39 |
+
|
| 40 |
R_ = RGB[2]
|
| 41 |
G_ = RGB[1]
|
| 42 |
B_ = RGB[0]
|
| 43 |
|
| 44 |
+
if R_ < 50 and G_ < 50 and B_ < 50:
|
| 45 |
+
|
| 46 |
return (R_, G_, B_)
|
| 47 |
|
| 48 |
+
elif 220 < R_ < 255 and 220 < G_ < 255 and 220 < B_ < 255:
|
| 49 |
+
|
| 50 |
+
return (R_, G_, B_)
|
| 51 |
else:
|
| 52 |
sign = lambda x, y: random.choice([x,y])
|
| 53 |
|
|
|
|
| 61 |
|
| 62 |
return (R_, G_, B_)
|
| 63 |
|
| 64 |
+
def colorPicker(self):
|
|
|
|
| 65 |
pixL.numOfSquaresFunc(self)
|
| 66 |
|
| 67 |
for j in range(1,self.numOfSquaresH):
|
| 68 |
|
| 69 |
for i in range(1,self.numOfSquaresW):
|
| 70 |
+
|
| 71 |
+
self.pixValues.append((self.image.getpixel((
|
| 72 |
i * self.square - self.square//2,
|
| 73 |
j * self.square - self.square//2)),
|
| 74 |
(i * self.square - self.square//2,
|
| 75 |
j * self.square - self.square//2)))
|
| 76 |
+
|
| 77 |
+
self.background = 255 * np.ones(shape=[self.ImgH - self.square,
|
| 78 |
+
self.ImgW - self.square*2, 3],
|
| 79 |
+
dtype=np.uint8)
|
| 80 |
+
|
| 81 |
+
def PEN(self,coorX,coorY,R,G,B):
|
| 82 |
+
SQUARE = self.square
|
| 83 |
+
cv2.rectangle(self.background,
|
| 84 |
+
pt1=(coorX - SQUARE, coorY - SQUARE), #0, 0 -> 0, 0
|
| 85 |
+
pt2=(coorX, coorY), #6, 6 -> 3, 3
|
| 86 |
+
color=(pixL.optimizer((R,G,B))),
|
| 87 |
+
thickness=-1)
|
| 88 |
+
|
| 89 |
+
cv2.rectangle(self.background,
|
| 90 |
+
pt1=(coorX, coorY - SQUARE), #0, 0 -> 3, 0
|
| 91 |
+
pt2=(coorX + SQUARE, coorY), #6, 6 -> 6, 3
|
| 92 |
+
color=(pixL.optimizer((R,G,B))),
|
| 93 |
+
thickness=-1)
|
| 94 |
+
|
| 95 |
+
cv2.rectangle(self.background,
|
| 96 |
+
pt1=(coorX - SQUARE, coorY), #0, 0 -> 0, 3
|
| 97 |
+
pt2=(coorX, coorY + SQUARE), #6, 6 -> 3, 6
|
| 98 |
+
color=(pixL.optimizer((R,G,B))),
|
| 99 |
+
thickness=-1)
|
| 100 |
+
|
| 101 |
+
cv2.rectangle(self.background,
|
| 102 |
+
pt1=(coorX, coorY), #0, 0 -> 3, 3
|
| 103 |
+
pt2=(coorX + SQUARE, coorY + SQUARE), #6, 6 -> 6, 6
|
| 104 |
+
color=(pixL.optimizer((R,G,B))),
|
| 105 |
+
thickness=-1)
|
| 106 |
+
|
| 107 |
+
def complier(self):
|
| 108 |
+
for index, value in enumerate(self.pixValues):
|
| 109 |
+
(R,G,B), (coorX, coorY) = value
|
| 110 |
+
pixL.PEN(self,coorX,coorY,R,G,B)
|
| 111 |
+
self.background = np.array(self.background).astype(np.uint8)
|
| 112 |
+
self.background = cv2.resize(self.background, (self.ImgW,self.ImgH), interpolation = cv2.INTER_AREA)
|