ALYYAN commited on
Commit
aef7d82
·
unverified ·
1 Parent(s): a84b21c

Update image_utils.py

Browse files
Files changed (1) hide show
  1. app/image_utils.py +36 -31
app/image_utils.py CHANGED
@@ -12,51 +12,56 @@ def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Imag
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")
 
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, 210) if is_pneumonia else (25, 135, 84, 210) # Red/Green with high opacity
18
  text_color = (255, 255, 255, 255)
19
  shadow_color = (0, 0, 0, 180)
20
 
21
+ # --- FONT FIX: Use a reliable font path for Linux/HF Spaces ---
22
+ font_size = int(image.height / 7) # Large font size
23
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
24
+ try:
25
+ font = ImageFont.truetype(font_path, font_size)
26
+ except IOError:
27
+ print(f"Font at '{font_path}' not found, falling back to default.")
28
+ font = ImageFont.load_default() # Fallback
 
 
 
 
 
29
 
30
+ # Text to draw
31
  text_to_draw = f"{text}\n{confidence:.1%}"
32
+
33
+ # --- POSITIONING: Center the watermark ---
34
  try:
35
+ # getbbox is the modern, accurate way to measure text
36
+ _, top, _, bottom = draw.textbbox((0, 0), text_to_draw, font=font, align="center")
37
  text_height = bottom - top
38
  except Exception:
39
+ text_height = font_size * 2.2 # Estimate height if getbbox fails
40
+
41
+ # Position the entire block at the top with a margin
42
+ y_start = image.height * 0.05
43
 
44
+ # Draw background box across the full width
45
+ draw.rectangle([0, y_start, image.width, y_start + text_height + 20], fill=box_color)
46
 
47
+ # Draw each line of text centered horizontally
48
+ y = y_start + 10
49
+ for i, line in enumerate(text_to_draw.split('\n')):
50
  try:
51
+ _, _, line_width, _ = draw.textbbox((0,0), line, font=font)
52
  except Exception:
53
+ line_width = len(line) * font_size / 2 # Fallback
54
 
55
+ x = (image.width - line_width) / 2
56
+
57
+ # Draw shadow for contrast
58
+ draw.text((x+3, y+3), line, font=font, fill=shadow_color, align="center")
59
+ # Draw main text
60
+ draw.text((x, y), line, font=font, fill=text_color, align="center")
61
+
62
+ # Adjust y position for the next line
63
+ y += font_size * 1.1 if i == 0 else font_size
64
 
65
  watermarked_image = Image.alpha_composite(image, txt_overlay)
66
+
67
  return watermarked_image.convert("RGB")