Skip to content

Commit 7b42aac

Browse files
committed
upd watermark
1 parent b88bf72 commit 7b42aac

File tree

6 files changed

+40
-31
lines changed

6 files changed

+40
-31
lines changed

image_2.jpg

252 KB
Loading

image_3x.jpg

6.01 KB
Loading

src/storage/watermark.py

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,51 @@ def calculate_corners(img_w, img_h, text_bbox, margin) -> list:
4343
return corners
4444

4545
def check_font_size(text, font_path, image, width_ratio):
46-
breakpoint = width_ratio * image.size[0]
47-
fontsize = 20
48-
learning_rate = 5
49-
font = ImageFont.truetype(font_path, fontsize)
50-
while True:
51-
if font.getlength(text) < breakpoint:
52-
fontsize += learning_rate
53-
else:
54-
learning_rate = learning_rate // 2
55-
fontsize -= learning_rate
56-
font = ImageFont.truetype(font_path, fontsize)
57-
if learning_rate <= 1:
58-
break
59-
return font
46+
# breakpoint = width_ratio * image.size[0]
47+
# fontsize = 20
48+
# learning_rate = 5
49+
fontsize = int(image.size[0] * width_ratio)
50+
print(fontsize)
51+
fontsize = ImageFont.truetype(font_path, fontsize)
52+
53+
# while True:
54+
# if font.getlength(text) < breakpoint:
55+
# fontsize += learning_rate
56+
# else:
57+
# learning_rate = learning_rate // 2
58+
# fontsize -= learning_rate
59+
# font = ImageFont.truetype(font_path, fontsize)
60+
# if learning_rate <= 1:
61+
# break
62+
return fontsize
6063

6164
def draw_corner_watermark(
6265
image_bytes: BytesIO,
6366
text: str,
6467
font_family: str = "Gidole-Regular.ttf",
68+
font_path: str = "static",
6569
margin: int = 24
6670
) -> Image:
71+
6772
with Image.open(image_bytes).convert("RGBA") as base:
6873
txt = Image.new("RGBA", base.size, (255, 255, 255, 0))
6974

7075
d = ImageDraw.Draw(txt)
71-
72-
width_ratio = .15 # Portion of the image the text width should be (between 0 and 1)
73-
font_path = str(Path(Path.home(), "src", "fonts", font_family))
74-
fnt = check_font_size(text, font_path, base, width_ratio)
75-
# fnt = ImageFont.load_default()
76+
# Portion of the image the text width should be (between 0 and 1)
77+
width_ratio = .1
78+
# font_path = str(Path(Path.home(), "static", "fonts", font_family))
79+
# font_files_dir = Path(__file__).parent.parent
80+
localization_files_dir = Path(__file__).parent.parent / "static/localization"
81+
print(localization_files_dir, font)
82+
with open(Path(localization_files_dir, "fonts", font_family), "r") as f:
83+
font_path = f
84+
fntsize = check_font_size(text, font_path, base, width_ratio)
85+
font = ImageFont.truetype(font_path, fntsize)
7686
# calculate size of textbox
77-
text_bbox = d.textbbox((0, 0), text, font=fnt)
87+
text_bbox = d.textbbox((0, 0), text, font=font)
7888
# choose a random corner for the text
7989
corners = calculate_corners(img_w=base.size[0], img_h=base.size[1], text_bbox=text_bbox, margin=margin)
8090
text_position = random.choice(corners)
81-
# text_position = choice(calculate_corners(img_w=base.size[0], img_h=base.size[1], text_bbox=text_bbox, margin=margin))
8291
# average brightness of pixel check and switch between black/white
8392
base_brightness = sum(base.getpixel(text_position)[:3]) / 3
8493
text_colour = select_wm_colour(base_brightness)
@@ -97,26 +106,26 @@ def add_watermark(image_content: bytes) -> BytesIO | None:
97106
image = draw_corner_watermark(
98107
image_bytes,
99108
text='@ffmemesbot',
100-
text_size=18,
109+
# text_size=18,
101110
margin=20
102111
)
103112
except Exception as e:
104113
print(f'Error while adding watermark: {e}')
105114
return None
106115

107116
buff = BytesIO()
108-
buff.name = 'image_x.jpeg'
117+
buff.name = 'image.jpeg'
109118
image.save(buff, 'JPEG')
110119
buff.seek(0)
111120

112-
return buff#image
121+
return buff
113122

114-
# if __name__ == '__main__':
123+
if __name__ == '__main__':
115124

116-
# # image_path = Path(Path.home(), "src", "test1.jpeg") # Adjust the path if necessary
117-
# image_path = Path(Path.home(), "src", "test1.jpeg") # Adjust the path if necessary
118-
# with open(image_path, 'rb') as image_file:
119-
# image_bytes = image_file.read()
120-
# watermarked_image = add_watermark(image_bytes)
121-
# watermarked_image.save('/src/image_3x.jpg')
125+
# image_path = Path(Path.home(), "src", "test1.jpeg") # Adjust the path if necessary
126+
image_path = Path(Path.home(), "src", "test2.jpeg") # Adjust the path if necessary
127+
with open(image_path, 'rb') as image_file:
128+
image_bytes = image_file.read()
129+
watermarked_image = add_watermark(image_bytes)
130+
watermarked_image.save('/src/image_Xx.jpg')
122131

src/test1.jpeg

5.16 KB
Loading

src/test2.jpeg

60.4 KB
Loading

0 commit comments

Comments
 (0)