File size: 1,972 Bytes
48b3884
 
 
 
b5e2348
48b3884
 
 
c2a4f2d
48b3884
6515a1e
9a1f285
6515a1e
 
48b3884
 
c2a4f2d
9a1f285
6515a1e
c2a4f2d
 
6515a1e
c2a4f2d
9a1f285
c2a4f2d
 
 
aef7d82
c2a4f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
9a1f285
 
c2a4f2d
 
 
 
9a1f285
 
 
 
6515a1e
9a1f285
6515a1e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# app/image_utils.py

from PIL import Image, ImageDraw, ImageFont
import numpy as np
from pathlib import Path

def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
    """
    Adds a large, prominent, and consistently sized text banner to the top of an image.
    """
    image = Image.fromarray(image_array).convert("RGBA")
    
    txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
    draw = ImageDraw.Draw(txt_overlay)

    is_pneumonia = (text == "PNEUMONIA")
    banner_color = (220, 53, 69, 215) if is_pneumonia else (25, 135, 84, 215)
    text_color = (255, 255, 255)
    
    # --- SIZING FIX: Banner is 1/7th of the image height ---
    banner_height = int(image.height / 7)
    
    # --- FONT FIX: Start large and shrink to fit ---
    font_path = Path(__file__).parent / "Roboto-Bold.ttf"
    font_size = int(banner_height * 0.75) # Start with a large font
    
    text_to_draw = f"{text} | {confidence:.1%}"
    
    # Shrink font until the text fits within 90% of the image width
    while True:
        try:
            font = ImageFont.truetype(str(font_path), font_size)
            text_width = font.getlength(text_to_draw)
            if text_width < image.width * 0.9:
                break
            font_size -= 2
        except IOError:
            print(f"Font at '{font_path}' not found, using default.")
            font = ImageFont.load_default()
            break # Exit loop if font fails
            
    # Draw banner
    draw.rectangle([0, 0, image.width, banner_height], fill=banner_color)

    # Center the text within the banner
    text_height = font.getbbox(text_to_draw)[3]
    text_x = (image.width - text_width) / 2
    text_y = (banner_height - text_height) / 2
    
    # Draw text
    draw.text((text_x, text_y), text_to_draw, font=font, fill=text_color)
    
    watermarked_image = Image.alpha_composite(image, txt_overlay)
    
    return watermarked_image.convert("RGB")