Spaces:
Runtime error
Runtime error
Commit
·
06819c2
1
Parent(s):
2fc0f3f
Delete pixelart.py
Browse files- pixelart.py +0 -148
pixelart.py
DELETED
@@ -1,148 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
|
3 |
-
import modules.scripts as scripts
|
4 |
-
from modules import images
|
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_black_and_white, convert_to_grayscale
|
8 |
-
|
9 |
-
class Script(scripts.Script):
|
10 |
-
def title(self):
|
11 |
-
return "Pixel art"
|
12 |
-
|
13 |
-
|
14 |
-
def show(self, is_img2img):
|
15 |
-
return scripts.AlwaysVisible
|
16 |
-
|
17 |
-
|
18 |
-
def ui(self, is_img2img):
|
19 |
-
quantization_methods = ['Median cut', 'Maximum coverage', 'Fast octree']
|
20 |
-
dither_methods = ['None', 'Floyd-Steinberg']
|
21 |
-
|
22 |
-
with gr.Accordion("Pixel art", open=False):
|
23 |
-
with gr.Row():
|
24 |
-
enabled = gr.Checkbox(label="Enable", value=False)
|
25 |
-
|
26 |
-
with gr.Column():
|
27 |
-
with gr.Row():
|
28 |
-
downscale = gr.Slider(label="Downscale", minimum=1, maximum=32, step=2, value=8)
|
29 |
-
need_rescale = gr.Checkbox(label="Rescale to original size", value=True)
|
30 |
-
with gr.Tabs():
|
31 |
-
with gr.TabItem("Color"):
|
32 |
-
enable_color_limit = gr.Checkbox(label="Enable", value=False)
|
33 |
-
number_of_colors = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
|
34 |
-
quantization_method = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
|
35 |
-
dither_method = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
36 |
-
use_k_means = gr.Checkbox(label="Enable k-means for color quantization", value=True)
|
37 |
-
with gr.TabItem("Grayscale"):
|
38 |
-
is_grayscale = gr.Checkbox(label="Enable", value=False)
|
39 |
-
number_of_shades = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
|
40 |
-
quantization_method_grayscale = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
|
41 |
-
dither_method_grayscale = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
42 |
-
use_k_means_grayscale = gr.Checkbox(label="Enable k-means for color quantization", value=True)
|
43 |
-
with gr.TabItem("Black and white"):
|
44 |
-
with gr.Row():
|
45 |
-
is_black_and_white = gr.Checkbox(label="Enable", value=False)
|
46 |
-
is_inversed_black_and_white = gr.Checkbox(label="Inverse", value=False)
|
47 |
-
with gr.Row():
|
48 |
-
black_and_white_threshold = gr.Slider(label="Threshold", minimum=1, maximum=256, step=1, value=128)
|
49 |
-
with gr.TabItem("Custom color palette"):
|
50 |
-
use_color_palette = gr.Checkbox(label="Enable", value=False)
|
51 |
-
palette_image=gr.Image(label="Color palette image", type="pil")
|
52 |
-
palette_colors = gr.Slider(label="Palette Size (only for complex images)", minimum=1, maximum=256, step=1, value=16)
|
53 |
-
dither_method_palette = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
|
54 |
-
|
55 |
-
return [enabled, downscale, need_rescale, enable_color_limit, number_of_colors, quantization_method, dither_method, use_k_means, is_grayscale, number_of_shades, quantization_method_grayscale, dither_method_grayscale, use_k_means_grayscale, is_black_and_white, is_inversed_black_and_white, black_and_white_threshold, use_color_palette, palette_image, palette_colors, dither_method_palette]
|
56 |
-
|
57 |
-
def postprocess(
|
58 |
-
self,
|
59 |
-
p,
|
60 |
-
processed,
|
61 |
-
enabled,
|
62 |
-
|
63 |
-
downscale,
|
64 |
-
need_rescale,
|
65 |
-
|
66 |
-
enable_color_limit,
|
67 |
-
number_of_colors,
|
68 |
-
quantization_method,
|
69 |
-
dither_method,
|
70 |
-
use_k_means,
|
71 |
-
|
72 |
-
is_grayscale,
|
73 |
-
number_of_shades,
|
74 |
-
quantization_method_grayscale,
|
75 |
-
dither_method_grayscale,
|
76 |
-
use_k_means_grayscale,
|
77 |
-
|
78 |
-
is_black_and_white,
|
79 |
-
is_inversed_black_and_white,
|
80 |
-
black_and_white_threshold,
|
81 |
-
|
82 |
-
use_color_palette,
|
83 |
-
palette_image,
|
84 |
-
palette_colors,
|
85 |
-
dither_method_palette
|
86 |
-
):
|
87 |
-
if not enabled:
|
88 |
-
return
|
89 |
-
|
90 |
-
dither = DITHER_METHODS[dither_method]
|
91 |
-
quantize = QUANTIZATION_METHODS[quantization_method]
|
92 |
-
dither_grayscale = DITHER_METHODS[dither_method_grayscale]
|
93 |
-
quantize_grayscale = QUANTIZATION_METHODS[quantization_method_grayscale]
|
94 |
-
dither_palette = DITHER_METHODS[dither_method_palette]
|
95 |
-
|
96 |
-
def process_image(original_image):
|
97 |
-
original_width, original_height = original_image.size
|
98 |
-
|
99 |
-
if original_image.mode != "RGB":
|
100 |
-
new_image = original_image.convert("RGB")
|
101 |
-
else:
|
102 |
-
new_image = original_image
|
103 |
-
|
104 |
-
new_image = downscale_image(new_image, downscale)
|
105 |
-
|
106 |
-
if use_color_palette:
|
107 |
-
new_image = limit_colors(
|
108 |
-
image=new_image,
|
109 |
-
palette=palette_image,
|
110 |
-
palette_colors=palette_colors,
|
111 |
-
dither=dither_palette
|
112 |
-
)
|
113 |
-
|
114 |
-
if is_black_and_white:
|
115 |
-
new_image = convert_to_black_and_white(new_image, black_and_white_threshold, is_inversed_black_and_white)
|
116 |
-
|
117 |
-
if is_grayscale:
|
118 |
-
new_image = convert_to_grayscale(new_image)
|
119 |
-
new_image = limit_colors(
|
120 |
-
image=new_image,
|
121 |
-
limit=int(number_of_shades),
|
122 |
-
quantize=quantize_grayscale,
|
123 |
-
dither=dither_grayscale,
|
124 |
-
use_k_means=use_k_means_grayscale
|
125 |
-
)
|
126 |
-
|
127 |
-
if enable_color_limit:
|
128 |
-
new_image = limit_colors(
|
129 |
-
image=new_image,
|
130 |
-
limit=int(number_of_colors),
|
131 |
-
quantize=quantize,
|
132 |
-
dither=dither,
|
133 |
-
use_k_means=use_k_means
|
134 |
-
)
|
135 |
-
|
136 |
-
if need_rescale:
|
137 |
-
new_image = resize_image(new_image, (original_width, original_height))
|
138 |
-
|
139 |
-
return new_image.convert('RGBA')
|
140 |
-
|
141 |
-
for i in range(len(processed.images)):
|
142 |
-
pixel_image = process_image(processed.images[i])
|
143 |
-
processed.images.append(pixel_image)
|
144 |
-
|
145 |
-
images.save_image(pixel_image, p.outpath_samples, "pixel",
|
146 |
-
processed.seed + i, processed.prompt, opts.samples_format, info= processed.info, p=p)
|
147 |
-
|
148 |
-
return processed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|