Skip to content

Commit 67b3d8f

Browse files
feat(images): auto-scale watermark font and padding based on image width
- Font size: ~1% of image width (min 24px) - Padding: ~0.5% of image width (min 15px) - Ensures consistent watermark proportions across different image sizes
1 parent 958117f commit 67b3d8f

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

core/images.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,20 @@ def create_thumbnail(input_path: str | Path, output_path: str | Path, width: int
7777

7878

7979
def add_watermark(
80-
input_path: str | Path, output_path: str | Path, spec_id: str | None = None, font_size: int = 48, padding: int = 25
80+
input_path: str | Path, output_path: str | Path, spec_id: str | None = None, font_size: int | None = None, padding: int | None = None
8181
) -> None:
8282
"""Add pyplots.ai branded watermark to an image.
8383
8484
Adds pyplots.ai (in brand colors) to bottom-right and spec_id to bottom-left.
8585
Uses JetBrains Mono Bold font with gray shadow for readability.
86+
Font size and padding scale automatically based on image width.
8687
8788
Args:
8889
input_path: Path to the source image.
8990
output_path: Path where the watermarked image will be saved.
9091
spec_id: Spec ID for bottom-left corner (e.g., "scatter-basic").
91-
font_size: Size of the watermark font in pixels.
92-
padding: Padding from the image edge in pixels.
92+
font_size: Size of the watermark font in pixels. If None, auto-scales (~1% of width).
93+
padding: Padding from the image edge in pixels. If None, auto-scales (~0.5% of width).
9394
9495
Raises:
9596
FileNotFoundError: If input_path does not exist.
@@ -99,6 +100,12 @@ def add_watermark(
99100
overlay = Image.new("RGBA", img.size, (0, 0, 0, 0))
100101
draw = ImageDraw.Draw(overlay)
101102

103+
# Auto-scale font size and padding based on image width
104+
if font_size is None:
105+
font_size = max(24, int(img.width * 0.01)) # ~1% of width, min 24px
106+
if padding is None:
107+
padding = max(15, int(img.width * 0.005)) # ~0.5% of width, min 15px
108+
102109
font = _get_font(font_size)
103110
alpha = int(255 * 0.95)
104111

0 commit comments

Comments
 (0)