| 4 | |
| 5 | |
| 6 | class Watermark: |
| 7 | def __init__(self): |
| 8 | pass |
| 9 | |
| 10 | def add_text_watermark( |
| 11 | self, image, text, text_color, font_style, font_size, position=(0, 0) |
| 12 | ): |
| 13 | font = ImageFont.truetype(font_style, font_size) |
| 14 | draw = ImageDraw.Draw(image) |
| 15 | draw.text(position, text, fill=text_color, font=font) |
| 16 | return image |
| 17 | |
| 18 | def add_logo(self, image, logo, position=(0, 0)): |
| 19 | if logo.mode != "RGBA": |
| 20 | logo = logo.convert("RGBA") |
| 21 | if image.mode != "RGBA": |
| 22 | image = image.convert("RGBA") |
| 23 | |
| 24 | if (position[0] + logo.width > image.width) or ( |
| 25 | position[1] + logo.height > image.height |
| 26 | ): |
| 27 | CTkMessagebox(title="Logo position", message="Logo position out of bounds.") |
| 28 | |
| 29 | image.paste(logo, position, mask=logo) |
| 30 | return image |
| 31 | |
| 32 | def save_image(self, image): |
| 33 | save_path = filedialog.asksaveasfilename( |
| 34 | defaultextension="*.png", |
| 35 | title="Save as", |
| 36 | filetypes=[ |
| 37 | ("PNG files", "*.png"), |
| 38 | ("All files", "*.*"), |
| 39 | ], |
| 40 | ) |
| 41 | if save_path: |
| 42 | try: |
| 43 | image.save(save_path) |
| 44 | except Exception: |
| 45 | print("Failed to save image: {e}") |