enesbol commited on
Commit
f1fe5f2
·
1 Parent(s): 744e1ac

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +9 -8
handler.py CHANGED
@@ -3,7 +3,7 @@ from diffusers import StableDiffusionInstructPix2PixPipeline, EulerAncestralDisc
3
  import base64
4
  from io import BytesIO
5
  from PIL import Image
6
- from scipy.spatial import KDTree
7
  from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb, hex_to_name
8
  import cv2
9
 
@@ -71,6 +71,7 @@ class EndpointHandler():
71
  torch.manual_seed(seed)
72
  images = self.pipe(result_prompt, negative_prompt = negative_prompt, image=raw_images, num_inference_steps=num_inference_steps, guidance_scale = guidance_scale, image_guidance_scale = image_guidance_scale).images
73
  img=images[0]
 
74
  img.save("./1.png")
75
  logo.save("./logo.png")
76
 
@@ -133,7 +134,7 @@ class EndpointHandler():
133
 
134
  # Helpers.
135
 
136
- def draw_text(img, text, font=cv2.FONT_HERSHEY_PLAIN, pos=(20, 45), font_scale=1, font_thickness=1, text_color=(0, 0, 255)):
137
  x, y = pos
138
  text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
139
  text_w, text_h = text_size
@@ -141,7 +142,7 @@ class EndpointHandler():
141
  return text_size
142
 
143
 
144
- def smooth_corners(image, radius, alpha=255):
145
  factor = 5 # Factor to increase the image size for anti-aliasing
146
  radius = radius * factor
147
  size = (image.width, image.height)
@@ -175,7 +176,7 @@ class EndpointHandler():
175
 
176
  return result_image
177
 
178
- def add_logo_to_image(base_image_path, logo_path, corner_radius=30):
179
 
180
  base_image = Image.open(base_image_path)
181
  smoothed_image = smooth_corners(base_image, radius=30)
@@ -230,7 +231,7 @@ class EndpointHandler():
230
  return result_logo_image
231
 
232
 
233
- def create_template(logo_and_image, punchline_text, punchline_text_color, punchline_text_max_width, spacing_image_text):
234
 
235
  result_logo_image_resized = logo_and_image.resize((325, 425))
236
  # Create a white background with a size of 645x645
@@ -292,7 +293,7 @@ class EndpointHandler():
292
 
293
  return template, punchline_text_height, len(lines)
294
 
295
- def draw_button_with_text(button_color, button_text, button_font=cv2.FONT_HERSHEY_TRIPLEX, button_font_scale = 0.7, button_font_thickness=1, button_text_color=(0, 0, 255)):
296
  # Create a blank 300x80 white image (background)
297
  background_image = Image.new("RGB", (300, 80), "white")
298
  button_image_pil = smooth_corners(background_image, 20, alpha=255)
@@ -313,7 +314,7 @@ class EndpointHandler():
313
 
314
  return button_image_pil
315
 
316
- def concat_template_with_button(template, button, line_count, spacing_between_punchline_and_button, punchline_text_height):
317
 
318
  result_template = template.copy()
319
 
@@ -329,7 +330,7 @@ class EndpointHandler():
329
 
330
  return result_template
331
 
332
- def create_image_template(
333
  base_image_path,
334
  logo_path,
335
  punchline_text,
 
3
  import base64
4
  from io import BytesIO
5
  from PIL import Image
6
+ from scipy.spatial import KDT
7
  from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb, hex_to_name
8
  import cv2
9
 
 
71
  torch.manual_seed(seed)
72
  images = self.pipe(result_prompt, negative_prompt = negative_prompt, image=raw_images, num_inference_steps=num_inference_steps, guidance_scale = guidance_scale, image_guidance_scale = image_guidance_scale).images
73
  img=images[0]
74
+
75
  img.save("./1.png")
76
  logo.save("./logo.png")
77
 
 
134
 
135
  # Helpers.
136
 
137
+ def draw_text(self, img, text, font=cv2.FONT_HERSHEY_PLAIN, pos=(20, 45), font_scale=1, font_thickness=1, text_color=(0, 0, 255)):
138
  x, y = pos
139
  text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
140
  text_w, text_h = text_size
 
142
  return text_size
143
 
144
 
145
+ def smooth_corners(self, image, radius, alpha=255):
146
  factor = 5 # Factor to increase the image size for anti-aliasing
147
  radius = radius * factor
148
  size = (image.width, image.height)
 
176
 
177
  return result_image
178
 
179
+ def add_logo_to_image(self, base_image_path, logo_path, corner_radius=30):
180
 
181
  base_image = Image.open(base_image_path)
182
  smoothed_image = smooth_corners(base_image, radius=30)
 
231
  return result_logo_image
232
 
233
 
234
+ def create_template(self, logo_and_image, punchline_text, punchline_text_color, punchline_text_max_width, spacing_image_text):
235
 
236
  result_logo_image_resized = logo_and_image.resize((325, 425))
237
  # Create a white background with a size of 645x645
 
293
 
294
  return template, punchline_text_height, len(lines)
295
 
296
+ def draw_button_with_text(self, button_color, button_text, button_font=cv2.FONT_HERSHEY_TRIPLEX, button_font_scale = 0.7, button_font_thickness=1, button_text_color=(0, 0, 255)):
297
  # Create a blank 300x80 white image (background)
298
  background_image = Image.new("RGB", (300, 80), "white")
299
  button_image_pil = smooth_corners(background_image, 20, alpha=255)
 
314
 
315
  return button_image_pil
316
 
317
+ def concat_template_with_button(self, template, button, line_count, spacing_between_punchline_and_button, punchline_text_height):
318
 
319
  result_template = template.copy()
320
 
 
330
 
331
  return result_template
332
 
333
+ def create_image_template(self,
334
  base_image_path,
335
  logo_path,
336
  punchline_text,