File size: 3,901 Bytes
13090e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import random
import numpy as np
from PIL import Image
#Author: Alican Akca

class pixL:

  def __init__(self,numOfSquaresW = None, numOfSquaresH= None, size = [True, (512,512)],square = 6,ImgH = None,ImgW = None,images = [],background_image = None):
    self.images = images
    self.size = size
    self.ImgH = ImgH
    self.ImgW = ImgW
    self.square = square
    self.numOfSquaresW = numOfSquaresW
    self.numOfSquaresH = numOfSquaresH

  def preprocess(self):
    for image in self.images:

      size = (image.shape[0] - (image.shape[0] % 4), image.shape[1] - (image.shape[1] % 4))
      image = cv2.resize(image, size)
    return self.images


  def toThePixL(self,images, pixel_size):
    self.images = []
    self.square = pixel_size
    for image in images:
      image = Image.fromarray(image)
      image = image.convert("RGB")
      self.ImgW, self.ImgH = image.size
      self.images.append(pixL.epicAlgorithm(self, image))
      
    return pixL.preprocess(self)

  def numOfSquaresFunc(self):
    self.numOfSquaresW = round((self.ImgW / self.square) + 1)
    self.numOfSquaresH = round((self.ImgH / self.square) + 1)

  def optimizer(RGB):
    
    R_ = RGB[2]
    G_ = RGB[1]
    B_ = RGB[0]

    if R_ < 50 and G_ < 50 and B_ < 50: 
      
      return (R_, G_, B_)

    else:
      sign = lambda x, y: random.choice([x,y])

      R_ = RGB[2] + sign(+1,-1)*random.randint(1,10)
      G_ = RGB[1] + sign(+1,-1)*random.randint(1,10)
      B_ = RGB[0] + sign(+1,-1)*random.randint(1,10)

      R_ = 0 if R_ < 0 else (255 if R_ > 255 else R_)
      G_ = 0 if G_ < 0 else (255 if G_ > 255 else G_)
      B_ = 0 if B_ < 0 else (255 if B_ > 255 else B_)

      return (R_, G_, B_)

  def epicAlgorithm(self, image):
    pixValues = []
    pixL.numOfSquaresFunc(self)

    for j in range(1,self.numOfSquaresH):

      for i in range(1,self.numOfSquaresW):
        
        pixValues.append((image.getpixel((
              i * self.square - self.square//2,
              j * self.square - self.square//2)),
              (i * self.square - self.square//2,
              j * self.square - self.square//2)))
    
    background = 255 * np.ones(shape=[self.ImgH - self.square, 
                                      self.ImgW - self.square*2, 3], 
                                      dtype=np.uint8)                
    
    for pen in range(len(pixValues)):
      

      cv2.rectangle(background, 
                    pt1=(pixValues[pen][1][0] - self.square, pixValues[pen][1][1] - self.square), #0, 0 -> 0, 0
                    pt2=(pixValues[pen][1][0], pixValues[pen][1][1]), #6, 6 -> 3, 3
                    color=(pixL.optimizer(pixValues[pen][0])), 
                    thickness=-1)
      
      cv2.rectangle(background, 
                    pt1=(pixValues[pen][1][0], pixValues[pen][1][1] - self.square), #0, 0 -> 3, 0
                    pt2=(pixValues[pen][1][0] + self.square, pixValues[pen][1][1]), #6, 6 -> 6, 3
                    color=(pixL.optimizer(pixValues[pen][0])), 
                    thickness=-1)
      
      cv2.rectangle(background, 
                    pt1=(pixValues[pen][1][0] - self.square, pixValues[pen][1][1]), #0, 0 -> 0, 3
                    pt2=(pixValues[pen][1][0], pixValues[pen][1][1] + self.square), #6, 6 -> 3, 6
                    color=(pixL.optimizer(pixValues[pen][0])), 
                    thickness=-1)
      
      cv2.rectangle(background, 
                    pt1=(pixValues[pen][1][0], pixValues[pen][1][1]), #0, 0 -> 3, 3
                    pt2=(pixValues[pen][1][0] + self.square, pixValues[pen][1][1] + self.square), #6, 6 -> 6, 6
                    color=(pixL.optimizer(pixValues[pen][0])), 
                    thickness=-1)
      
    background = np.array(background).astype(np.uint8)
    background = cv2.resize(background, (self.ImgW,self.ImgH), interpolation = cv2.INTER_AREA)
    
    return background