-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyx.py
433 lines (381 loc) · 13.6 KB
/
pyx.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
""" Command-line interface for Pyxelate. It simplifies batch processing
and provides user-friendly console output.
Usage:
pyx.py [-h help] [-i folder of input images or path to single image]
[-o folder of output images] [-f scale down input image by factor]
[-s scale up output image by factor] [-c colors] [-d dither] [-a alpha]
[-r regenerate_palette] [-t random_state] [-w warnings] [-S sequence]
"""
import argparse
import sys
import warnings
import time as t
from pathlib import Path
from numpy import uint8
from skimage import io
from skimage import transform
from pyxelate import Pyxelate
def parse_arguments():
""" Parses arguments and returns values. This function contains
descriptions for the -h flag
"""
parser = argparse.ArgumentParser(
description='Pixelate images in a directory.'
)
parser.add_argument(
'-f', '--factor',
required=False, metavar='int', type=int, default=5, nargs='?',
help='''The factor by which the image should be downscaled.
Defaults to 5.'''
)
parser.add_argument(
'-s', '--scaling',
required=False, metavar='int', type=int, default=5, nargs='?',
help='''The factor by which the generated image should be
upscaled. Defaults to 5.'''
)
parser.add_argument(
'-c', '--colors',
required=False, metavar='2-32', type=int, default=8, nargs='?',
help='''The amount of colors of the pixelated image. Defaults
to 8.'''
)
parser.add_argument(
'-d', '--dither',
required=False, metavar='bool', type=str_as_bool, nargs='?',
default=None, help='Allow dithering. Defaults to True.'
)
parser.add_argument(
'-a', '--alpha',
required=False, metavar='threshold', type=float, default=.6,
nargs='?', help='''Threshold for visibility for images with
alpha channel. Defaults to .6.'''
)
parser.add_argument(
'-r', '--regenerate_palette',
required=False, metavar='bool', type=str_as_bool, nargs='?',
default=None, help='''Regenerate the palette for each image.
Defaults to True.'''
)
parser.add_argument(
'-t', '--random_state',
required=False, metavar='int', type=int, default=0, nargs='?',
help='''Sets the random state of the Bayesian Gaussian Mixture.
Defaults to 0.'''
)
parser.add_argument(
'-i', '--input',
required=False, metavar='path', type=str, default='', nargs='?',
help='''Path to single image or directory containing images for
processing. Defaults <cwd>.'''
)
parser.add_argument(
'-o', '--output',
required=False, metavar='path', type=str, default='', nargs='?',
help='''Path to the directory where the pixelated images are
stored. Defaults to <cwd>/pyxelated'''
)
parser.add_argument(
'-w', '--warnings',
required=False, metavar='bool', type=str_as_bool, nargs='?',
default=True, help='''Outputs non-critical library warnings.
Defaults to True.'''
)
parser.add_argument(
'-S', '--sequence',
required=False, metavar='bool', type=str_as_bool, nargs='?',
default=False, help='''Uses a separate function for converting
image sequences. Defaults to False.'''
)
return parser.parse_args()
def str_as_bool(val):
""" Interpret the string input as a boolean
"""
if val.lower() in ("false", "none", "no", "0"):
return False
return True
# Exclude hidden files and directories
F_EXCLUDED = 0
def exclude_hidden(elm):
""" A filter that returns a file if it's not hidden
"""
global F_EXCLUDED
if not any(e.startswith('.') for e in elm.parts):
return elm
F_EXCLUDED += 1
return False
def with_extension(elm):
""" A filter that returns only a file with extension
"""
global F_EXCLUDED
if elm.is_file() and '.' in elm.name:
return elm
F_EXCLUDED += 1
return False
def get_file_list(path):
""" Finds all the files recursively and filters them
"""
path = Path(path)
if path.is_file() and '.' in path.name:
return [path]
if path.is_dir():
# Get all files and directories
tree = list(path.glob('**/*'))
# Filter files and directories
tree = list(filter(exclude_hidden, tree))
file_names = list(filter(with_extension, tree))
return file_names
print("Path points to " + s['red']("non image") + " file.")
sys.exit(1)
def parse_path(file):
""" Returns relative path, file name and extension
"""
file, ext = str(file).rsplit('.', 1)
inp = str(Path(args.input))
if inp == '.':
file = '/' + file
try:
path, file = file.rsplit('/', 1)
except ValueError:
path = ""
if inp == str(file):
path = ""
path = path.replace(inp, "")
path += '/' if path else ''
return [path, file, ext]
# Define CLI colors
s = {
'green': lambda txt: '\u001b[32m' + str(txt) + '\u001b[0m',
'red': lambda txt: '\u001b[31m' + str(txt) + '\u001b[0m',
'mag': lambda txt: '\u001b[35m' + str(txt) + '\u001b[0m',
'dim': lambda txt: '\u001b[37;2m' + str(txt) + '\u001b[0m'
}
# Status bar logic
CUR_FILE = 0
WARN_CNT = 0
ERR_CNT = 0
TIME_IMG = []
T_SPLIT = []
AVG_LAST_VALS = 10
T_UP = '\x1b[1A'
T_ERASE = '\x1b[2K'
BAR_RMV = '\n' + T_ERASE + T_UP + T_ERASE
def sec_to_time(sec):
""" Returns the formatted time H:MM:SS
"""
mins, sec = divmod(sec, 60)
hrs, mins = divmod(mins, 60)
return f"{hrs:d}:{mins:02d}:{sec:02d}"
def bar_redraw(last=False):
""" Updates the progress bar and current status
"""
t_pass = round(t.process_time())
i_cur = CUR_FILE
# Print bar
percent = round(i_cur / ALL_FILES * 100, 1)
p_int = round(i_cur / ALL_FILES * 100) // 2
pbar = "[ " + "•" * (p_int) + s['dim']("-") * (50 - p_int) + " ] "
pbar += str(percent) + " %"
print(pbar)
# Print status
stat = "Elapsed: " + sec_to_time(t_pass)
stat += s['dim'](" | ") + "Remaining: "
# Remaining time. Averaging requires at least 1 value
if len(TIME_IMG) > 0 and not last:
t_avg = sum(TIME_IMG) / len(TIME_IMG)
rem = round(t_avg * (ALL_FILES - i_cur))
stat += sec_to_time(rem)
else:
stat += "Calculating..." if not last else sec_to_time(0)
stat += ("\nDone " + s['green'](str(i_cur)) + '/' +
str(ALL_FILES) + s['dim'](" | "))
if args.warnings:
stat += "Warnings: " + s['mag'](str(WARN_CNT)) + s['dim'](" | ")
stat += "Errors: " + s['red'](str(ERR_CNT))
# Adding escape codes depending on the passed argument
if last:
stat = BAR_RMV + stat
else:
# Raise the carriage three lines up and return it
stat = stat + T_UP * 3 + '\r'
print(stat)
def print_warn(warn):
""" Outputs text as a warning
"""
if str(warn) and args.warnings:
warn = str(warn).strip().capitalize()
print(BAR_RMV + s['mag']("\tWarning: ") + warn)
bar_redraw()
def print_err(err):
""" Outputs text as an error
"""
if str(err):
err = str(err).strip().capitalize()
print(BAR_RMV + s['red']("\tError: ") + err)
bar_redraw()
def print_settings():
""" Outputs the full list of settings
"""
print("Pyxelate settings\n" +
s['dim']("\tFactor: ") + "\t" +
str(args.factor) + "\t" +
s['dim']("\tScaling: ") + "\t" +
str(args.scaling) + "\n" +
s['dim']("\tColors: ") + "\t" +
str(args.colors) + "\t" +
s['dim']("\tDither: ") + "\t" +
("No", "Yes")[args.dither] + "\n" +
s['dim']("\tAlpha channel: ") + "\t" +
str(args.alpha) + "\t" +
s['dim']("\tRegenerate: ") + "\t" +
("No", "Yes")[args.regenerate_palette] + "\n" +
s['dim']("\tRandom state: ") + "\t" +
str(args.random_state) + "\t" +
s['dim']("\tSequence: ") + "\t" +
("No", "Yes")[args.sequence] + "\n")
if __name__ == "__main__":
# Get arguments and file list
args = parse_arguments()
IMAGE_FILES = get_file_list(args.input)
ALL_FILES = len(IMAGE_FILES)
# Use the output directory defined by args
if not args.output:
OUTPUT_DIR = Path.cwd() / "pyxelated"
OUTPUT_DIR.mkdir(exist_ok=True)
else:
OUTPUT_DIR = Path(args.output)
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
# Set the default settings depending on the type of conversion
if args.dither is None:
args.dither = (True, False)[args.sequence]
if args.regenerate_palette is None:
args.regenerate_palette = (True, False)[args.sequence]
print_settings()
# Get input and output paths
INPUT_DIR = Path(args.input) if args.input else Path.cwd()
# Display input path
if "/" in str(INPUT_DIR):
I_PATH, I_BASE = str(INPUT_DIR).rsplit('/', 1)
print("Reading files from " + s['dim'](I_PATH + '/') + I_BASE)
else:
print("Reading files from " + s['dim'](str(Path.cwd())) +
"/" + str(INPUT_DIR))
# At least one relevant file is required to run
print(end="\t")
if IMAGE_FILES:
print(s['green'](len(IMAGE_FILES)) + " relevant files found" +
s['dim'](" | ") + s['red'](F_EXCLUDED) + " excluded" +
"\n")
else:
print(s['red'](len(IMAGE_FILES)) + " relevant files found")
sys.exit(1)
# Display output path
if "/" in str(OUTPUT_DIR):
O_PATH, O_BASE = str(OUTPUT_DIR).rsplit('/', 1)
print("Writing files to " + s['dim'](O_PATH + '/') + O_BASE)
else:
print("Writing files to " + str(OUTPUT_DIR))
# Setup Pyxelate with placeholder dimensions 1x1
p = Pyxelate(
1, 1,
color=args.colors,
dither=args.dither,
alpha=args.alpha,
regenerate_palette=args.regenerate_palette,
random_state=args.random_state
)
# Prepare images to convert sequence
if args.sequence:
SEQUENCE = [io.imread(file) for file in IMAGE_FILES]
SEQ_IMAGE = p.convert_sequence(SEQUENCE)
HEIGHT, WIDTH, _ = io.imread(IMAGE_FILES[0]).shape
p.height = HEIGHT // args.factor
p.width = WIDTH // args.factor
for i, image_file in enumerate(IMAGE_FILES):
# Make a time stamp to calculate remaining
if i or not args.sequence:
# Calculate the time difference between iterations
T_SPLIT += [t.time()]
if T_SPLIT[1:]:
if len(TIME_IMG) == AVG_LAST_VALS:
del TIME_IMG[0]
diff = round(T_SPLIT[1] - T_SPLIT.pop(0), 1)
TIME_IMG.append(diff)
else:
# Skip the first iteration to generate sequence palette
print("\tPreparing to convert image sequence...")
# Get the path, file name, and extension
base = str(image_file.stem) + ".png"
outfile = OUTPUT_DIR / base
f_path, f_name, f_ext = parse_path(image_file)
# The file format must be supported by skimage
try:
image = io.imread(image_file)
except ValueError:
# When the file is not an image just move to the next file
print(BAR_RMV + "\tSkipping " + s['red']("unsupported") +
":\t" + s['dim'](f_path) + f_name + '.' +
s['red'](f_ext))
bar_redraw()
continue
print(BAR_RMV + "\tProcessing image:\t" + s['dim'](f_path) +
f_name + '.' + f_ext)
# Redraw status bar
bar_redraw()
# Get current dimensions
if args.sequence:
# Get sequence dimensions
height, width = HEIGHT, WIDTH
else:
# Get image dimensions
height, width, _ = image.shape
# Apply the dimensions to Pyxelate
p.height = height // args.factor
p.width = width // args.factor
# Convert the image
with warnings.catch_warnings(record=True) as w:
try:
if args.sequence:
# Convert image from sequence
pyxelated = next(SEQ_IMAGE)
else:
# Convert a single image
pyxelated = p.convert(image)
except KeyboardInterrupt:
print(BAR_RMV + "\tCancelled with " + s['red']("Ctrl+C"))
bar_redraw(last=True)
sys.exit(0)
except IndexError as e:
# When the file is not an image just move to the next file
ERR_CNT += 1
print_err(e)
bar_redraw()
continue
if w:
WARN_CNT += 1
print_warn(w.pop().message)
# Scale the image up if so requested
if args.scaling > 1:
pyxelated = transform.resize(
pyxelated, (
(height // args.factor) * args.scaling,
(width // args.factor) * args.scaling
),
anti_aliasing=False, mode='edge',
preserve_range=True, order=0
)
# Finally save the image
with warnings.catch_warnings(record=True) as w:
try:
io.imsave(outfile, pyxelated.astype(uint8))
except KeyboardInterrupt:
print(BAR_RMV + "\tCancelled with " + s['red']("Ctrl+C"))
bar_redraw(last=True)
sys.exit(0)
if w:
WARN_CNT += 1
print_warn(w.pop().message)
CUR_FILE += 1
bar_redraw(last=True)