ALYYAN commited on
Commit
6c03139
·
unverified ·
1 Parent(s): 4f56e85

Update image_utils.py

Browse files
Files changed (1) hide show
  1. app/image_utils.py +34 -31
app/image_utils.py CHANGED
@@ -5,61 +5,64 @@ import numpy as np
5
 
6
  def add_watermark(image_array: np.ndarray, text: str, confidence: float) -> Image.Image:
7
  """
8
- Adds a translucent watermark to an image with the prediction result and confidence.
9
-
10
- Args:
11
- image_array: The input image as a NumPy array.
12
- text: The prediction text (e.g., "NORMAL" or "PNEUMONIA").
13
- confidence: The confidence score of the prediction.
14
-
15
- Returns:
16
- A PIL Image object with the watermark applied.
17
  """
18
- # Convert NumPy array to PIL Image
19
  image = Image.fromarray(image_array).convert("RGBA")
20
 
21
- # Create a transparent overlay for the text
22
  txt_overlay = Image.new("RGBA", image.size, (255, 255, 255, 0))
23
  draw = ImageDraw.Draw(txt_overlay)
24
 
25
  # Define watermark properties
26
  is_pneumonia = (text == "PNEUMONIA")
27
- box_color = (220, 53, 69, 180) if is_pneumonia else (25, 135, 84, 180) # Red for Pneumonia, Green for Normal
28
  text_color = (255, 255, 255, 255)
29
 
30
- # Define font (uses a default if a specific .ttf is not found)
31
  try:
32
- font_size = int(image.height / 8)
33
- font = ImageFont.truetype("arialbd.ttf", font_size)
 
 
34
  except IOError:
35
- print("Arial Bold font not found, using default. Watermark quality may be lower.")
36
- font_size = int(image.height / 8)
37
- font = ImageFont.load_default()
 
 
 
 
 
38
 
39
- # Define text and box position
40
  text_to_draw = f"{text}\n{confidence:.1%}"
41
 
42
- # Get text size
43
  try:
44
- # Use getbbox for modern Pillow versions
45
- _, _, text_width, text_height = draw.textbbox((0, 0), text_to_draw, font=font)
 
 
46
  except AttributeError:
47
  # Fallback for older Pillow versions
48
  text_width, text_height = draw.textsize(text_to_draw, font=font)
49
 
50
- position = (20, 20) # Top-left corner with some padding
 
 
 
 
 
51
  box_position = [
52
- position[0] - 10,
53
- position[1] - 10,
54
- position[0] + text_width + 10,
55
- position[1] + text_height + 10
56
  ]
57
 
58
- # Draw the semi-transparent rectangle and the text
59
  draw.rectangle(box_position, fill=box_color)
60
- draw.text(position, text_to_draw, font=font, fill=text_color)
61
 
62
- # Combine the overlay with the original image
63
  watermarked_image = Image.alpha_composite(image, txt_overlay)
64
 
65
- 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 professional-looking 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, 200) if is_pneumonia else (25, 135, 84, 200) # Red/Green with higher opacity
18
  text_color = (255, 255, 255, 255)
19
 
20
+ # --- FONT FIX: Make font size proportional and larger ---
21
  try:
22
+ # On many systems (including HF Spaces), Arial is available.
23
+ font_path = "arialbd.ttf"
24
+ font_size = int(image.height / 7) # Made font significantly larger
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
+ # Try a generic sans-serif font
32
+ font = ImageFont.truetype("DejaVuSans-Bold.ttf", font_size)
33
+ except IOError:
34
+ font = ImageFont.load_default()
35
 
36
+ # Define text
37
  text_to_draw = f"{text}\n{confidence:.1%}"
38
 
39
+ # --- POSITIONING FIX: Center the watermark ---
40
  try:
41
+ # Use getbbox for modern Pillow versions to get precise text dimensions
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 AttributeError:
46
  # Fallback for older Pillow versions
47
  text_width, text_height = draw.textsize(text_to_draw, font=font)
48
 
49
+ x = (image.width - text_width) / 2
50
+ y = (image.height - text_height) / 2
51
+ position = (x, y)
52
+
53
+ # Add padding to the background box
54
+ padding = font_size / 4
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
  watermarked_image = Image.alpha_composite(image, txt_overlay)
67
 
68
+ return watermarked_image.convert("RGB")