leonelhs commited on
Commit
6d0cee8
·
1 Parent(s): f16824f
Files changed (5) hide show
  1. .gitignore +3 -0
  2. app.py +102 -0
  3. filters.py +66 -0
  4. requirements.txt +1 -0
  5. utils.py +6 -0
.gitignore ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ .idea/
2
+ __pycache__/
3
+ playground.py
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from utils import *
4
+ from filters import *
5
+
6
+ builtin_funcs = {
7
+ "Blur": blur,
8
+ "Contour": contour,
9
+ "Detail": detail,
10
+ "Edge enhance": edge,
11
+ "Edge enhance more": edgeMore,
12
+ "Emboss": emboss,
13
+ "Find edges": findEdges,
14
+ "Sharpen": sharpen,
15
+ "Smoot": smoot,
16
+ "Smoot More": smootMore
17
+ }
18
+
19
+ radio_funcs = {
20
+ "Gaussian Blur": gaussianBlur,
21
+ "Box Blur": boxBlur
22
+ }
23
+
24
+ builtin_choices = keys(builtin_funcs)
25
+ radio_choices = keys(radio_funcs)
26
+
27
+
28
+ def convolution(image, kernel, size, scale, offset, iterations):
29
+ if isinstance(kernel, str):
30
+ size = split_numbers(size)
31
+ kernel = split_numbers(kernel)
32
+ result = image.filter(ImageFilter.Kernel(size, kernel, scale, offset))
33
+ if iterations == 1:
34
+ return result
35
+ return convolution(result, kernel, size, scale, offset, iterations - 1)
36
+
37
+
38
+ def filters_builtin(image, func):
39
+ return builtin_funcs[func](image)
40
+
41
+
42
+ def filters_radio(image, func, radio):
43
+ return radio_funcs[func](image, radio)
44
+
45
+
46
+ def mirror(x):
47
+ return x
48
+
49
+
50
+ footer = r"""
51
+ <center>
52
+ <b>
53
+ Demo based on <a href='https://github.com/leonelhs/pillow-gui'>Pillow Tool Utility</a>
54
+ </b>
55
+ </center>
56
+ """
57
+
58
+ with gr.Blocks(title="Pillow Tool") as app:
59
+ gr.HTML("<center><h1>Pillow Tool Utility</h1></center>")
60
+
61
+ with gr.Row(equal_height=False):
62
+
63
+ with gr.Column():
64
+ input_img = gr.Image(type="pil", label="Input image")
65
+ with gr.Accordion(label="Basic filters", open=True):
66
+ drp_builtin = gr.Dropdown(choices=builtin_choices, label="Filter functions", value="Blur")
67
+ gr.HTML("</br>")
68
+ bti_btn = gr.Button(value="Filter")
69
+ with gr.Accordion(label="Blur filters", open=False):
70
+ rdi_builtin = gr.Dropdown(choices=radio_choices, label="Filter Blur", value="Gaussian Blur")
71
+ blr_rad = gr.Slider(1, 100, step=1, value=1, label="Blur radius")
72
+ gr.HTML("</br>")
73
+ rad_btn = gr.Button(value="Blurry")
74
+ with gr.Accordion(label="Unsharp filter", open=False):
75
+ unsr_rad = gr.Slider(1, 100, step=1, value=1, label="Unshap mask radius")
76
+ unsr_per = gr.Slider(1, 200, step=1, value=1, label="Unshap percent")
77
+ unsr_tre = gr.Slider(1, 10, step=1, value=1, label="Unshap threshold")
78
+ gr.HTML("</br>")
79
+ srp_btn = gr.Button(value="Unsharp")
80
+ with gr.Accordion(label="Convolution filter", open=False):
81
+ ker_txt = gr.Textbox(label="Kernel", value="-1, -1, -1, -1, 9, -1, -1, -1, -1")
82
+ siz_txt = gr.Textbox(label="Size", value="3,3")
83
+ scl_txt = gr.Number(label="Scale", value=1)
84
+ ofs_txt = gr.Number(label="Offset", value=0)
85
+ itr_txt = gr.Number(label="Iterations", value=1, minimum=1, maximum=10)
86
+ gr.HTML("</br>")
87
+ ker_btn = gr.Button(value="Convolution")
88
+
89
+ with gr.Column():
90
+ output_img = gr.Image(type="pil", label="Input image", interactive=False)
91
+ kpt_btn = gr.Button(value="Keep it", variant="primary")
92
+
93
+ bti_btn.click(filters_builtin, [input_img, drp_builtin], [output_img])
94
+ rad_btn.click(filters_radio, inputs=[input_img, rdi_builtin, blr_rad], outputs=[output_img])
95
+ srp_btn.click(unsharpMask, inputs=[input_img, unsr_rad, unsr_per, unsr_tre], outputs=[output_img])
96
+ ker_btn.click(convolution, [input_img, ker_txt, siz_txt, scl_txt, ofs_txt, itr_txt], [output_img])
97
+ kpt_btn.click(mirror, inputs=[output_img], outputs=[input_img])
98
+
99
+ with gr.Row():
100
+ gr.HTML(footer)
101
+
102
+ app.launch(share=False, debug=True, enable_queue=True, show_error=True)
filters.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import ImageFilter
2
+
3
+
4
+ def blur(image):
5
+ image_working = image.filter(ImageFilter.BLUR())
6
+ return image_working
7
+
8
+
9
+ def contour(image):
10
+ image_working = image.filter(ImageFilter.CONTOUR())
11
+ return image_working
12
+
13
+
14
+ def detail(image):
15
+ image_working = image.filter(ImageFilter.DETAIL())
16
+ return image_working
17
+
18
+
19
+ def edge(image):
20
+ image_working = image.filter(ImageFilter.EDGE_ENHANCE())
21
+ return image_working
22
+
23
+
24
+ def edgeMore(image):
25
+ image_working = image.filter(ImageFilter.EDGE_ENHANCE_MORE())
26
+ return image_working
27
+
28
+
29
+ def emboss(image):
30
+ image_working = image.filter(ImageFilter.EMBOSS())
31
+ return image_working
32
+
33
+
34
+ def findEdges(image):
35
+ image_working = image.filter(ImageFilter.FIND_EDGES())
36
+ return image_working
37
+
38
+
39
+ def sharpen(image):
40
+ image_working = image.filter(ImageFilter.SHARPEN())
41
+ return image_working
42
+
43
+
44
+ def smoot(image):
45
+ image_working = image.filter(ImageFilter.SMOOTH())
46
+ return image_working
47
+
48
+
49
+ def smootMore(image):
50
+ image_working = image.filter(ImageFilter.SMOOTH_MORE())
51
+ return image_working
52
+
53
+
54
+ def gaussianBlur(image, radius):
55
+ image_working = image.filter(ImageFilter.GaussianBlur(radius=radius))
56
+ return image_working
57
+
58
+
59
+ def boxBlur(image, radius):
60
+ image_working = image.filter(ImageFilter.BoxBlur(radius=radius))
61
+ return image_working
62
+
63
+
64
+ def unsharpMask(image, radius, percent, threshold):
65
+ image_working = image.filter(ImageFilter.UnsharpMask(radius=radius, percent=percent, threshold=threshold))
66
+ return image_working
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ pillow~=9.5.0
utils.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ def keys(dictionary: dict):
2
+ return [k for k, v in dictionary.items()]
3
+
4
+
5
+ def split_numbers(numbers: str):
6
+ return [int(i) for i in numbers.split(",")]