Spaces:
Runtime error
Runtime error
artificialguybr
commited on
Commit
•
12c32a1
1
Parent(s):
ebf4e0c
Delete postprocessing_pixelart.py
Browse files- postprocessing_pixelart.py +0 -166
postprocessing_pixelart.py
DELETED
@@ -1,166 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from PIL import features
|
3 |
-
|
4 |
-
from modules import scripts_postprocessing
|
5 |
-
from modules.shared import opts
|
6 |
-
|
7 |
-
from sd_webui_pixelart.utils import DITHER_METHODS, QUANTIZATION_METHODS, downscale_image, limit_colors, resize_image, convert_to_grayscale, convert_to_black_and_white
|
8 |
-
|
9 |
-
|
10 |
-
class ScriptPostprocessingUpscale(scripts_postprocessing.ScriptPostprocessing):
|
11 |
-
name = "Pixel art"
|
12 |
-
order = 20005
|
13 |
-
model = None
|
14 |
-
|
15 |
-
def ui(self):
|
16 |
-
quantization_methods = ['Median cut', 'Maximum coverage', 'Fast octree']
|
17 |
-
dither_methods = ['None', 'Floyd-Steinberg']
|
18 |
-
|
19 |
-
if features.check_feature("libimagequant"):
|
20 |
-
quantization_methods.insert(0, "libimagequant")
|
21 |
-
|
22 |
-
with gr.Blocks():
|
23 |
-
with gr.Accordion(label="Pixel art", open=False):
|
24 |
-
enabled = gr.Checkbox(label="Enable", value=False)
|
25 |
-
with gr.Row():
|
26 |
-
downscale = gr.Slider(label="Downscale", minimum=1, maximum=32, step=2, value=8)
|
27 |
-
need_rescale = gr.Checkbox(label="Rescale to original size", value=True)
|
28 |
-
with gr.Tabs():
|
29 |
-
with gr.TabItem("Color"):
|
30 |
-
enable_color_limit = gr.Checkbox(label="Enable", value=False)
|
31 |
-
number_of_colors = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
|
32 |
-
quantization_method = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
|
33 |
-
dither_method = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
34 |
-
use_k_means = gr.Checkbox(label="Enable k-means for color quantization", value=True)
|
35 |
-
with gr.TabItem("Grayscale"):
|
36 |
-
is_grayscale = gr.Checkbox(label="Enable", value=False)
|
37 |
-
number_of_shades = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
|
38 |
-
quantization_method_grayscale = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
|
39 |
-
dither_method_grayscale = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
40 |
-
use_k_means_grayscale = gr.Checkbox(label="Enable k-means for color quantization", value=True)
|
41 |
-
with gr.TabItem("Black and white"):
|
42 |
-
with gr.Row():
|
43 |
-
black_and_white = gr.Checkbox(label="Enable", value=False)
|
44 |
-
inversed_black_and_white = gr.Checkbox(label="Inverse", value=False)
|
45 |
-
with gr.Row():
|
46 |
-
black_and_white_threshold = gr.Slider(label="Threshold", minimum=1, maximum=256, step=1, value=128)
|
47 |
-
with gr.TabItem("Custom color palette"):
|
48 |
-
use_color_palette = gr.Checkbox(label="Enable", value=False)
|
49 |
-
palette_image=gr.Image(label="Color palette image", type="pil")
|
50 |
-
palette_colors = gr.Slider(label="Palette Size (only for complex images)", minimum=1, maximum=256, step=1, value=16)
|
51 |
-
dither_method_palette = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
52 |
-
|
53 |
-
return {
|
54 |
-
"enabled": enabled,
|
55 |
-
|
56 |
-
"downscale": downscale,
|
57 |
-
"need_rescale": need_rescale,
|
58 |
-
|
59 |
-
"enable_color_limit": enable_color_limit,
|
60 |
-
"number_of_colors": number_of_colors,
|
61 |
-
"quantization_method": quantization_method,
|
62 |
-
"dither_method": dither_method,
|
63 |
-
"use_k_means": use_k_means,
|
64 |
-
|
65 |
-
"is_grayscale": is_grayscale,
|
66 |
-
"number_of_shades": number_of_shades,
|
67 |
-
"quantization_method_grayscale": quantization_method_grayscale,
|
68 |
-
"dither_method_grayscale": dither_method_grayscale,
|
69 |
-
"use_k_means_grayscale": use_k_means_grayscale,
|
70 |
-
|
71 |
-
"use_color_palette": use_color_palette,
|
72 |
-
"palette_image": palette_image,
|
73 |
-
"palette_colors": palette_colors,
|
74 |
-
"dither_method_palette": dither_method_palette,
|
75 |
-
|
76 |
-
"black_and_white": black_and_white,
|
77 |
-
"inversed_black_and_white": inversed_black_and_white,
|
78 |
-
"black_and_white_threshold": black_and_white_threshold,
|
79 |
-
}
|
80 |
-
|
81 |
-
|
82 |
-
def process(
|
83 |
-
self,
|
84 |
-
pp: scripts_postprocessing.PostprocessedImage,
|
85 |
-
|
86 |
-
enabled,
|
87 |
-
|
88 |
-
downscale,
|
89 |
-
need_rescale,
|
90 |
-
|
91 |
-
enable_color_limit,
|
92 |
-
number_of_colors,
|
93 |
-
quantization_method,
|
94 |
-
dither_method,
|
95 |
-
use_k_means,
|
96 |
-
|
97 |
-
is_grayscale,
|
98 |
-
number_of_shades,
|
99 |
-
quantization_method_grayscale,
|
100 |
-
dither_method_grayscale,
|
101 |
-
use_k_means_grayscale,
|
102 |
-
|
103 |
-
use_color_palette,
|
104 |
-
palette_image,
|
105 |
-
palette_colors,
|
106 |
-
dither_method_palette,
|
107 |
-
|
108 |
-
black_and_white,
|
109 |
-
inversed_black_and_white,
|
110 |
-
black_and_white_threshold
|
111 |
-
):
|
112 |
-
dither = DITHER_METHODS[dither_method]
|
113 |
-
quantize = QUANTIZATION_METHODS[quantization_method]
|
114 |
-
dither_grayscale = DITHER_METHODS[dither_method_grayscale]
|
115 |
-
quantize_grayscale = QUANTIZATION_METHODS[quantization_method_grayscale]
|
116 |
-
dither_palette = DITHER_METHODS[dither_method_palette]
|
117 |
-
|
118 |
-
if not enabled:
|
119 |
-
return
|
120 |
-
|
121 |
-
def process_image(original_image):
|
122 |
-
original_width, original_height = original_image.size
|
123 |
-
|
124 |
-
if original_image.mode != "RGB":
|
125 |
-
new_image = original_image.convert("RGB")
|
126 |
-
else:
|
127 |
-
new_image = original_image
|
128 |
-
|
129 |
-
new_image = downscale_image(new_image, downscale)
|
130 |
-
|
131 |
-
if use_color_palette:
|
132 |
-
new_image = limit_colors(
|
133 |
-
image=new_image,
|
134 |
-
palette=palette_image,
|
135 |
-
palette_colors=palette_colors,
|
136 |
-
dither=dither_palette
|
137 |
-
)
|
138 |
-
|
139 |
-
if black_and_white:
|
140 |
-
new_image = convert_to_black_and_white(new_image, black_and_white_threshold, inversed_black_and_white)
|
141 |
-
|
142 |
-
if is_grayscale:
|
143 |
-
new_image = convert_to_grayscale(new_image)
|
144 |
-
new_image = limit_colors(
|
145 |
-
image=new_image,
|
146 |
-
limit=int(number_of_shades),
|
147 |
-
quantize=quantize_grayscale,
|
148 |
-
dither=dither_grayscale,
|
149 |
-
use_k_means=use_k_means_grayscale
|
150 |
-
)
|
151 |
-
|
152 |
-
if enable_color_limit:
|
153 |
-
new_image = limit_colors(
|
154 |
-
image=new_image,
|
155 |
-
limit=int(number_of_colors),
|
156 |
-
quantize=quantize,
|
157 |
-
dither=dither,
|
158 |
-
use_k_means=use_k_means
|
159 |
-
)
|
160 |
-
|
161 |
-
if need_rescale:
|
162 |
-
new_image = resize_image(new_image, (original_width, original_height))
|
163 |
-
|
164 |
-
return new_image.convert('RGBA')
|
165 |
-
|
166 |
-
pp.image = process_image(pp.image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|