File size: 9,645 Bytes
c939804
 
 
 
 
 
 
7990c94
f6d8b89
7990c94
 
 
 
 
 
461e557
 
7990c94
 
c939804
 
 
 
7990c94
c939804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7990c94
c939804
 
 
 
 
 
 
f6d8b89
 
 
 
 
 
 
 
 
 
 
 
 
03d41a2
f6d8b89
 
 
 
 
 
 
 
 
 
 
 
c939804
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7990c94
 
c939804
7990c94
 
c939804
7990c94
 
 
c939804
7990c94
c939804
7990c94
 
 
 
 
 
183cdc5
7990c94
 
c939804
 
7990c94
c939804
 
7990c94
c939804
 
7990c94
c939804
 
 
 
 
 
 
7990c94
c939804
 
 
 
 
 
 
7990c94
c939804
 
 
 
 
 
 
 
7990c94
c939804
 
 
 
 
 
 
7990c94
c939804
 
7990c94
 
c939804
 
7990c94
 
c939804
 
 
 
 
 
 
 
 
7990c94
 
 
 
 
 
 
 
c939804
 
 
 
7990c94
c939804
 
183cdc5
c939804
 
1989b5e
c939804
 
 
 
 
 
 
 
 
 
 
7990c94
 
 
 
 
 
 
 
183cdc5
7990c94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c939804
7990c94
 
 
 
 
 
183cdc5
7990c94
 
1989b5e
7990c94
 
 
 
 
 
 
 
 
 
 
c939804
 
7990c94
 
 
 
 
c939804
7990c94
c939804
7990c94
 
 
 
c939804
 
 
 
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import numpy as np
import cv2
import matplotlib.pyplot as plt
import cvzone
import gradio as gr
from sklearn.cluster import KMeans
from PIL import Image, ImageFont, ImageDraw, ImageColor
import os
import math


import boto3
import os

AWS_S3_CREDS = {
    "aws_access_key_id":os.environ['AccessKey'],
    "aws_secret_access_key":os.environ['SecretKey']
}
s3_client = boto3.client('s3',**AWS_S3_CREDS)

def get_color(image):
  pixels = image.reshape(-1, 3)

  num_colors = 2

  kmeans = KMeans(n_clusters=num_colors)
  kmeans.fit(pixels)

  dominant_colors = kmeans.cluster_centers_.astype(int)

  theme_color = dominant_colors[0]

  return theme_color

base_color = (195, 212, 220)

def generate_shades(base_color, num_shades):
    r, g, b = base_color

    shades = []

    for i in range(num_shades):
        brightness = float(i) / (num_shades - 1)
        new_r = int(r * brightness)
        new_g = int(g * brightness)
        new_b = int(b * brightness)
        shades.append((new_r, new_g, new_b))
    return shades

