gaur3009 commited on
Commit
04baf73
·
verified ·
1 Parent(s): 1071bed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -4,20 +4,24 @@ from PIL import Image
4
 
5
  def overlay_images(x, y, scale, design_img, target_img):
6
  # Convert inputs to PIL images
7
- design_image = Image.fromarray(design_img)
8
- target_image = Image.fromarray(target_img)
9
 
10
  # Resize the design image based on scale
11
  design_image = design_image.resize(
12
  (int(design_image.width * scale), int(design_image.height * scale)), Image.Resampling.LANCZOS
13
  )
14
 
 
 
 
 
15
  # Create a transparent overlay
16
  overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
17
  overlay.paste(design_image, (x, y), design_image)
18
-
19
  # Combine overlay with target image
20
- combined = Image.alpha_composite(target_image.convert('RGBA'), overlay)
21
 
22
  return np.array(combined)
23
 
 
4
 
5
  def overlay_images(x, y, scale, design_img, target_img):
6
  # Convert inputs to PIL images
7
+ design_image = Image.fromarray(design_img).convert("RGBA")
8
+ target_image = Image.fromarray(target_img).convert("RGBA")
9
 
10
  # Resize the design image based on scale
11
  design_image = design_image.resize(
12
  (int(design_image.width * scale), int(design_image.height * scale)), Image.Resampling.LANCZOS
13
  )
14
 
15
+ # Ensure the design image has an alpha channel for transparency
16
+ if design_image.mode != 'RGBA':
17
+ design_image = design_image.convert('RGBA')
18
+
19
  # Create a transparent overlay
20
  overlay = Image.new('RGBA', target_image.size, (255, 255, 255, 0))
21
  overlay.paste(design_image, (x, y), design_image)
22
+
23
  # Combine overlay with target image
24
+ combined = Image.alpha_composite(target_image, overlay)
25
 
26
  return np.array(combined)
27