|
| 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) |
0 commit comments