-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize.py
155 lines (134 loc) · 5.26 KB
/
resize.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
#!/usr/bin/env python3
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Telegram Sticker Resizer "
" Resize images to fit telegram sticker size limitations. "
" Author : @jaidTw "
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
import cv2
import os
import sys
import subprocess
import shutil
from pathlib import Path
from PIL import Image
MAX_HEIGHT = 512
MAX_WIDTH = 512
WAIFU2X = False
OUTPUT_DIR = './out/'
FORCE_OVERWRITE = False
EXT_SUPPORT = ['.png', '.jpg', '.bmp', '.jpeg']
WAIFU2X_BIN = "waifu2x-ncnn-vulkan-20210521-windows\waifu2x-ncnn-vulkan.exe"
WAIFU2X_OUTPUT_DIR = "./_waifu2x_tmp/"
def banner():
print('[-] --- Telegram Sticker Resizer ---')
print(' Resize images to fit the telegram sticker size restrictions.')
def help():
print(' Usage: ./resize.py [-f] [-h] [-w] <input_path>')
print(' -f : Force overwriting.')
print(' -h : Show this helping message.')
print(' -w : Use Waifu2x to scale the image first')
print(' input_path : Path for input images.')
def resize(fname):
img = cv2.imread(fname.as_posix(), cv2.IMREAD_UNCHANGED)
if img is None:
print("[ERROR] Can't load %s, probably broken? Skipping" % fname)
return 0, 0, 1
height, width, _ = img.shape
print(" Size : %d x %d" % (height, width))
if (height == 512 and width <= MAX_WIDTH) or \
(height <= MAX_HEIGHT and width == 512):
print(" Size already feasible. Skipping")
return 0, 1, 0
scale = min(MAX_HEIGHT / height, MAX_WIDTH / width)
new = cv2.resize(img, None, fx=scale, fy=scale,
interpolation=cv2.INTER_AREA)
print(" Resized : %d x %d, scale = %f" % (*new.shape[:2], scale))
fname = fname.stem + '.png'
output = OUTPUT_DIR / fname
if output.is_file():
if not FORCE_OVERWRITE:
print("\n%s already exists, overwrite? (y/N)" % output,
file=sys.stderr)
opt = input()
if opt == '' or opt[0].lower() != 'y':
print(' File exists. Skipping')
return 0, 1, 0
elif output.is_dir():
print('[ERROR] %s is an existing directory. Skipping' % output)
return 0, 0, 1
if not cv2.imwrite(output.as_posix(), new):
print("[ERROR] Failed to save output to %s. Skipping" % output)
return 0, 0, 1
print(" Successfully saved to %s" % output)
return 1, 0, 0
def main():
global OUTPUT_DIR, FORCE_OVERWRITE, WAIFU2X
banner()
if '-h' in sys.argv:
help()
exit()
if '-f' in sys.argv:
FORCE_OVERWRITE = True
sys.argv.remove('-f')
if '-w' in sys.argv:
WAIFU2X = True
sys.argv.remove('-w')
if len(sys.argv) > 1:
INPUT_DIR = Path(sys.argv[1])
if not INPUT_DIR.exists():
print("[ERROR] \"%s\" doest not exist" % INPUT_DIR)
exit()
if not INPUT_DIR.is_dir():
print("[ERROR] \"%s\" is not a directory" % INPUT_DIR)
exit()
OUTPUT_DIR = INPUT_DIR / 'out'
try:
OUTPUT_DIR.mkdir(exist_ok=True)
except PermissionError:
print('[ERROR] Failed to create %s, permission denied.' %
OUTPUT_DIR)
exit()
if not os.access(OUTPUT_DIR, os.W_OK):
print('[ERROR] No permission to write in %s.' %
OUTPUT_DIR)
exit()
cnt = (0, 0, 0)
try:
if WAIFU2X:
print('[-] Fixing alpha channel in case of it\'s wrong.')
# fix alpha channel in original image in case it's ill-formed
for fname in INPUT_DIR.iterdir():
if fname.suffix in EXT_SUPPORT:
img = Image.open(fname.as_posix())
if img.mode == "L" or img.mode == "P":
img.convert("RGBA").save(fname.as_posix())
print('[-] Using Waifu2x to scale the image')
waifu2x_output = INPUT_DIR / WAIFU2X_OUTPUT_DIR
try:
waifu2x_output.mkdir(exist_ok=True)
except PermissionError:
print('[ERROR] Failed to create %s, permission denied.' %
waifu2x_output)
exit()
if not os.access(waifu2x_output, os.W_OK):
print('[ERROR] No permission to write in %s.' %
waifu2x_output)
exit()
subprocess.run([WAIFU2X_BIN, "-i", INPUT_DIR.absolute(), "-o", waifu2x_output.absolute(), "-x", "-n", "2"])
for fname in INPUT_DIR.iterdir():
if fname.suffix in EXT_SUPPORT:
print("[-] Opening %s ..." % fname)
cnt = tuple(map(sum, zip(cnt, resize(fname))))
shutil.rmtree(waifu2x_output)
else:
for fname in INPUT_DIR.iterdir():
if fname.suffix in EXT_SUPPORT:
print("[-] Opening %s ..." % fname)
cnt = tuple(map(sum, zip(cnt, resize(fname))))
print('[-] Done.')
except KeyboardInterrupt:
print('[-] Interrupted.')
print('[-] %d file(s) processed, %d file(s) skipped, %d error(s) occured.'
% cnt)
if __name__ == '__main__':
main()