ALYYAN commited on
Commit
b5e2348
·
unverified ·
1 Parent(s): a658c43

Update image_utils.py

Browse files
Files changed (1) hide show
  1. app/image_utils.py +29 -43
app/image_utils.py CHANGED
@@ -2,66 +2,52 @@
2
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  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 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
- # 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")
 
2
 
3
  from PIL import Image, ImageDraw, ImageFont
4
  import numpy as np
5
+ 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 highly readable text overlay to an image.
10
  """
11
+ image = Image.fromarray(image_array).convert("RGB")
12
+ draw = ImageDraw.Draw(image)
 
 
13
 
 
14
  is_pneumonia = (text == "PNEUMONIA")
15
+ text_color = (220, 53, 69) if is_pneumonia else (25, 135, 84) # Solid Red or Green
16
+ shadow_color = (0, 0, 0)
17
+
18
+ # --- FONT FIX: Use a bundled font file for reliability ---
19
+ font_size = int(image.height / 6) # Make it very large
20
+ font_path = Path(__file__).parent / "Roboto-Bold.ttf" # Assumes font is in the same 'app' folder
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
+ # --- POSITIONING: Center the text block ---
32
  try:
33
+ left, top, right, bottom = draw.textbbox((0, 0), text_to_draw, font=font, align="center")
34
+ text_width = right - left
35
  text_height = bottom - top
36
  except Exception:
37
+ # Fallback for older Pillow
38
+ text_width, text_height = draw.textsize(text_to_draw, font=font)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Draw main text
51
+ draw.text((x, y), text_to_draw, font=font, fill=text_color, align="center")
52
 
53
+ return image