-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhatsapp_sticker_maker.py
308 lines (231 loc) · 10.2 KB
/
whatsapp_sticker_maker.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from glob import glob
# from os import remove
from os import path
from pathlib import Path
import cv2
import numpy as np
from PIL import Image, ImageSequence, ImageFilter
"""
Program made by Ishan Jindal
GitHub: https://github.com/IshanJ25/Stickoz-for-WhatsApp
"""
#########################################
img_types = ['png', 'jpg', 'jpeg', 'gif']
#########################################
square_side = 512
side_gap = 16
stroke_size = 8
threshold = 20
aa_strength = 1.2
white = (255, 255, 255)
total_gap = side_gap + stroke_size
gif_loop_count = 0
#########################################
def make_stickers(folder: str = None,
output_folder: str = None,
# animated: bool = False,
# empty_if_contents=False,
native_format_output=False,
skip_if_exists=False):
"""
Function to automate processing mass image files to produce stickers as per WhatsApp Guides.
8 px thick white border around the image fit in 512x512 square with 16 px distance from edge.
Supports png, jpg, jpeg and gif formats.
:param skip_if_exists: Skip if output file already exists. Default is False.
:param native_format_output: Export in native format instead of webp format. Default is False.
# :param animated: Is the image animated? Default is False.
:param folder: Folder location where all images are present.
:param output_folder: Export folder. New folder is made if already not exists. if not provided,
'sticker_export' is used.
# :param empty_if_contents: Empty exports' folder if already contains files. Default is False.
:return: None. Makes specified type files in specified output folder.
"""
#########################################
if folder is None:
print('Error: Please provide "folder" parameter')
return
if output_folder is None:
output_folder = 'sticker_export'
Path(f'{output_folder}').mkdir(parents=True, exist_ok=True)
# for file in glob(f'{output_folder}/*'):
# remove(file)
#########################################
# noinspection PyUnresolvedReferences
def stroke(origin_image, threshold, stroke_size: int, color):
def change_matrix(input_mat, stroke_size):
stroke_size -= 1
mat = np.ones(input_mat.shape)
check_size = stroke_size + 1.0
mat[input_mat > check_size] = 0
border = (input_mat > stroke_size) & (input_mat <= check_size)
mat[border] = 1.0 - (input_mat[border] - stroke_size)
return mat
def cv2pil(cv_img):
pil_img = Image.fromarray(cv_img.astype("uint8"))
return pil_img
img = np.array(origin_image)
h, w, _ = img.shape
padding = stroke_size
alpha = img[:, :, 3]
rgb_img = img[:, :, 0:3]
bigger_img = cv2.copyMakeBorder(rgb_img, padding, padding, padding, padding,
cv2.BORDER_CONSTANT, value=(0, 0, 0, 0))
alpha = cv2.copyMakeBorder(alpha, padding, padding, padding, padding, cv2.BORDER_CONSTANT, value=0)
bigger_img = cv2.merge((bigger_img, alpha))
h, w, _ = bigger_img.shape
_, alpha_without_shadow = cv2.threshold(alpha, threshold, 255, cv2.THRESH_BINARY) # threshold=0 in photoshop
alpha_without_shadow = 255 - alpha_without_shadow
dist = cv2.distanceTransform(alpha_without_shadow, cv2.DIST_L2, cv2.DIST_MASK_3) # dist l1 : L1 , dist l2 : l2
stroked = change_matrix(dist, stroke_size)
stroke_alpha = (stroked * 255).astype(np.uint8)
stroke_b = np.full((h, w), color[2], np.uint8)
stroke_g = np.full((h, w), color[1], np.uint8)
stroke_r = np.full((h, w), color[0], np.uint8)
stroke = cv2.merge((stroke_b, stroke_g, stroke_r, stroke_alpha))
stroke = cv2pil(stroke).filter(ImageFilter.GaussianBlur(aa_strength))
bigger_img = cv2pil(bigger_img)
result = Image.alpha_composite(stroke, bigger_img)
return result
def get_x_y(img):
im_w, im_h = img.size
if im_w > im_h:
new_w = square_side - 2 * total_gap
new_h = round(im_h * new_w / im_w)
x = side_gap
y = round(square_side / 2 - new_h / 2)
elif im_w < im_h:
new_h = square_side - 2 * total_gap
new_w = round(im_w * new_h / im_h)
x = round(square_side / 2 - new_w / 2)
y = side_gap
else:
new_w = square_side - 2 * total_gap
new_h = square_side - 2 * total_gap
x = side_gap
y = side_gap
return x, y, new_w, new_h
def gen_frame(im):
alpha = im.getchannel('A')
# Convert the image into P mode but only use 255 colors in the palette out of 256
im = im.convert('RGBA').convert('P', palette=Image.ADAPTIVE, colors=255)
# Set all pixel values below 128 to 255 , and the rest to 0
mask = Image.eval(alpha, lambda a: 255 if a <= 128 else 0)
# Paste the color of index 255 and use alpha as a mask
im.paste(255, mask)
# The transparency index is 255
im.info['transparency'] = 255
return im
#########################################
image_count = 0
error_img = []
file_list = []
for i in img_types:
file_list += glob(f'{folder}/*{i}')
for file in file_list:
if file.split('.')[-1] != 'gif':
if native_format_output:
output_extension = 'png'
else:
output_extension = 'webp'
try:
im = Image.open(file)
im_name = str(im.filename[len(folder) + 1:]).split('.')[0]
if skip_if_exists:
if path.isfile(f"{output_folder}/{im_name}.{output_extension}"):
# print(f"{output_folder}/{im_name}.{output_extension} exists")
continue
print(f"{output_folder}/{im_name}.{output_extension} ... ", end='')
except:
print("Error!")
error_img.append(str(file[len(folder) + 1:]))
continue
x, y, new_w, new_h = get_x_y(im)
im = im.convert('RGBA').resize((new_w, new_h))
try:
im = stroke(im, threshold=threshold, stroke_size=stroke_size, color=white)
except:
error_img.append(im_name)
print("Error!")
continue
image = Image.new('RGBA', (square_side, square_side))
image.paste(im, (x, y), im)
im_name = im_name.split('.')[0]
image.save(f"{output_folder}/{im_name}.{output_extension}")
image_count += 1
print("Done!")
elif file.split('.')[-1] == 'gif':
if native_format_output:
output_extension = 'gif'
else:
output_extension = 'webp'
try:
im = Image.open(file)
im_name = str(im.filename[len(folder) + 1:]).split('.')[0]
if skip_if_exists:
if path.isfile(f"{output_folder}/{im_name}.{output_extension}"):
# print(f"{output_folder}/{im_name}.{output_extension} exists")
continue
print(f"{output_folder}/{im_name}.{output_extension} ... ", end='')
except:
print("Error!")
error_img.append(str(file[len(folder) + 1:]))
continue
x, y, new_w, new_h = get_x_y(im)
im_list = []
for frame in ImageSequence.Iterator(im):
try:
frame = frame.convert('RGBA').resize((new_w, new_h))
frame = stroke(frame, threshold=threshold, stroke_size=stroke_size, color=white)
image = Image.new('RGBA', (square_side, square_side))
image.paste(frame, (x, y), frame)
image = gen_frame(image)
im_list.append(image)
except:
error_img.append(im_name)
print("Error!")
break
duration = im.info['duration']
img = im_list[0]
imgs = im_list[1:]
img.save(f"{output_folder}/{im_name}.{output_extension}", save_all=True, append_images=imgs,
duration=duration, loop=gif_loop_count, optimize=True, disposal=2, lossless=True,
quality=25)
image_count += 1
print("Done!")
if image_count == 0:
print(f'\nNo images were found in provided folder\n')
else:
print(f'\n{image_count} image(s) were processed.\n')
if error_img:
print(f'\nError processing {len(error_img)} image(s):')
for i in error_img:
print(i)
print('\n')
input('Press <enter> to continue...')
def give_code_format(folder: str = None, img_type: str = None):
"""
Function to automate and print names of image according to suitable syntax.
:param img_type: Type of image to index (png/gif/webp).
:param folder: Folder location where all exported pngs are present.
:return: None. Prints android app syntax with file names
"""
if folder is None and img_type is None:
print('No folder or type provided')
return
elif folder is None:
print('No folder provided')
return
elif img_type is None:
print('No type provided')
return
if img_type != 'png' and img_type != 'gif' and img_type != 'webp':
print('Invalid type')
return
lst = []
for file in glob(f'{folder}/*.{img_type}'):
lst.append(file.split('.')[0])
for i in lst:
print(' {')
print(' "image_file": "{}.webp",'.format(i))
print(' "emojis": []')
print(' },')