-
Notifications
You must be signed in to change notification settings - Fork 0
/
PixelArtIt.py
141 lines (137 loc) · 7.01 KB
/
PixelArtIt.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
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from sklearn.cluster import MiniBatchKMeans
import argparse
import ntpath
# Print iterations progress
def printProgressBar(iteration, total, prefix='', suffix='', decimals=1,
length=100, fill=u'\u2588', header=''):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
header - Optional : header string (Str)
"""
# Clear the current line and print the header
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
# Generate and print the bar
bar = fill * filledLength + u'-' * (length - filledLength)
print('%s |%s| %s%% %s' % (prefix, bar, percent, suffix))
# Print New Lines on Complete
if iteration == total:
print("Your " + input_image_file_name+' has been pixelated with success !!!')
#color filters
def color_filter_to_color_map(color_filter_string):
return {
"AUTUMN" : cv2.COLORMAP_AUTUMN,
"BONE" : cv2.COLORMAP_BONE,
"JET" : cv2.COLORMAP_JET,
"WINTER" : cv2.COLORMAP_WINTER,
"RAINBOW" : cv2.COLORMAP_RAINBOW,
"OCEAN" : cv2.COLORMAP_OCEAN,
"SUMMER" : cv2.COLORMAP_SUMMER,
"SPRING" : cv2.COLORMAP_SPRING,
"COOL" : cv2.COLORMAP_COOL,
"HSV" : cv2.COLORMAP_HSV,
"PINK" : cv2.COLORMAP_PINK,
"HOT" : cv2.COLORMAP_HOT,
"CUSTOM" : 99
}.get(color_filter_string,None)
color_filters =["AUTUMN","BONE","JET","WINTER","RAINBOW","OCEAN","SUMMER","SPRING","COOL","HSV","PINK","HOT","CUSTOM"]
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser(add_help=False)
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
ap.add_argument("-o", "--output", required = False, default="_", help = "Output to the image")
ap.add_argument("-s", "--saturation", required = False, nargs='?', default=1.25, type = int, help = "% of saturation change")
ap.add_argument("-c", "--clusters", required = False, nargs='?', default=8, type = int, help = "# of clusters")
ap.add_argument("-d", "--downscaling", required = False, nargs='?', default=0.25, type = float, help = "% of downscaling")
ap.add_argument("-st", "--steps", required = False, nargs='?', default=False, type = bool, help = "enable saving images for each step")
ap.add_argument("-cf", "--colorFilter", required = False, choices = color_filters, default=-1, help = "apply color mapping")
ap.add_argument("-r", "--resize", required = False, type = int, nargs=2, help = "resize final result to width x height")
args = vars(ap.parse_args())
total_steps = 5
step = 0
input_image_dir = ntpath.dirname(args["image"])
input_image_file_name = ntpath.basename(args["image"])
output_image_dir = ntpath.dirname(args["output"])
output_image_file_name = ntpath.basename(args["output"])
if(output_image_file_name == "_"): output_image_file_name = input_image_file_name.split(".")[0]+"_"
imageFormat = input_image_file_name.split(".")[1]
applyColorFilter = color_filter_to_color_map(args["colorFilter"])
if(applyColorFilter != None): total_steps +=1
print("You are using PixelArtIt 1.0 by Joao Estrela @ DCC.FC.UP.PT")
if(applyColorFilter == 99):
custom_color_map = cv2.imread("custom_colormap.png")
if(custom_color_map.size != 0):
if(np.size(custom_color_map, 0) != 1 and np.size(custom_color_map, 1) != 256):
print("Invalid Custom Colormap - Not 256x1 image.\nNot applying a color filter!")
applyColorFilter = 9
else:
print("No custom_colormap.png found!\nNot applying a color filter!");
applyColorFilter = 9
print("Your " + input_image_file_name+' is going to be pixelated !!!')
img = cv2.imread(args["image"],-1)
step +=1
printProgressBar(step, total_steps, prefix = 'Progress:', suffix = 'Complete', length = 50)
height, width, channels = img.shape
# SATURATION - Giving the image a more vivid look for easier quantitization
saturated = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
for i in range(0, height):
for j in range(0, width):
if(saturated[i,j,1] * args["saturation"] < 0):
saturated[i,j,1] = 0
elif(saturated[i,j,1] * args["saturation"] > 255):
saturated[i,j,1] = 255;
else:
saturated[i,j,1] = int(saturated[i,j,1] * args["saturation"])
saturated = cv2.cvtColor(saturated, cv2.COLOR_HSV2RGB)
if(args["steps"]): cv2.imwrite(output_image_dir+output_image_file_name+"saturated."+imageFormat, saturated)
step +=1
printProgressBar(step, total_steps, prefix = 'Progress:', suffix = 'Complete', length = 50)
# QUANTITIZATION BY K-CLUSTERING - Making only the k most relevant colors appear
quant = cv2.cvtColor(saturated, cv2.COLOR_BGR2LAB)
quant = quant.reshape((quant.shape[0] * quant.shape[1], 3))
clt = MiniBatchKMeans(args["clusters"])
labels = clt.fit_predict(quant)
quant = clt.cluster_centers_.astype("uint8")[labels]
quant = quant.reshape((height, width, 3))
quant = cv2.cvtColor(quant, cv2.COLOR_LAB2BGR)
if(channels == 4):
b = quant[:,:,0]
g = quant[:,:,1]
r = quant[:,:,2]
a = img[:,:,3]
quant = cv2.merge((b,g,r,a))
if(args["steps"]): cv2.imwrite(output_image_dir+output_image_file_name+"quantitized."+imageFormat,quant)
step +=1
printProgressBar(step, total_steps, prefix = 'Progress:', suffix = 'Complete', length = 50)
# Nearest Neighbor Downscaling - Giving a pixelated look
downscaled = cv2.resize(quant,(int(width*args["downscaling"]),int(height*args["downscaling"])), interpolation = cv2.INTER_NEAREST)
if(args["steps"]): cv2.imwrite(output_image_dir+output_image_file_name+"downscaled."+imageFormat,downscaled)
step +=1
printProgressBar(step, total_steps, prefix = 'Progress:', suffix = 'Complete', length = 50)
# Nearest Neighbor Upscaling - Returning to normal size while preserving the pixelated look
if(args["resize"]):
width = args["resize"][0]
height = args["resize"][1]
res = cv2.resize(downscaled,(width,height), interpolation = cv2.INTER_NEAREST)
if(args["steps"]): cv2.imwrite(output_image_dir+output_image_file_name+"rescaled."+imageFormat,res)
step +=1
if(applyColorFilter != None):
if(applyColorFilter == 99):
colored = cv2.LUT(res,custom_color_map)
else:
colored = cv2.applyColorMap(res, applyColorFilter)
cv2.imwrite(output_image_dir+output_image_file_name+"output."+imageFormat,colored)
step +=1
else:
cv2.imwrite(output_image_dir+output_image_file_name+"output."+imageFormat,res)
printProgressBar(step, total_steps, prefix = 'Progress:', suffix = 'Complete', length = 50)