Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,130 +1,130 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from color_quantization import (
|
3 |
-
kmeans_quantization,
|
4 |
-
median_cut_quantization,
|
5 |
-
floyd_steinberg_dithering,
|
6 |
-
median_cut_perceptual_weighting
|
7 |
-
)
|
8 |
-
from pixelation import pixelate_image, mosaic_pixelation, oil_paint_pixelation, hierarchical_pixelation
|
9 |
-
import math
|
10 |
-
|
11 |
-
|
12 |
-
# 定义像素化和颜色量化处理函数
|
13 |
-
def pixelate_and_quantize(image, pixel_size, n_colors, methods, interpolation, pixelation_types):
|
14 |
-
results = []
|
15 |
-
|
16 |
-
# 根据用户选择的像素化方法进行像素化处理
|
17 |
-
for pixelation_type in pixelation_types:
|
18 |
-
if pixelation_type == "Classic Nearest Neighbor":
|
19 |
-
pixelated_img = pixelate_image(image, pixel_size, interpolation)
|
20 |
-
elif pixelation_type == "Mosaic":
|
21 |
-
pixelated_img = mosaic_pixelation(image, pixel_size)
|
22 |
-
elif pixelation_type == "Oil Painting":
|
23 |
-
pixelated_img = oil_paint_pixelation(image, pixel_size)
|
24 |
-
elif pixelation_type == "Hierarchical":
|
25 |
-
pixelated_img = hierarchical_pixelation(image, pixel_size, pixel_size * 2)
|
26 |
-
else:
|
27 |
-
raise ValueError(f"未知的像素化方法: {pixelation_type}")
|
28 |
-
|
29 |
-
# 根据用户选择的量化方法进行颜色量化,并将量化后的图像添加到列表中
|
30 |
-
for method in methods:
|
31 |
-
if method == "K-Means":
|
32 |
-
quantized_img = kmeans_quantization(pixelated_img, n_colors)
|
33 |
-
elif method == "Median Cut":
|
34 |
-
quantized_img = median_cut_quantization(pixelated_img, n_colors)
|
35 |
-
elif method == "Floyd-Steinberg Dithering":
|
36 |
-
quantized_img = floyd_steinberg_dithering(pixelated_img, n_colors)
|
37 |
-
elif method == "
|
38 |
-
quantized_img = median_cut_perceptual_weighting(pixelated_img, n_colors)
|
39 |
-
else:
|
40 |
-
raise ValueError(f"未知的量化方法: {method}")
|
41 |
-
|
42 |
-
# 只返回量化后的图像
|
43 |
-
results.append(quantized_img)
|
44 |
-
|
45 |
-
return results
|
46 |
-
|
47 |
-
|
48 |
-
# Gradio 用户界面
|
49 |
-
def gradio_interface():
|
50 |
-
with gr.Blocks(title="Image Pixelation Tool") as demo:
|
51 |
-
gr.Markdown("## 像素化图片工具 (AI像素修补)")
|
52 |
-
with gr.Row():
|
53 |
-
with gr.Column(scale=4):
|
54 |
-
gr.Markdown("""
|
55 |
-
此工具提供了多种图像像素化效果和颜色量化算法,包括经典的邻近像素化、马赛克效果、油画效果以及层次像素化。
|
56 |
-
用户可以根据需要选择不同的像素化方法并对图像进行颜色量化处理。
|
57 |
-
|
58 |
-
### 许可协议(License)
|
59 |
-
本工具基于 **MPL 2.0(Mozilla Public License 2.0)** 协议发布。该协议允许修改和分发源代码,且允许商业使用,前提是对源代码的修改部分仍需保持开源。
|
60 |
-
|
61 |
-
- **允许**:修改、分发、用于商业用途。
|
62 |
-
- **限制**:修改过的源代码必须继续保持开源。
|
63 |
-
|
64 |
-
[查看详细的 MPL 2.0 协议](https://www.mozilla.org/en-US/MPL/2.0/)
|
65 |
-
|
66 |
-
**注意:如需将本工具用于商业用途,请联系作者以获得进一步的授权。**
|
67 |
-
|
68 |
-
联系方式:[email protected]
|
69 |
-
|
70 |
-
版权所有 © 2024
|
71 |
-
""")
|
72 |
-
with gr.Column(scale=1):
|
73 |
-
gr.Image(value="https://gravatar.com/avatar/483611663515c5a569c10664f3abf78c8d01498febd864a3413ecc8fe01dd740.jpg?s=256", show_download_button=False,interactive=False,show_fullscreen_button=False)
|
74 |
-
|
75 |
-
with gr.Row():
|
76 |
-
with gr.Column():
|
77 |
-
image_input = gr.Image(type="pil", label="上传图片")
|
78 |
-
|
79 |
-
with gr.Column():
|
80 |
-
pixel_size_slider = gr.Slider(minimum=2, maximum=100, value=10, step=1, label="选择像素块大小")
|
81 |
-
color_slider = gr.Slider(minimum=2, maximum=128, value=16, step=1, label="选择颜色数量")
|
82 |
-
|
83 |
-
method_checkboxes = gr.CheckboxGroup(
|
84 |
-
choices=["K-Means", "Median Cut", "Floyd-Steinberg Dithering", "MedianCut(PerceptualWeighting)"],
|
85 |
-
value=["K-Means"],
|
86 |
-
label="选择颜色量化方法"
|
87 |
-
)
|
88 |
-
pixelation_checkboxes = gr.CheckboxGroup(
|
89 |
-
choices=["Classic Nearest Neighbor", "Mosaic", "Oil Painting", "Hierarchical"],
|
90 |
-
value=["Classic Nearest Neighbor"],
|
91 |
-
label="选择像素化方法"
|
92 |
-
)
|
93 |
-
interpolation_radio = gr.Radio(
|
94 |
-
choices=["Nearest", "Bilinear", "Bicubic", "Lanczos"],
|
95 |
-
value="Nearest",
|
96 |
-
label="选择插值方法(仅对Classic Nearest Neighbour有效)"
|
97 |
-
)
|
98 |
-
|
99 |
-
btn = gr.Button("生成像素化图片")
|
100 |
-
|
101 |
-
@gr.render(inputs=[image_input, pixel_size_slider, color_slider, method_checkboxes, interpolation_radio,
|
102 |
-
pixelation_checkboxes], triggers=[btn.click])
|
103 |
-
def show_pictures(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types):
|
104 |
-
num_outputs = len(methods) * len(pixelation_types)
|
105 |
-
num_outputs = min(num_outputs, 16)
|
106 |
-
images = pixelate_and_quantize(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types)
|
107 |
-
cols = math.ceil(math.sqrt(num_outputs))
|
108 |
-
rows = math.ceil(num_outputs / cols)
|
109 |
-
for i in range(rows):
|
110 |
-
single_row = gr.Row()
|
111 |
-
with single_row:
|
112 |
-
for j in range(cols):
|
113 |
-
single_col = gr.Column()
|
114 |
-
idx = i * cols + j
|
115 |
-
with single_col:
|
116 |
-
if idx < num_outputs:
|
117 |
-
# 计算当前的 pixelation 和 quantization 方法
|
118 |
-
current_pixelation = pixelation_types[idx // len(methods)]
|
119 |
-
current_method = methods[idx % len(methods)]
|
120 |
-
# 更新图片的标签,包含像素化方法和颜色量化方法
|
121 |
-
label = f"像素化: {current_pixelation}, 颜色量化: {current_method}"
|
122 |
-
gr.Image(images[idx], label=label, format="png")
|
123 |
-
idx += 1
|
124 |
-
return demo
|
125 |
-
|
126 |
-
|
127 |
-
# 启动 Gradio 界面
|
128 |
-
if __name__ == "__main__":
|
129 |
-
demo = gradio_interface()
|
130 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from color_quantization import (
|
3 |
+
kmeans_quantization,
|
4 |
+
median_cut_quantization,
|
5 |
+
floyd_steinberg_dithering,
|
6 |
+
median_cut_perceptual_weighting
|
7 |
+
)
|
8 |
+
from pixelation import pixelate_image, mosaic_pixelation, oil_paint_pixelation, hierarchical_pixelation
|
9 |
+
import math
|
10 |
+
|
11 |
+
|
12 |
+
# 定义像素化和颜色量化处理函数
|
13 |
+
def pixelate_and_quantize(image, pixel_size, n_colors, methods, interpolation, pixelation_types):
|
14 |
+
results = []
|
15 |
+
|
16 |
+
# 根据用户选择的像素化方法进行像素化处理
|
17 |
+
for pixelation_type in pixelation_types:
|
18 |
+
if pixelation_type == "Classic Nearest Neighbor":
|
19 |
+
pixelated_img = pixelate_image(image, pixel_size, interpolation)
|
20 |
+
elif pixelation_type == "Mosaic":
|
21 |
+
pixelated_img = mosaic_pixelation(image, pixel_size)
|
22 |
+
elif pixelation_type == "Oil Painting":
|
23 |
+
pixelated_img = oil_paint_pixelation(image, pixel_size)
|
24 |
+
elif pixelation_type == "Hierarchical":
|
25 |
+
pixelated_img = hierarchical_pixelation(image, pixel_size, pixel_size * 2)
|
26 |
+
else:
|
27 |
+
raise ValueError(f"未知的像素化方法: {pixelation_type}")
|
28 |
+
|
29 |
+
# 根据用户选择的量化方法进行颜色量化,并将量化后的图像添加到列表中
|
30 |
+
for method in methods:
|
31 |
+
if method == "K-Means":
|
32 |
+
quantized_img = kmeans_quantization(pixelated_img, n_colors)
|
33 |
+
elif method == "Median Cut":
|
34 |
+
quantized_img = median_cut_quantization(pixelated_img, n_colors)
|
35 |
+
elif method == "Floyd-Steinberg Dithering":
|
36 |
+
quantized_img = floyd_steinberg_dithering(pixelated_img, n_colors)
|
37 |
+
elif method == "MedianCut(PerceptualWeighting)":
|
38 |
+
quantized_img = median_cut_perceptual_weighting(pixelated_img, n_colors)
|
39 |
+
else:
|
40 |
+
raise ValueError(f"未知的量化方法: {method}")
|
41 |
+
|
42 |
+
# 只返回量化后的图像
|
43 |
+
results.append(quantized_img)
|
44 |
+
|
45 |
+
return results
|
46 |
+
|
47 |
+
|
48 |
+
# Gradio 用户界面
|
49 |
+
def gradio_interface():
|
50 |
+
with gr.Blocks(title="Image Pixelation Tool") as demo:
|
51 |
+
gr.Markdown("## 像素化图片工具 (AI像素修补)")
|
52 |
+
with gr.Row():
|
53 |
+
with gr.Column(scale=4):
|
54 |
+
gr.Markdown("""
|
55 |
+
此工具提供了多种图像像素化效果和颜色量化算法,包括经典的邻近像素化、马赛克效果、油画效果以及层次像素化。
|
56 |
+
用户可以根据需要选择不同的像素化方法并对图像进行颜色量化处理。
|
57 |
+
|
58 |
+
### 许可协议(License)
|
59 |
+
本工具基于 **MPL 2.0(Mozilla Public License 2.0)** 协议发布。该协议允许修改和分发源代码,且允许商业使用,前提是对源代码的修改部分仍需保持开源。
|
60 |
+
|
61 |
+
- **允许**:修改、分发、用于商业用途。
|
62 |
+
- **限制**:修改过的源代码必须继续保持开源。
|
63 |
+
|
64 |
+
[查看详细的 MPL 2.0 协议](https://www.mozilla.org/en-US/MPL/2.0/)
|
65 |
+
|
66 |
+
**注意:如需将本工具用于商业用途,请联系作者以获得进一步的授权。**
|
67 |
+
|
68 |
+
联系方式:[email protected]
|
69 |
+
|
70 |
+
版权所有 © 2024
|
71 |
+
""")
|
72 |
+
with gr.Column(scale=1):
|
73 |
+
gr.Image(value="https://gravatar.com/avatar/483611663515c5a569c10664f3abf78c8d01498febd864a3413ecc8fe01dd740.jpg?s=256", show_download_button=False,interactive=False,show_fullscreen_button=False)
|
74 |
+
|
75 |
+
with gr.Row():
|
76 |
+
with gr.Column():
|
77 |
+
image_input = gr.Image(type="pil", label="上传图片")
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
pixel_size_slider = gr.Slider(minimum=2, maximum=100, value=10, step=1, label="选择像素块大小")
|
81 |
+
color_slider = gr.Slider(minimum=2, maximum=128, value=16, step=1, label="选择颜色数量")
|
82 |
+
|
83 |
+
method_checkboxes = gr.CheckboxGroup(
|
84 |
+
choices=["K-Means", "Median Cut", "Floyd-Steinberg Dithering", "MedianCut(PerceptualWeighting)"],
|
85 |
+
value=["K-Means"],
|
86 |
+
label="选择颜色量化方法"
|
87 |
+
)
|
88 |
+
pixelation_checkboxes = gr.CheckboxGroup(
|
89 |
+
choices=["Classic Nearest Neighbor", "Mosaic", "Oil Painting", "Hierarchical"],
|
90 |
+
value=["Classic Nearest Neighbor"],
|
91 |
+
label="选择像素化方法"
|
92 |
+
)
|
93 |
+
interpolation_radio = gr.Radio(
|
94 |
+
choices=["Nearest", "Bilinear", "Bicubic", "Lanczos"],
|
95 |
+
value="Nearest",
|
96 |
+
label="选择插值方法(仅对Classic Nearest Neighbour有效)"
|
97 |
+
)
|
98 |
+
|
99 |
+
btn = gr.Button("生成像素化图片")
|
100 |
+
|
101 |
+
@gr.render(inputs=[image_input, pixel_size_slider, color_slider, method_checkboxes, interpolation_radio,
|
102 |
+
pixelation_checkboxes], triggers=[btn.click])
|
103 |
+
def show_pictures(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types):
|
104 |
+
num_outputs = len(methods) * len(pixelation_types)
|
105 |
+
num_outputs = min(num_outputs, 16)
|
106 |
+
images = pixelate_and_quantize(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types)
|
107 |
+
cols = math.ceil(math.sqrt(num_outputs))
|
108 |
+
rows = math.ceil(num_outputs / cols)
|
109 |
+
for i in range(rows):
|
110 |
+
single_row = gr.Row()
|
111 |
+
with single_row:
|
112 |
+
for j in range(cols):
|
113 |
+
single_col = gr.Column()
|
114 |
+
idx = i * cols + j
|
115 |
+
with single_col:
|
116 |
+
if idx < num_outputs:
|
117 |
+
# 计算当前的 pixelation 和 quantization 方法
|
118 |
+
current_pixelation = pixelation_types[idx // len(methods)]
|
119 |
+
current_method = methods[idx % len(methods)]
|
120 |
+
# 更新图片的标签,包含像素化方法和颜色量化方法
|
121 |
+
label = f"像素化: {current_pixelation}, 颜色量化: {current_method}"
|
122 |
+
gr.Image(images[idx], label=label, format="png")
|
123 |
+
idx += 1
|
124 |
+
return demo
|
125 |
+
|
126 |
+
|
127 |
+
# 启动 Gradio 界面
|
128 |
+
if __name__ == "__main__":
|
129 |
+
demo = gradio_interface()
|
130 |
+
demo.launch()
|