Skip to content

Commit c73aef8

Browse files
authored
Merge pull request larymak#88 from odinmay/main
Add CLI-Photo-Watermark script, Update readme.md
2 parents 115f4ba + 4e7a362 commit c73aef8

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

CLI-Photo-Watermark/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Usage #
2+
---
3+
4+
- This script is for watermarking an entire directory of images ( .jpgs | .pngs )
5+
- Run the script in the directory that you want to watermark like this:
6+
``python main.py -w [watermark] -p [position] ``
7+
8+
- watermark - The text you want to watermark on the images
9+
- position - ul : upper left , ur - upper right , ll - lower left , lr - lower right
10+
11+
- This will make a folder called 'Watermarked' and copy the new watermarked images into it, preserving original images

CLI-Photo-Watermark/main.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import argparse
2+
import cv2
3+
import os
4+
5+
# Watermark Configuration
6+
font = cv2.FONT_HERSHEY_COMPLEX
7+
color = (255, 255, 255)
8+
thickness = 4
9+
10+
# Setting up the argument parser for CMD Line interface
11+
ap = argparse.ArgumentParser()
12+
ap.add_argument('-f', '--file', required=False,
13+
help='Path to target file')
14+
ap.add_argument('-w', '--watermark', required=True,
15+
help='Text you would like to watermark image with | (Enclose in quotes if there are spaces)')
16+
ap.add_argument('-d', '--directory', required=False,
17+
help='Processes every image in the CWD')
18+
ap.add_argument('-p', '--position', required=True,
19+
help='Options are "ul"(upper left) "ur"(upper right) "ll"(lower left) "lr"(lower right)')
20+
args = ap.parse_args()
21+
print(args)
22+
23+
def process_image(filename, watermark, pos):
24+
"""
25+
:param filename: str
26+
the path of the photo, built from cwd
27+
:param watermark: str
28+
the text you want watermarked on the image
29+
:param pos: str
30+
the position of the watermark ex. "ll" (lower left) | "ur" (upper right)
31+
:return: None
32+
a new folder name "Watermarked" will be made in CWD with finished images
33+
"""
34+
35+
working_image = cv2.imread(filename)
36+
text_length = len(watermark)
37+
if working_image.shape[0] >= 4000:
38+
avg_char = 120
39+
text_width = text_length * avg_char
40+
fontScale = 6
41+
image_ul = (0, 150)
42+
image_ur = (working_image.shape[1] - text_width, 150)
43+
image_ll = (0, working_image.shape[0] - 50)
44+
image_lr = (working_image.shape[1] - text_width, working_image.shape[0] - 50)
45+
else:
46+
avg_char = 80
47+
text_width = text_length * avg_char
48+
fontScale = 3
49+
image_ul = (0, 100)
50+
image_ur = (working_image.shape[1] - text_width, 100)
51+
image_ll = (0, working_image.shape[0] - 50)
52+
image_lr = (working_image.shape[1] - text_width, working_image.shape[0] - 50)
53+
54+
if pos == 'ul':
55+
new_image = cv2.putText(working_image, args.watermark, image_ul, font, fontScale, color, thickness, cv2.LINE_AA)
56+
57+
if pos == 'ur':
58+
new_image = cv2.putText(working_image, args.watermark, image_ur, font, fontScale, color, thickness, cv2.LINE_AA)
59+
60+
if pos == 'll':
61+
new_image = cv2.putText(working_image, args.watermark, image_ll, font, fontScale, color, thickness, cv2.LINE_AA)
62+
63+
if pos == 'lr':
64+
new_image = cv2.putText(working_image, args.watermark, image_lr, font, fontScale, color, thickness, cv2.LINE_AA)
65+
66+
if not os.path.exists(os.getcwd() + '\\Watermarked'):
67+
os.mkdir(os.getcwd() + '\\Watermarked')
68+
69+
path = os.getcwd() + '\\' + 'Watermarked' + '\\' + file
70+
cv2.imwrite(path, new_image)
71+
72+
73+
# Call function on all files in CWD ending with .png or .jpg
74+
for file in os.listdir(os.getcwd()):
75+
if file.endswith('.jpg') or file.endswith('.png'):
76+
process_image(os.getcwd() + '\\' + file, args.watermark, args.position)

CLI-Photo-Watermark/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
opencv-python~=4.5.1.48

CLI-Photo-Watermark/testphoto.jpg

32.7 KB
Loading

CLI-Photo-Watermark/testpng.png

40.3 KB
Loading

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ The contribution guidelines are as per the guide [HERE](https://github.com/larym
8787
| 44 | [Sudoku Solver](https://github.com/Mannuel25/Python-project-Scripts/tree/main/SudokuSolver) | [Ruben Grande Muñoz](https://github.com/RgrMz) |
8888
| 45 | [Duplicate File Remover](https://github.com/mas-designs/Python-project-Scripts/tree/main/Remove%20Duplicate%20Files%20in%20Folder) | [Michael Stadler](https://github.com/mas-designs) |
8989
| 46 | [Image Divider](https://github.com/larymak/Python-project-Scripts/tree/main/ImageDivider) | [Rajarshi Banerjee](https://github.com/GSAUC3) |)
90-
| 47 | [Morse Code Converter](https://github.com/HarshitRV/Python-project-Scripts/tree/main/Morse-Code-Converter) | [HarshitRV](https://github.com/HarshitRV) |)
90+
| 47 | [Morse Code Converter](https://github.com/HarshitRV/Python-project-Scripts/tree/main/Morse-Code-Converter) | [HarshitRV](https://github.com/HarshitRV) |)
91+
| 48 | [CLI Photo Watermark](https://github.com/odinmay/Python-project-Scripts/tree/main/CLI-Photo-Watermark) | [Odin May](https://github.com/odinmay)

0 commit comments

Comments
 (0)