def text_to_image(
    text: str,
    font_filepath: str,
    font_size: int,
    color: (int, int, int),
    font_align="center"
):
    font = ImageFont.truetype(font_filepath, size=font_size)
    draw = ImageDraw.Draw(Image.new("RGBA", (1, 1), (0, 0, 0, 0)))
    
    bbox = draw.multiline_textbbox((0, 0), text, font=font, align=font_align)
    width = math.ceil(bbox[2] - bbox[0])
    height = math.ceil(bbox[3] - bbox[1])
    
    img = Image.new("RGBA", (1000, 500), (0, 0, 0, 0))
    draw = ImageDraw.Draw(img)
    
    if font_align == "center":
        draw_point = ((width - (bbox[2] - bbox[0])) // 2, 0)
    elif font_align == "right":
        draw_point = (width - (bbox[2] - bbox[0]), 0)
    else:
        draw_point = (0, 0)
    
    draw.multiline_text(draw_point, text, font=font, fill=color, align=font_align)
    
    return img

def add_line_breaks(text, max_characters_per_line):
    lines = []
    current_line = ""

    for word in text.split():
        if len(current_line) + len(word) + 1 <= max_characters_per_line:
            if current_line:
                current_line += " "
            current_line += word
        else:
            lines.append(current_line)
            current_line = word

    if current_line:
        lines.append(current_line)

    return "\n".join(lines)
def hex_to_rgb(hex_code):
    hex_code = hex_code.lstrip('#')

    if not all(c in '0123456789ABCDEFabcdef' for c in hex_code):
        raise ValueError("Invalid hex code format")

    r = int(hex_code[0:2], 16)
    g = int(hex_code[2:4], 16)
    b = int(hex_code[4:6], 16)

    return (b, g, r)

def create_img1(imagep, text_overlay, color):

  s3_client.download_file('inpaintingnitin', 'D1/1.jpg', '1.jpg')
  s3_client.download_file('inpaintingnitin', 'D1/2.jpg', '2.jpg')
  s3_client.download_file('inpaintingnitin', 'D1/3.jpg', '3.jpg')
  s3_client.download_file('inpaintingnitin', 'D1/Screenshot 2023-08-25 at 2.30.38 PM.png', 'img.png')
  s3_client.download_file('inpaintingnitin', 'Poppins-Medium.ttf', 'Poppins-Medium.ttf')

  image_path = 'img.png'
  image = cv2.imread(image_path)

  mask1_path = '3.jpg'
  mask = cv2.imread(mask1_path, cv2.IMREAD_GRAYSCALE)

  foreground_image_path =imagep
  foreground_image = cv2.imread(foreground_image_path)

  prim_color = hex_to_rgb(color)
  color = generate_shades((prim_color[0],prim_color[1],prim_color[2]), 10)
  new_color = color[1]

  output_image = image.copy()

  for i in range(image.shape[0]):
      for j in range(image.shape[1]):
          if mask[i, j] > 150:
              output_image[i, j] = new_color

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, output_image)

  image = output_image

  mask2_path = '2.jpg'
  mask = cv2.imread(mask2_path, cv2.IMREAD_GRAYSCALE)

  new_color = color[8]

  output_image = image.copy()

  for i in range(image.shape[0]):
      for j in range(image.shape[1]):
          if mask[i, j] > 150:
              output_image[i, j] = new_color

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, output_image)

  background_image = output_image

  mask_path = '1.jpg'
  mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)

  width, height = background_image.shape[1], background_image.shape[0]
  white_image = np.ones((height, width, 3), dtype=np.uint8) * 255

  foreground_image = cv2.resize(foreground_image, (755, 890))
  x_offset = 1000
  y_offset = 50

  for y in range(foreground_image.shape[0]):
      for x in range(foreground_image.shape[1]):
          if x + x_offset < width and y + y_offset < height:
              white_image[y + y_offset, x + x_offset] = foreground_image[y, x]

  foreground_image = white_image
  mask = cv2.resize(mask, (background_image.shape[1], background_image.shape[0]))

  inv_mask = np.zeros_like(mask)

  for i in range(mask.shape[0]):
      for j in range(mask.shape[1]):
          if mask[i, j] <=150:
              inv_mask[i, j] = 255
          else:
              inv_mask[i, j] = 0

  overlay = cv2.bitwise_and(background_image, background_image, mask=inv_mask)
  overlay += cv2.bitwise_and(foreground_image, foreground_image, mask=mask)

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, overlay)

  img = text_to_image(add_line_breaks(text_overlay, 13),"Poppins-Medium.ttf",100,color[8] )
  img.save("outputtext.png", format="PNG")

  text = cv2.imread('outputtext.png',cv2.IMREAD_UNCHANGED)
  text = cv2.resize(text,(0,0),None,1,1)

  blue_channel, green_channel, red_channel, alpha_channel = cv2.split(text)

  inverted_image = cv2.merge([red_channel, green_channel, blue_channel, alpha_channel])

  final_img = cvzone.overlayPNG(overlay,inverted_image,[180,400])

  blue_channel, green_channel, red_channel = cv2.split(final_img)

  inverted_image = cv2.merge([red_channel, green_channel, blue_channel])
  return inverted_image

