faranbutt789 commited on
Commit
efb54b9
·
verified ·
1 Parent(s): 4082a30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -99,16 +99,25 @@ INV_AGE_SCALE = 80 # training used age/80 normalization
99
 
100
 
101
  def draw_caption_on_image(image, caption):
 
 
102
  draw = ImageDraw.Draw(image)
103
- font = ImageFont.load_default() # Default font (always available)
104
 
105
  bbox = draw.textbbox((0,0), caption, font=font)
106
  text_w = bbox[2] - bbox[0]
107
  text_h = bbox[3] - bbox[1]
108
-
109
- draw.rectangle([0,0,text_w+20,text_h+20], fill=(0,0,0,127))
 
 
 
 
 
 
110
  draw.text((10,10), caption, font=font, fill="white")
111
- return image
 
112
 
113
 
114
  # --- Prediction function for multiple images ---
 
99
 
100
 
101
  def draw_caption_on_image(image, caption):
102
+ if image.mode != "RGBA":
103
+ image = image.convert("RGBA")
104
  draw = ImageDraw.Draw(image)
105
+ font = ImageFont.load_default()
106
 
107
  bbox = draw.textbbox((0,0), caption, font=font)
108
  text_w = bbox[2] - bbox[0]
109
  text_h = bbox[3] - bbox[1]
110
+
111
+ # semi-transparent rectangle
112
+ overlay = Image.new("RGBA", image.size)
113
+ overlay_draw = ImageDraw.Draw(overlay)
114
+ overlay_draw.rectangle([0,0,text_w+20,text_h+20], fill=(0,0,0,127))
115
+ image = Image.alpha_composite(image, overlay)
116
+
117
+ draw = ImageDraw.Draw(image)
118
  draw.text((10,10), caption, font=font, fill="white")
119
+ return image.convert("RGB")
120
+
121
 
122
 
123
  # --- Prediction function for multiple images ---