August 3, 2023

Функция для добавления водяного знака на фото

def add_watermark_to_image(image_bytes, watermark_path, margin: int = 0, scale: int = 0.15):
 image = Image.open(BytesIO(image_bytes))
 
 watermark = Image.open(watermark_path)
 
 img_width, img_height = image.size
 
 wm_width, wm_height = watermark.size
 new_wm_width = int(img_width * scale)
 new_wm_height = int(wm_height * (new_wm_width / wm_width))
 watermark = watermark.resize((new_wm_width, new_wm_height), Image.LANCZOS)
 
 watermark_pos = (img_width - new_wm_width - margin, img_height - new_wm_height - margin)
 
 image.paste(watermark, watermark_pos, watermark)
 
 output_image_bytes = BytesIO()
 image.save(output_image_bytes, format="JPEG")
 output_image_bytes.seek(0)
 
 return output_image_bytes.getvalue()
https://t.me/PyDevPro