Update app.py
Browse files
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()
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 ---
|