update - search files in subfolders by mask except already watermarked
This commit is contained in:
parent
036dfb9144
commit
168d43e363
45
main.py
45
main.py
@ -1,20 +1,47 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
import os.path
|
||||||
|
import pathlib
|
||||||
from app.watermark import watermark_text_bottom_right
|
from app.watermark import watermark_text_bottom_right
|
||||||
|
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(__name__)
|
parser = argparse.ArgumentParser(__name__)
|
||||||
parser.add_argument("-f", "--font", required=True, help='TTF Font file')
|
parser.add_argument("-f", "--font", required=True, help='TTF Font file')
|
||||||
parser.add_argument("-t", "--text", required=True, help='Text watermark')
|
parser.add_argument("-t", "--text", required=True, help='Text watermark')
|
||||||
parser.add_argument("-i", "--input_filename", required=True, help='Input filename')
|
parser.add_argument("-i", "--input_filename", required=False, help='Input filename')
|
||||||
parser.add_argument("-o", "--output_filename", required=True, help='Output filename')
|
parser.add_argument("-if", "--input_folder", required=False, help='Input folder')
|
||||||
|
parser.add_argument("-m", "--file_mask", required=False, help='File mask')
|
||||||
|
parser.add_argument("-o", "--output_filename", required=False, help='Output filename')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
|
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) 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)}"
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
watermark_text_bottom_right(
|
if args.input_filename is not None:
|
||||||
input_image_path=args.input_filename,
|
watermark_text_bottom_right(
|
||||||
output_image_path=args.output_filename,
|
input_image_path=args.input_filename,
|
||||||
text=args.text,
|
output_image_path=args.output_filename,
|
||||||
font_path=args.font,
|
text=args.text,
|
||||||
pos=(1000, 200),
|
font_path=args.font,
|
||||||
size=60)
|
pos=(1000, 200),
|
||||||
|
size=60)
|
||||||
|
elif args.input_folder is not None and args.file_mask is not None:
|
||||||
|
files = _find_all_files(args.input_folder, args.file_mask)
|
||||||
|
print(f"files={files}")
|
||||||
|
for file in files:
|
||||||
|
watermark_text_bottom_right(
|
||||||
|
input_image_path=file,
|
||||||
|
output_image_path=_build_output_filename(file),
|
||||||
|
text=args.text,
|
||||||
|
font_path=args.font,
|
||||||
|
pos=(1000, 200),
|
||||||
|
size=60)
|
||||||
|
5
run-2.sh
Executable file
5
run-2.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
poetry run python main.py -t 'Кострома
|
||||||
|
фото: Вячеслав Бойко, bvn13.me' -if 'samples' -m *.jpg -f fonts/Roboto-Regular.ttf
|
||||||
|
|
@ -1,22 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
FOLDER=$1
|
|
||||||
MASK=$2
|
|
||||||
TEXT=$3
|
|
||||||
|
|
||||||
FILES=$(find $1 -name "$2")
|
|
||||||
echo $FILES
|
|
||||||
|
|
||||||
for fullpath in $FILES; do
|
|
||||||
filename="${fullpath##*/}"
|
|
||||||
dir="${fullpath:0:${#fullpath} - ${#filename}}"
|
|
||||||
base="${filename%.[^.]*}"
|
|
||||||
echo "$base"
|
|
||||||
if [[ $base != *_wm ]]
|
|
||||||
then
|
|
||||||
ext="${filename:${#base} + 1}"
|
|
||||||
output_file="$dir${base}_wm.$ext"
|
|
||||||
echo -e "$fullpath:\n\tdir = \"$dir\"\n\tbase = \"$base\"\n\text = \"$ext\"\n\toutput = $output_file"
|
|
||||||
poetry run python main.py -t "$TEXT" -i $fullpath -o $output_file -f fonts/Roboto-Regular.ttf
|
|
||||||
fi
|
|
||||||
done
|
|
BIN
samples/output_wm.jpg
Normal file
BIN
samples/output_wm.jpg
Normal file
Binary file not shown.
After ![]() (image error) Size: 944 KiB |
Loading…
x
Reference in New Issue
Block a user