Skip to content

Commit

Permalink
feat: add watermark command closes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
mdsanima committed Apr 26, 2023
1 parent b6884f6 commit ef714a6
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/commands.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Avaiable command for this package:
- `mdsanima uuid` rename image file to uuid
- `mdsanima number` rename image file to number
- `mdsanima logo` append a logo
- `mdsanima watermark` append a watermark

The `pixelart` command works in folder that have only `.png` images and convert this images to pixel
art with creating the new file and appending the suffix `pixelart` to original file name.
Expand Down
Binary file added img/watermark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/mdsanima_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from .cli_number import cli_number
from .cli_pixelart import cli_pixelart
from .cli_uuid import cli_uuid
from .cli_watermark import cli_watermark


def main_cli():
Expand All @@ -40,5 +41,7 @@ def main_cli():
cli_number()
if args.command == "logo":
cli_logo()
if args.command == "watermark":
cli_watermark()
except AttributeError:
parser.print_help()
119 changes: 119 additions & 0 deletions src/mdsanima_cli/cli_watermark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Copyright © 2023 Marcin Różewski MDSANIMA


"""This module is designed to adding a watermark to all images in the current
directory. It operates within a specified folder and can process all images at
once.
"""


from __future__ import annotations

import os
from PIL import Image

from .ascii import ascii_title
from .mprints import print_cli_data
from .mprints import print_cli_proc

from .cli_check import print_directory_check


WATERMARK = os.path.expanduser("~/.mdsanima-cli/config/watermark.png")


def append_watermark(image_path: str, waterm_path: str, new_name: str) -> None:
"""Append a watermark to one image, and then save the result with a new
file name. The watermark is a rotated on 45 degrees and shifted.
"""

# Open image and watermark file.
image = Image.open(image_path)
watermark = Image.open(waterm_path)

# Image and watermark size.
image_width, _image_height = image.size
watermark_width, watermark_height = watermark.size

# Calculating size for watermark.
resize_ratio = 0.25
new_watermark_width = int(image_width * resize_ratio)
new_watermark_height = int(
watermark_height * new_watermark_width / watermark_width
)

# Resizing and rotating watermark.
watermark = watermark.resize(
(new_watermark_width, new_watermark_height)
).rotate(45, expand=1)

# Rotate watermark size.
rotate_watermark_width, rotate_watermark_height = watermark.size

# Background helper size.
bg_size = int(image_width * resize_ratio)
bg_half = int(bg_size / 2)

# Calculating position for background helper.
position_x = int(bg_half - rotate_watermark_width / 2)
position_y = int(bg_half - rotate_watermark_height / 2)

# New background helper for watermark position.
watermark_bg = Image.new("RGBA", (bg_size, bg_size), (0, 0, 0, 0))
watermark_bg.paste(watermark, (position_x, position_y), watermark)

# Append and shift rotated watermakr to image.
for row in range(10):
for col in range(10):
width = int(-bg_half + bg_size * row)
height = int(-bg_half + bg_size * col)
image.paste(watermark_bg, (width, height), watermark_bg)

# Save the result.
image.save(new_name)


def compute_watermark() -> None:
"""Add a watermark to all images in the current directory and save them
with a new file name that includes the watermark.
"""

# Get directory stats info.
directory = os.listdir()
count = 1

# New file name suffix for generated files.
suffix = "_watermark"
png = ".png"
jpg = ".jpg"
webp = ".webp"

# Checking extension and appending watermark to all images in directory.
for file in directory:
if file.endswith(png) and not file.endswith(suffix + png):
new_name = file[:-4] + suffix + png
append_watermark(file, WATERMARK, new_name)
print_cli_proc("COMPUTE", count, file, new_name)
count += 1
if file.endswith(jpg) and not file.endswith(suffix + jpg):
new_name = file[:-4] + suffix + jpg
append_watermark(file, WATERMARK, new_name)
print_cli_proc("COMPUTE", count, file, new_name)
count += 1
if file.endswith(webp) and not file.endswith(suffix + webp):
new_name = file[:-5] + suffix + webp
append_watermark(file, WATERMARK, new_name)
print_cli_proc("COMPUTE", count, file, new_name)
count += 1


def cli_watermark() -> None:
"""Main function for `watermark` command."""
print_directory_check("WATERMARK", "APPENDING A WATERMARK")
ascii_title("processing")

try:
compute_watermark()
except FileNotFoundError:
warning = "Put your watermakr here '" + WATERMARK + "' to continue!"
print_cli_data("WARNING WATE", warning, 197, 209, 38)
13 changes: 13 additions & 0 deletions src/mdsanima_cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
LP_DESC = "Append a logo to all images in the current directory."
LP_HELP = "appending a logo to all images in the current directory"

WP_PROG = "watermark"
WP_DESC = "Append a watermark to all images in the current directory."
WP_HELP = "appending a watermark to all images in the current directory"


def create_argument_parser() -> None:
"""This function creates an argument parser for all available functions in
Expand Down Expand Up @@ -101,4 +105,13 @@ def create_argument_parser() -> None:
)
logo_parser.set_defaults(command=LP_PROG)

# Create subparser for watermark command.
watermark_parser = subparsers.add_parser(
WP_PROG,
description=WP_DESC,
help=WP_HELP,
epilog=AP_TOP_EPIL,
)
watermark_parser.set_defaults(command=WP_PROG)

return parser

0 comments on commit ef714a6

Please sign in to comment.