diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/cli.py b/app/cli.py new file mode 100644 index 0000000..9ffa9cb --- /dev/null +++ b/app/cli.py @@ -0,0 +1,54 @@ +import argparse +import os.path +import pathlib +from app.watermark import watermark_text_bottom_right + +_FONTS_DIR = pathlib.Path(__file__).parent / "fonts" +_DEFAULT_FONT = str(_FONTS_DIR / "Roboto-Regular.ttf") + + +def _find_all_files(folder: str, mask: str) -> list: + root = pathlib.Path(folder) + return [f"{os.path.join(file.parent, file.name)}" for file in root.rglob(mask, case_sensitive=False) if '_wm' != os.path.splitext(file.name)[0][-3:]] + + +def _build_output_filename(filename: str) -> str: + file = pathlib.Path(filename) + basename, extension = os.path.splitext(file.name) + newfilename = f"{basename}_wm{extension}" + print(f"file={file}, basename={basename}, extension={extension}, newname={newfilename}") + return f"{os.path.join(file.parent, newfilename)}" + + +def main(): + parser = argparse.ArgumentParser("watermark") + parser.add_argument("-f", "--font", required=False, default=_DEFAULT_FONT, help='TTF Font file (default: bundled Roboto-Regular)') + parser.add_argument("-t", "--text", required=True, help='Text watermark') + parser.add_argument("-i", "--input_filename", required=False, help='Input filename') + parser.add_argument("-if", "--input_folder", required=False, default=".", help='Input folder (default: current directory)') + parser.add_argument("-m", "--file_mask", required=False, default="*.jpg", help='File mask (default: *.jpg)') + parser.add_argument("-o", "--output_filename", required=False, help='Output filename') + args = parser.parse_args() + + if args.input_filename is not None: + watermark_text_bottom_right( + input_image_path=args.input_filename, + output_image_path=args.output_filename, + text=args.text, + font_path=args.font, + pos=(1000, 200), + size=60) + else: + files = _find_all_files(args.input_folder, args.file_mask) + print(f"files={files}") + for file in files: + newfilename = _build_output_filename(file) + if os.path.isfile(newfilename): + continue + watermark_text_bottom_right( + input_image_path=file, + output_image_path=newfilename, + text=args.text, + font_path=args.font, + pos=(1000, 200), + size=60) diff --git a/app/fonts/Roboto-Regular.ttf b/app/fonts/Roboto-Regular.ttf new file mode 100644 index 0000000..7e3bb2f Binary files /dev/null and b/app/fonts/Roboto-Regular.ttf differ diff --git a/app/fonts/__init__.py b/app/fonts/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dist/py_watermark-0.1.0-py3-none-any.whl b/dist/py_watermark-0.1.0-py3-none-any.whl new file mode 100644 index 0000000..ba2abe4 Binary files /dev/null and b/dist/py_watermark-0.1.0-py3-none-any.whl differ diff --git a/dist/py_watermark-0.1.0.tar.gz b/dist/py_watermark-0.1.0.tar.gz new file mode 100644 index 0000000..cdd23a1 Binary files /dev/null and b/dist/py_watermark-0.1.0.tar.gz differ diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..98b8770 --- /dev/null +++ b/install.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Установка watermark как глобальной команды +# +# После установки запуск из любой директории: +# watermark -t 'текст' -m '*.JPG' +# +# Параметры: +# -t текст водяного знака (обязательный) +# -if папка с фото (по умолчанию: текущая директория) +# -m маска файлов (по умолчанию: *.jpg, регистр не важен) +# -f путь к TTF-шрифту (по умолчанию: bundled Roboto-Regular) + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$SCRIPT_DIR" + +echo "Сборка пакета..." +poetry build + +echo "Установка..." +python3 -m pip install --force-reinstall dist/py_watermark-*.whl + +echo "" +echo "Готово. Проверка:" +watermark --help diff --git a/pyproject.toml b/pyproject.toml index 4a8beda..34eaeed 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,13 @@ dependencies = [ ] +[project.scripts] +watermark = "app.cli:main" + +[tool.poetry] +packages = [{include = "app"}] +include = ["app/fonts/Roboto-Regular.ttf"] + [build-system] requires = ["poetry-core>=2.0.0,<3.0.0"] build-backend = "poetry.core.masonry.api" diff --git a/run-1.sh b/run-1.sh new file mode 100755 index 0000000..b4f3156 --- /dev/null +++ b/run-1.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +poetry run python main.py -t 'Кострома +фото: Вячеслав Бойко, bvn13.me' -i samples/input.jpg -o samples/output.jpg -f fonts/Roboto-Regular.ttf + diff --git a/run.sh b/run.sh index b4f3156..59d537b 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,4 @@ #!/bin/bash -poetry run python main.py -t 'Кострома -фото: Вячеслав Бойко, bvn13.me' -i samples/input.jpg -o samples/output.jpg -f fonts/Roboto-Regular.ttf +poetry run python main.py -t 'фото: Вячеслав Бойко, bvn13.me' -if './' -m *.jpg -f /home/bvn13/develop/py-watermark/fonts/Roboto-Regular.ttf