-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgen_wm.py
132 lines (95 loc) · 4.15 KB
/
gen_wm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os, shutil
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras import models
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw, ImageOps
import string
import numpy as np
import cv2
import random
"""
This file adds extra watermark application styles. Currently this is not in use but,
if implemented provides a greater diversity of the watermarks applied to images
"""
def create_rnd_txt_wm ():
"""Generates the random text based watermark
Returns:
Pil.Image: The random text watermark.
"""
prints = list(string.printable)[0:84]
#open all of the images from the VOC2008 dataset as jpegs
font_size = np.random.randint(low = 50, high = 350)
#create the watermark font for the image
font = ImageFont.truetype("arial.ttf", font_size)
#generate image to hold the watermark text object
img_temp = Image.new('L', (350,350))
#create the watermark text, of random length, using random printable characters
text_str = np.random.choice(prints, np.random.randint(low=5, high = 14))
text_str = "".join(text_str)
#draw on temporary image with text
draw_temp = ImageDraw.Draw(img_temp)
#generate a random integer for the opacity argument (fill)
opac = np.random.randint(low= 70, high=150)
#insert text onto the temporary image
draw_temp.text((0, 0), text_str, font=font, fill=opac)
#generate a random integer for rotation:
rot_int = np.random.randint(low = 0, high = 180)
#rotate the text on the temporary image
rotated_text = img_temp.rotate(rot_int, expand=1)
#generate a random location for the watermark on the image
#merge the temporary image with text with the image passed in
#third tuple also needs to be random: controls the location of the img
return rotated_text
def apply_rnd_watermark(im):
"""Applies the random watermark to a given image.
Args:
im (PIL.Image): The image that needs marking.
Returns:
PIL.Image: The marked image.
"""
img = im
for i in range(np.random.randint(low=1, high=3)):
#default color of watermark set to white; change if desired
col_1 = (255,255,255)
col_2 = (255,255,255)
width, height = img.size
# #generate a random location for the watermark on the image
rand_loc = (np.random.randint(low=10,high=width-width*0.3), np.random.randint(low=10,high=height-height*0.3))
rotated_text = create_rnd_txt_wm()
img.paste(ImageOps.colorize(rotated_text, col_1, col_2), rand_loc, rotated_text)
#this yeilds a new image with a watermark
#save this jpeg with a watermark to the WATS directory
return img
def apply_grid_watermark(im, watermark_1):
"""Applies a grid style watermark to the given image
Args:
im (PIL.Image): The image that needs marking
watermark_1 (PIL.Image): The basis for the grid watermark
"""
main = im
mark = random.choice([watermark_1, create_rnd_txt_wm().convert("RGBA")])
mask = mark.convert('L').point(lambda x: min(x, np.random.randint(low = 100, high= 170)))
mark.putalpha(mask)
############################################################
#generate a random integer for rotation:
rot_int = np.random.randint(low = 0, high = 180)
mark_width, mark_height = mark.size
main_width, main_height = main.size
aspect_ratio = mark_width / mark_height
new_mark_width = main_width * random.uniform(0.125, 0.35)
mark = mark.rotate(rot_int, expand=1)
mark.thumbnail((new_mark_width, new_mark_width / aspect_ratio), Image.ANTIALIAS)
############################################################
tmp_img = Image.new('RGB', main.size)
for i in range(0, tmp_img.size[0], mark.size[0]):
for j in range(0, tmp_img.size[1], mark.size[1]):
main.paste(mark, (i, j), mark)
main.thumbnail((8000, 8000), Image.ANTIALIAS)
# main.save(f"{out_str}{im_name}", quality=100)
return main