Alican commited on
Commit
9cbde03
·
1 Parent(s): f37a2ae

Update util/img2pixl.py

Browse files

The pixera algorithm has been improved.

Files changed (1) hide show
  1. 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,images = [],background_image = 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 preprocess(self):
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
- for image in images:
30
- image = Image.fromarray(image)
31
- image = image.convert("RGB")
32
- self.ImgW, self.ImgH = image.size
33
- self.images.append(pixL.epicAlgorithm(self, image))
34
-
35
- return pixL.preprocess(self)
 
 
 
 
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 epicAlgorithm(self, image):
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
- self.ImgW - self.square*2, 3],
80
- dtype=np.uint8)
81
-
82
- for pen in range(len(pixValues)):
83
-
84
-
85
- cv2.rectangle(background,
86
- pt1=(pixValues[pen][1][0] - self.square, pixValues[pen][1][1] - self.square), #0, 0 -> 0, 0
87
- pt2=(pixValues[pen][1][0], pixValues[pen][1][1]), #6, 6 -> 3, 3
88
- color=(pixL.optimizer(pixValues[pen][0])),
89
- thickness=-1)
90
-
91
- cv2.rectangle(background,
92
- pt1=(pixValues[pen][1][0], pixValues[pen][1][1] - self.square), #0, 0 -> 3, 0
93
- pt2=(pixValues[pen][1][0] + self.square, pixValues[pen][1][1]), #6, 6 -> 6, 3
94
- color=(pixL.optimizer(pixValues[pen][0])),
95
- thickness=-1)
96
-
97
- cv2.rectangle(background,
98
- pt1=(pixValues[pen][1][0] - self.square, pixValues[pen][1][1]), #0, 0 -> 0, 3
99
- pt2=(pixValues[pen][1][0], pixValues[pen][1][1] + self.square), #6, 6 -> 3, 6
100
- color=(pixL.optimizer(pixValues[pen][0])),
101
- thickness=-1)
102
-
103
- cv2.rectangle(background,
104
- pt1=(pixValues[pen][1][0], pixValues[pen][1][1]), #0, 0 -> 3, 3
105
- pt2=(pixValues[pen][1][0] + self.square, pixValues[pen][1][1] + self.square), #6, 6 -> 6, 6
106
- color=(pixL.optimizer(pixValues[pen][0])),
107
- thickness=-1)
108
-
109
- background = np.array(background).astype(np.uint8)
110
- background = cv2.resize(background, (self.ImgW,self.ImgH), interpolation = cv2.INTER_AREA)
111
-
112
- return background
 
 
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)