Spaces:
Sleeping
Sleeping
Update image_utils.py
Browse files- app/image_utils.py +35 -41
app/image_utils.py
CHANGED
|
@@ -5,64 +5,58 @@ import numpy as np
|
|
| 5 |
|
| 6 |
def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
|
| 7 |
"""
|
| 8 |
-
Adds a large, prominent, and
|
| 9 |
"""
|
| 10 |
image = Image.fromarray(image_array).convert("RGBA")
|
| 11 |
|
| 12 |
txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
|
| 13 |
draw = ImageDraw.Draw(txt_overlay)
|
| 14 |
|
| 15 |
-
# Define watermark properties
|
| 16 |
is_pneumonia = (text == "PNEUMONIA")
|
| 17 |
-
box_color = (220, 53, 69,
|
| 18 |
text_color = (255, 255, 255, 255)
|
|
|
|
| 19 |
|
| 20 |
-
# --- FONT FIX:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
font = ImageFont.truetype(font_path, font_size)
|
| 26 |
-
except IOError:
|
| 27 |
-
print(f"Font '{font_path}' not found, using default. Watermark quality may be lower.")
|
| 28 |
-
# Fallback if font is not on the system
|
| 29 |
-
font_size = int(image.height / 7)
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
except IOError:
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
# Define text
|
| 37 |
text_to_draw = f"{text}\n{confidence:.1%}"
|
| 38 |
-
|
| 39 |
-
# --- POSITIONING FIX: Center the watermark ---
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
left, top, right, bottom = draw.textbbox((0, 0), text_to_draw, font=font)
|
| 43 |
-
text_width = right - left
|
| 44 |
text_height = bottom - top
|
| 45 |
-
except
|
| 46 |
-
|
| 47 |
-
text_width, text_height = draw.textsize(text_to_draw, font=font)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
y = (image.height - text_height) / 2
|
| 51 |
-
position = (x, y)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
box_position = [
|
| 56 |
-
x - padding,
|
| 57 |
-
y - padding,
|
| 58 |
-
x + text_width + padding,
|
| 59 |
-
y + text_height + padding
|
| 60 |
-
]
|
| 61 |
-
|
| 62 |
-
# Draw the rectangle and the text
|
| 63 |
-
draw.rectangle(box_position, fill=box_color)
|
| 64 |
-
draw.text(position, text_to_draw, font=font, fill=text_color, align="center")
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
| 68 |
return watermarked_image.convert("RGB")
|
|
|
|
| 5 |
|
| 6 |
def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
|
| 7 |
"""
|
| 8 |
+
Adds a large, prominent, and highly readable watermark to an image.
|
| 9 |
"""
|
| 10 |
image = Image.fromarray(image_array).convert("RGBA")
|
| 11 |
|
| 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: Try multiple common system fonts ---
|
| 21 |
+
font_size = int(image.height / 6) # Even larger font size
|
| 22 |
+
font_paths = ["/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "arialbd.ttf", "/System/Library/Fonts/Arial Bold.ttf"]
|
| 23 |
+
font = None
|
| 24 |
+
for path in font_paths:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
font = ImageFont.truetype(path, font_size)
|
| 27 |
+
break
|
| 28 |
except IOError:
|
| 29 |
+
continue
|
| 30 |
+
if not font:
|
| 31 |
+
print("No suitable bold font found, using default. Watermark quality will be lower.")
|
| 32 |
+
font = ImageFont.load_default()
|
| 33 |
|
| 34 |
+
# Define text and calculate its position
|
| 35 |
text_to_draw = f"{text}\n{confidence:.1%}"
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
+
_, top, _, bottom = draw.textbbox((0, 0), text_to_draw, font=font)
|
|
|
|
|
|
|
| 38 |
text_height = bottom - top
|
| 39 |
+
except Exception:
|
| 40 |
+
text_height = font_size * 2 # Fallback
|
|
|
|
| 41 |
|
| 42 |
+
y = 15
|
|
|
|
|
|
|
| 43 |
|
| 44 |
+
# Draw background box
|
| 45 |
+
draw.rectangle([0, y - 10, image.width, y + text_height + 10], fill=box_color)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
# Draw text with shadow for readability
|
| 48 |
+
for line in text_to_draw.split('\n'):
|
| 49 |
+
try:
|
| 50 |
+
_, _, text_width, _ = draw.textbbox((0,0), line, font=font)
|
| 51 |
+
except Exception:
|
| 52 |
+
text_width = len(line) * font_size / 2 # Fallback
|
| 53 |
+
|
| 54 |
+
x = (image.width - text_width) / 2
|
| 55 |
+
# Draw shadow
|
| 56 |
+
draw.text((x+2, y+2), line, font=font, fill=shadow_color)
|
| 57 |
+
# Draw text
|
| 58 |
+
draw.text((x, y), line, font=font, fill=text_color)
|
| 59 |
+
y += text_height / 2 + 5
|
| 60 |
|
| 61 |
+
watermarked_image = Image.alpha_composite(image, txt_overlay)
|
| 62 |
return watermarked_image.convert("RGB")
|