Spaces:
Sleeping
Sleeping
Update image_utils.py
Browse files- app/image_utils.py +26 -32
app/image_utils.py
CHANGED
|
@@ -6,56 +6,50 @@ from pathlib import Path
|
|
| 6 |
|
| 7 |
def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
|
| 8 |
"""
|
| 9 |
-
Adds a large, prominent, and
|
| 10 |
"""
|
| 11 |
image = Image.fromarray(image_array).convert("RGBA")
|
| 12 |
|
| 13 |
-
# Create a transparent overlay layer
|
| 14 |
txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
|
| 15 |
draw = ImageDraw.Draw(txt_overlay)
|
| 16 |
|
| 17 |
-
# --- DEFINE BANNER AND TEXT PROPERTIES ---
|
| 18 |
is_pneumonia = (text == "PNEUMONIA")
|
| 19 |
-
banner_color = (220, 53, 69,
|
| 20 |
text_color = (255, 255, 255)
|
| 21 |
|
| 22 |
-
# --- SIZING FIX: Banner
|
| 23 |
-
banner_height = int(image.height /
|
| 24 |
|
| 25 |
-
# --- FONT FIX:
|
| 26 |
font_path = Path(__file__).parent / "Roboto-Bold.ttf"
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
font = ImageFont.truetype(str(font_path), font_size)
|
| 31 |
-
except IOError:
|
| 32 |
-
print(f"Font at '{font_path}' not found, using default.")
|
| 33 |
-
font = ImageFont.load_default()
|
| 34 |
-
|
| 35 |
-
# Text to draw
|
| 36 |
-
text_to_draw = f"{text} | Confidence: {confidence:.1%}"
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
draw.rectangle([0, 0, image.width, banner_height], fill=banner_color)
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
text_w = font.getlength(text_to_draw)
|
| 46 |
-
text_h = bottom - top
|
| 47 |
-
except Exception:
|
| 48 |
-
# Fallback for older Pillow
|
| 49 |
-
text_w = font.getsize(text_to_draw)[0]
|
| 50 |
-
text_h = font.getsize(text_to_draw)[1]
|
| 51 |
-
|
| 52 |
-
text_x = (image.width - text_w) / 2
|
| 53 |
-
text_y = (banner_height - text_h) / 2
|
| 54 |
|
| 55 |
# Draw text
|
| 56 |
draw.text((text_x, text_y), text_to_draw, font=font, fill=text_color)
|
| 57 |
|
| 58 |
-
# Composite the overlay onto the original image
|
| 59 |
watermarked_image = Image.alpha_composite(image, txt_overlay)
|
| 60 |
|
| 61 |
return watermarked_image.convert("RGB")
|
|
|
|
| 6 |
|
| 7 |
def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
|
| 8 |
"""
|
| 9 |
+
Adds a large, prominent, and consistently sized text banner to the top of an image.
|
| 10 |
"""
|
| 11 |
image = Image.fromarray(image_array).convert("RGBA")
|
| 12 |
|
|
|
|
| 13 |
txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
|
| 14 |
draw = ImageDraw.Draw(txt_overlay)
|
| 15 |
|
|
|
|
| 16 |
is_pneumonia = (text == "PNEUMONIA")
|
| 17 |
+
banner_color = (220, 53, 69, 215) if is_pneumonia else (25, 135, 84, 215)
|
| 18 |
text_color = (255, 255, 255)
|
| 19 |
|
| 20 |
+
# --- SIZING FIX: Banner is 1/7th of the image height ---
|
| 21 |
+
banner_height = int(image.height / 7)
|
| 22 |
|
| 23 |
+
# --- FONT FIX: Start large and shrink to fit ---
|
| 24 |
font_path = Path(__file__).parent / "Roboto-Bold.ttf"
|
| 25 |
+
font_size = int(banner_height * 0.75) # Start with a large font
|
| 26 |
+
|
| 27 |
+
text_to_draw = f"{text} | {confidence:.1%}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Shrink font until the text fits within 90% of the image width
|
| 30 |
+
while True:
|
| 31 |
+
try:
|
| 32 |
+
font = ImageFont.truetype(str(font_path), font_size)
|
| 33 |
+
text_width = font.getlength(text_to_draw)
|
| 34 |
+
if text_width < image.width * 0.9:
|
| 35 |
+
break
|
| 36 |
+
font_size -= 2
|
| 37 |
+
except IOError:
|
| 38 |
+
print(f"Font at '{font_path}' not found, using default.")
|
| 39 |
+
font = ImageFont.load_default()
|
| 40 |
+
break # Exit loop if font fails
|
| 41 |
+
|
| 42 |
+
# Draw banner
|
| 43 |
draw.rectangle([0, 0, image.width, banner_height], fill=banner_color)
|
| 44 |
|
| 45 |
+
# Center the text within the banner
|
| 46 |
+
text_height = font.getbbox(text_to_draw)[3]
|
| 47 |
+
text_x = (image.width - text_width) / 2
|
| 48 |
+
text_y = (banner_height - text_height) / 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
# Draw text
|
| 51 |
draw.text((text_x, text_y), text_to_draw, font=font, fill=text_color)
|
| 52 |
|
|
|
|
| 53 |
watermarked_image = Image.alpha_composite(image, txt_overlay)
|
| 54 |
|
| 55 |
return watermarked_image.convert("RGB")
|