Spaces:
Runtime error
Runtime error
File size: 6,645 Bytes
1f2b323 |
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 |
import gradio as gr
import modules.scripts as scripts
from modules import images
from modules.shared import opts
from sd_webui_pixelart.utils import DITHER_METHODS, QUANTIZATION_METHODS, downscale_image, limit_colors, resize_image, convert_to_black_and_white, convert_to_grayscale
class Script(scripts.Script):
def title(self):
return "Pixel art"
def show(self, is_img2img):
return scripts.AlwaysVisible
def ui(self, is_img2img):
quantization_methods = ['Median cut', 'Maximum coverage', 'Fast octree']
dither_methods = ['None', 'Floyd-Steinberg']
with gr.Accordion("Pixel art", open=False):
with gr.Row():
enabled = gr.Checkbox(label="Enable", value=False)
with gr.Column():
with gr.Row():
downscale = gr.Slider(label="Downscale", minimum=1, maximum=32, step=2, value=8)
need_rescale = gr.Checkbox(label="Rescale to original size", value=True)
with gr.Tabs():
with gr.TabItem("Color"):
enable_color_limit = gr.Checkbox(label="Enable", value=False)
number_of_colors = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
quantization_method = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
dither_method = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
use_k_means = gr.Checkbox(label="Enable k-means for color quantization", value=True)
with gr.TabItem("Grayscale"):
is_grayscale = gr.Checkbox(label="Enable", value=False)
number_of_shades = gr.Slider(label="Palette Size", minimum=1, maximum=256, step=1, value=16)
quantization_method_grayscale = gr.Radio(choices=quantization_methods, value=quantization_methods[0], label='Colors quantization method')
dither_method_grayscale = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
use_k_means_grayscale = gr.Checkbox(label="Enable k-means for color quantization", value=True)
with gr.TabItem("Black and white"):
with gr.Row():
is_black_and_white = gr.Checkbox(label="Enable", value=False)
is_inversed_black_and_white = gr.Checkbox(label="Inverse", value=False)
with gr.Row():
black_and_white_threshold = gr.Slider(label="Threshold", minimum=1, maximum=256, step=1, value=128)
with gr.TabItem("Custom color palette"):
use_color_palette = gr.Checkbox(label="Enable", value=False)
palette_image=gr.Image(label="Color palette image", type="pil")
palette_colors = gr.Slider(label="Palette Size (only for complex images)", minimum=1, maximum=256, step=1, value=16)
dither_method_palette = gr.Radio(choices=dither_methods, value=dither_methods[0], label='Colors dither method')
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]
def postprocess(
self,
p,
processed,
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
):
if not enabled:
return
dither = DITHER_METHODS[dither_method]
quantize = QUANTIZATION_METHODS[quantization_method]
dither_grayscale = DITHER_METHODS[dither_method_grayscale]
quantize_grayscale = QUANTIZATION_METHODS[quantization_method_grayscale]
dither_palette = DITHER_METHODS[dither_method_palette]
def process_image(original_image):
original_width, original_height = original_image.size
if original_image.mode != "RGB":
new_image = original_image.convert("RGB")
else:
new_image = original_image
new_image = downscale_image(new_image, downscale)
if use_color_palette:
new_image = limit_colors(
image=new_image,
palette=palette_image,
palette_colors=palette_colors,
dither=dither_palette
)
if is_black_and_white:
new_image = convert_to_black_and_white(new_image, black_and_white_threshold, is_inversed_black_and_white)
if is_grayscale:
new_image = convert_to_grayscale(new_image)
new_image = limit_colors(
image=new_image,
limit=int(number_of_shades),
quantize=quantize_grayscale,
dither=dither_grayscale,
use_k_means=use_k_means_grayscale
)
if enable_color_limit:
new_image = limit_colors(
image=new_image,
limit=int(number_of_colors),
quantize=quantize,
dither=dither,
use_k_means=use_k_means
)
if need_rescale:
new_image = resize_image(new_image, (original_width, original_height))
return new_image.convert('RGBA')
for i in range(len(processed.images)):
pixel_image = process_image(processed.images[i])
processed.images.append(pixel_image)
images.save_image(pixel_image, p.outpath_samples, "pixel",
processed.seed + i, processed.prompt, opts.samples_format, info= processed.info, p=p)
return processed
|