@@ -43,42 +43,51 @@ def calculate_corners(img_w, img_h, text_bbox, margin) -> list:
43
43
return corners
44
44
45
45
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
60
63
61
64
def draw_corner_watermark (
62
65
image_bytes : BytesIO ,
63
66
text : str ,
64
67
font_family : str = "Gidole-Regular.ttf" ,
68
+ font_path : str = "static" ,
65
69
margin : int = 24
66
70
) -> Image :
71
+
67
72
with Image .open (image_bytes ).convert ("RGBA" ) as base :
68
73
txt = Image .new ("RGBA" , base .size , (255 , 255 , 255 , 0 ))
69
74
70
75
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 )
76
86
# calculate size of textbox
77
- text_bbox = d .textbbox ((0 , 0 ), text , font = fnt )
87
+ text_bbox = d .textbbox ((0 , 0 ), text , font = font )
78
88
# choose a random corner for the text
79
89
corners = calculate_corners (img_w = base .size [0 ], img_h = base .size [1 ], text_bbox = text_bbox , margin = margin )
80
90
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))
82
91
# average brightness of pixel check and switch between black/white
83
92
base_brightness = sum (base .getpixel (text_position )[:3 ]) / 3
84
93
text_colour = select_wm_colour (base_brightness )
@@ -97,26 +106,26 @@ def add_watermark(image_content: bytes) -> BytesIO | None:
97
106
image = draw_corner_watermark (
98
107
image_bytes ,
99
108
text = '@ffmemesbot' ,
100
- text_size = 18 ,
109
+ # text_size=18,
101
110
margin = 20
102
111
)
103
112
except Exception as e :
104
113
print (f'Error while adding watermark: { e } ' )
105
114
return None
106
115
107
116
buff = BytesIO ()
108
- buff .name = 'image_x .jpeg'
117
+ buff .name = 'image .jpeg'
109
118
image .save (buff , 'JPEG' )
110
119
buff .seek (0 )
111
120
112
- return buff #image
121
+ return buff
113
122
114
- # if __name__ == '__main__':
123
+ if __name__ == '__main__' :
115
124
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' )
122
131
0 commit comments