Spaces:
Sleeping
Sleeping
Update image_utils.py
Browse files- app/image_utils.py +37 -35
app/image_utils.py
CHANGED
|
@@ -6,48 +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("
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
is_pneumonia = (text == "PNEUMONIA")
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
# --- FONT FIX: Use a bundled font file for reliability ---
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
try:
|
| 23 |
-
font = ImageFont.truetype(str(font_path), font_size)
|
| 24 |
-
except IOError:
|
| 25 |
-
print(f"Font at '{font_path}' not found, falling back to default.")
|
| 26 |
-
font = ImageFont.load_default()
|
| 27 |
-
|
| 28 |
-
# Text to draw
|
| 29 |
text_to_draw = f"{text}\n{confidence:.1%}"
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
x = (image.width - text_width) / 2
|
| 41 |
-
y = (image.height - text_height) / 2
|
| 42 |
-
|
| 43 |
-
# Draw shadow/outline for contrast
|
| 44 |
-
offset = font_size // 20
|
| 45 |
-
draw.text((x-offset, y-offset), text_to_draw, font=font, fill=shadow_color, align="center")
|
| 46 |
-
draw.text((x+offset, y-offset), text_to_draw, font=font, fill=shadow_color, align="center")
|
| 47 |
-
draw.text((x-offset, y+offset), text_to_draw, font=font, fill=shadow_color, align="center")
|
| 48 |
-
draw.text((x+offset, y+offset), text_to_draw, font=font, fill=shadow_color, align="center")
|
| 49 |
|
| 50 |
-
#
|
| 51 |
-
draw.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
| 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 overlay to an image.
|
| 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 |
+
box_color = (220, 53, 69, 210) if is_pneumonia else (25, 135, 84, 210)
|
| 17 |
+
text_color = (255, 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: Make the box a fixed proportion of the image width ---
|
| 24 |
+
box_width = int(image.width * 0.4) # Box will be 40% of the image width
|
| 25 |
+
box_height = int(box_width * 0.4) # Maintain an aspect ratio for the box
|
| 26 |
+
|
| 27 |
+
# Start with a large font size and shrink it until the text fits in the box
|
| 28 |
+
font_size = int(box_height / 2.5)
|
| 29 |
+
font = ImageFont.truetype(str(font_path), font_size)
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
text_to_draw = f"{text}\n{confidence:.1%}"
|
| 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 |
+
# --- POSITIONING: Place box in the top-left corner ---
|
| 39 |
+
box_position = [10, 10, 10 + box_width, 10 + box_height]
|
| 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")
|