|
import gradio as gr |
|
|
|
from utils import * |
|
from filters import * |
|
|
|
builtin_funcs = { |
|
"Blur": blur, |
|
"Contour": contour, |
|
"Detail": detail, |
|
"Edge enhance": edge, |
|
"Edge enhance more": edgeMore, |
|
"Emboss": emboss, |
|
"Find edges": findEdges, |
|
"Sharpen": sharpen, |
|
"Smoot": smoot, |
|
"Smoot More": smootMore |
|
} |
|
|
|
radio_funcs = { |
|
"Gaussian Blur": gaussianBlur, |
|
"Box Blur": boxBlur |
|
} |
|
|
|
builtin_choices = keys(builtin_funcs) |
|
radio_choices = keys(radio_funcs) |
|
|
|
|
|
def convolution(image, kernel, size, scale, offset, iterations): |
|
if isinstance(kernel, str): |
|
size = split_numbers(size) |
|
kernel = split_numbers(kernel) |
|
result = image.filter(ImageFilter.Kernel(size, kernel, scale, offset)) |
|
if iterations == 1: |
|
return result |
|
return convolution(result, kernel, size, scale, offset, iterations - 1) |
|
|
|
|
|
def filters_builtin(image, func): |
|
return builtin_funcs[func](image) |
|
|
|
|
|
def filters_radio(image, func, radio): |
|
return radio_funcs[func](image, radio) |
|
|
|
|
|
def mirror(x): |
|
return x |
|
|
|
|
|
footer = r""" |
|
<center> |
|
<b> |
|
Demo based on <a href='https://github.com/leonelhs/pillow-gui'>Pillow Tool Utility</a> |
|
</b> |
|
</center> |
|
""" |
|
|
|
with gr.Blocks(title="Pillow Tool") as app: |
|
gr.HTML("<center><h1>Pillow Tool Utility</h1></center>") |
|
|
|
with gr.Row(equal_height=False): |
|
|
|
with gr.Column(): |
|
input_img = gr.Image(type="pil", label="Input image") |
|
with gr.Accordion(label="Basic filters", open=True): |
|
drp_builtin = gr.Dropdown(choices=builtin_choices, label="Filter functions", value="Blur") |
|
gr.HTML("</br>") |
|
bti_btn = gr.Button(value="Filter") |
|
with gr.Accordion(label="Blur filters", open=False): |
|
rdi_builtin = gr.Dropdown(choices=radio_choices, label="Filter Blur", value="Gaussian Blur") |
|
blr_rad = gr.Slider(1, 100, step=1, value=1, label="Blur radius") |
|
gr.HTML("</br>") |
|
rad_btn = gr.Button(value="Blurry") |
|
with gr.Accordion(label="Unsharp filter", open=False): |
|
unsr_rad = gr.Slider(1, 100, step=1, value=1, label="Unshap mask radius") |
|
unsr_per = gr.Slider(1, 200, step=1, value=1, label="Unshap percent") |
|
unsr_tre = gr.Slider(1, 10, step=1, value=1, label="Unshap threshold") |
|
gr.HTML("</br>") |
|
srp_btn = gr.Button(value="Unsharp") |
|
with gr.Accordion(label="Convolution filter", open=False): |
|
ker_txt = gr.Textbox(label="Kernel", value="-1, -1, -1, -1, 9, -1, -1, -1, -1") |
|
siz_txt = gr.Textbox(label="Kernel Size", value="3,3") |
|
with gr.Row(): |
|
scl_txt = gr.Number(label="Scale", value=1) |
|
ofs_txt = gr.Number(label="Offset", value=0) |
|
itr_txt = gr.Number(label="Iterations", value=1, minimum=1, maximum=10) |
|
gr.HTML("</br>") |
|
ker_btn = gr.Button(value="Convolution") |
|
|
|
with gr.Column(): |
|
output_img = gr.Image(type="pil", label="Output image", interactive=False) |
|
kpt_btn = gr.Button(value="Keep it", variant="primary") |
|
gr.ClearButton(components=[input_img, output_img]) |
|
|
|
bti_btn.click(filters_builtin, [input_img, drp_builtin], [output_img]) |
|
rad_btn.click(filters_radio, inputs=[input_img, rdi_builtin, blr_rad], outputs=[output_img]) |
|
srp_btn.click(unsharpMask, inputs=[input_img, unsr_rad, unsr_per, unsr_tre], outputs=[output_img]) |
|
ker_btn.click(convolution, [input_img, ker_txt, siz_txt, scl_txt, ofs_txt, itr_txt], [output_img]) |
|
kpt_btn.click(mirror, inputs=[output_img], outputs=[input_img]) |
|
|
|
with gr.Row(): |
|
gr.HTML(footer) |
|
|
|
app.launch(share=False, debug=True, enable_queue=True, show_error=True) |
|
|