Spaces:
Sleeping
Sleeping
Update image_utils.py
Browse files- app/image_utils.py +40 -34
app/image_utils.py
CHANGED
|
@@ -6,50 +6,56 @@ 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 |
txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
|
| 13 |
draw = ImageDraw.Draw(txt_overlay)
|
| 14 |
|
|
|
|
| 15 |
is_pneumonia = (text == "PNEUMONIA")
|
| 16 |
-
|
| 17 |
-
text_color = (255, 255, 255
|
| 18 |
-
shadow_color = (0, 0, 0, 180)
|
| 19 |
-
|
| 20 |
-
# --- FONT FIX: Use a bundled font file for reliability ---
|
| 21 |
-
font_path = Path(__file__).parent / "Roboto-Bold.ttf"
|
| 22 |
|
| 23 |
-
# --- SIZING FIX:
|
| 24 |
-
|
| 25 |
-
box_height = int(box_width * 0.4) # Maintain an aspect ratio for the box
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Adjust font size until text fits within the box width
|
| 34 |
-
while font.getbbox(text_to_draw, align="center")[2] > box_width * 0.9:
|
| 35 |
-
font_size -= 2
|
| 36 |
font = ImageFont.truetype(str(font_path), font_size)
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
draw.rectangle(box_position, fill=box_color)
|
| 41 |
-
|
| 42 |
-
# Center the text within the box
|
| 43 |
-
text_bbox = draw.textbbox((0, 0), text_to_draw, font=font, align="center")
|
| 44 |
-
text_width = text_bbox[2] - text_bbox[0]
|
| 45 |
-
text_height = text_bbox[3] - text_bbox[1]
|
| 46 |
-
|
| 47 |
-
text_x = box_position[0] + (box_width - text_width) / 2
|
| 48 |
-
text_y = box_position[1] + (box_height - text_height) / 2
|
| 49 |
-
|
| 50 |
-
# Draw text with shadow
|
| 51 |
-
draw.text((text_x + 2, text_y + 2), text_to_draw, font=font, fill=shadow_color, align="center")
|
| 52 |
-
draw.text((text_x, text_y), text_to_draw, font=font, fill=text_color, align="center")
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
watermarked_image = Image.alpha_composite(image, txt_overlay)
|
|
|
|
| 55 |
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 professional-looking text banner to the top of an image.
|
| 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, 210) if is_pneumonia else (25, 135, 84, 210) # Red/Green
|
| 20 |
+
text_color = (255, 255, 255)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# --- SIZING FIX: Banner height is 1/8th of the image height ---
|
| 23 |
+
banner_height = int(image.height / 8)
|
|
|
|
| 24 |
|
| 25 |
+
# --- FONT FIX: Use a bundled font and scale it to fit the banner ---
|
| 26 |
+
font_path = Path(__file__).parent / "Roboto-Bold.ttf"
|
| 27 |
+
try:
|
| 28 |
+
# Start with a font size that's a good proportion of the banner height
|
| 29 |
+
font_size = int(banner_height * 0.6)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# --- DRAW THE BANNER ---
|
| 39 |
+
draw.rectangle([0, 0, image.width, banner_height], fill=banner_color)
|
| 40 |
+
|
| 41 |
+
# --- POSITIONING: Center the text within the banner ---
|
| 42 |
+
try:
|
| 43 |
+
# getbbox is the modern, accurate way to measure text
|
| 44 |
+
_, top, _, bottom = draw.textbbox((0, 0), text_to_draw, font=font)
|
| 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")
|