def create_img2(imagep, text_overlay, color):

  s3_client.download_file('inpaintingnitin', 'D2/1.jpg', '1.jpg')
  s3_client.download_file('inpaintingnitin', 'D2/2.jpg', '2.jpg')
  s3_client.download_file('inpaintingnitin', 'D2/3.jpg', '3.jpg')
  s3_client.download_file('inpaintingnitin', 'D2/Screenshot 2023-08-25 at 2.32.07 PM copy.jpg', 'img.jpg')
  s3_client.download_file('inpaintingnitin', 'Poppins-Medium.ttf', 'Poppins-Medium.ttf')


  image_path = 'img.jpg'
  image = cv2.imread(image_path)

  mask1_path = '1.jpg'
  mask = cv2.imread(mask1_path, cv2.IMREAD_GRAYSCALE)

  foreground_image_path =imagep
  foreground_image = cv2.imread(foreground_image_path)

  prim_color = hex_to_rgb(color)
  color = generate_shades((prim_color[0],prim_color[1],prim_color[2]), 10)
  new_color = color[1]

  output_image = image.copy()

  for i in range(image.shape[0]):
      for j in range(image.shape[1]):
          if mask[i, j] > 150:
              output_image[i, j] = new_color

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, output_image)

  image = output_image

  mask2_path = '2.jpg'
  mask = cv2.imread(mask2_path, cv2.IMREAD_GRAYSCALE)

  new_color = color[8]

  output_image = image.copy()

  for i in range(image.shape[0]):
      for j in range(image.shape[1]):
          if mask[i, j] > 150:
              output_image[i, j] = new_color

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, output_image)

  background_image = output_image

  mask_path = '3.jpg'
  mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)

  width, height = background_image.shape[1], background_image.shape[0]
  white_image = np.ones((height, width, 3), dtype=np.uint8) * 255

  foreground_image = cv2.resize(foreground_image, (720, 745))
  x_offset = 125
  y_offset = 123

  for y in range(foreground_image.shape[0]):
      for x in range(foreground_image.shape[1]):
          if x + x_offset < width and y + y_offset < height:
              white_image[y + y_offset, x + x_offset] = foreground_image[y, x]

  foreground_image = white_image
  mask = cv2.resize(mask, (background_image.shape[1], background_image.shape[0]))

  inv_mask = np.zeros_like(mask)

  for i in range(mask.shape[0]):
      for j in range(mask.shape[1]):
          if mask[i, j] <=150:
              inv_mask[i, j] = 255
          else:
              inv_mask[i, j] = 0

  overlay = cv2.bitwise_and(background_image, background_image, mask=inv_mask)
  overlay += cv2.bitwise_and(foreground_image, foreground_image, mask=mask)

  output_image_path = 'output_image.jpg'
  cv2.imwrite(output_image_path, overlay)

  img = text_to_image(add_line_breaks(text_overlay, 13),"Poppins-Medium.ttf",100,color[1] )
  img.save("outputtext.png", format="PNG")

  text = cv2.imread('outputtext.png',cv2.IMREAD_UNCHANGED)
  text = cv2.resize(text,(0,0),None,1,1)

  blue_channel, green_channel, red_channel, alpha_channel = cv2.split(text)

  inverted_image = cv2.merge([red_channel, green_channel, blue_channel, alpha_channel])

  final_img = cvzone.overlayPNG(overlay,inverted_image,[980,400])

  blue_channel, green_channel, red_channel = cv2.split(final_img)

  inverted_image = cv2.merge([red_channel, green_channel, blue_channel])
  return inverted_image

def exec(image, text, color):
  image1 = create_img1(image, text, color)
  image2 = create_img2(image, text, color)
  return image1,image2

interface = gr.Interface(
    fn=exec,
    inputs=[gr.Image(type='filepath'),
            gr.Textbox(label = 'Text'),
            gr.ColorPicker(label="Theme Color"),],
    outputs=["image","image"],
    title="Please use square images for best results",
    allow_flagging='never',
    theme="default",
    cache_examples=False,
    ).launch( debug=True, share=False)