Spaces:
Running
Running
Huxxshadow
commited on
Upload 4 files
Browse files- .gitattributes +1 -0
- Example.png +3 -0
- app_zh_private.py +131 -0
- pixelation.py +6 -1
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Example.png filter=lfs diff=lfs merge=lfs -text
|
Example.png
ADDED
Git LFS Details
|
app_zh_private.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
with gr.Row():
|
75 |
+
gr.Image(value="Example.png", show_download_button=True,interactive=False,show_fullscreen_button=False)
|
76 |
+
with gr.Row():
|
77 |
+
with gr.Column():
|
78 |
+
image_input = gr.Image(type="pil", label="上传图片")
|
79 |
+
|
80 |
+
with gr.Column():
|
81 |
+
pixel_size_slider = gr.Slider(minimum=2, maximum=100, value=10, step=1, label="选择像素块大小")
|
82 |
+
color_slider = gr.Slider(minimum=2, maximum=128, value=16, step=1, label="选择颜色数量")
|
83 |
+
|
84 |
+
method_checkboxes = gr.CheckboxGroup(
|
85 |
+
choices=["K-Means", "Median Cut", "Floyd-Steinberg Dithering", "MedianCut(PerceptualWeighting)"],
|
86 |
+
value=["K-Means"],
|
87 |
+
label="选择颜色量化方法"
|
88 |
+
)
|
89 |
+
pixelation_checkboxes = gr.CheckboxGroup(
|
90 |
+
choices=["Classic Nearest Neighbor", "Mosaic", "Oil Painting", "Hierarchical"],
|
91 |
+
value=["Classic Nearest Neighbor"],
|
92 |
+
label="选择像素化方法"
|
93 |
+
)
|
94 |
+
interpolation_radio = gr.Radio(
|
95 |
+
choices=["Nearest", "Bilinear", "Bicubic", "Lanczos"],
|
96 |
+
value="Nearest",
|
97 |
+
label="选择插值方法(仅对Classic Nearest Neighbour有效)"
|
98 |
+
)
|
99 |
+
|
100 |
+
btn = gr.Button("生成像素化图片")
|
101 |
+
|
102 |
+
@gr.render(inputs=[image_input, pixel_size_slider, color_slider, method_checkboxes, interpolation_radio,
|
103 |
+
pixelation_checkboxes], triggers=[btn.click])
|
104 |
+
def show_pictures(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types):
|
105 |
+
num_outputs = len(methods) * len(pixelation_types)
|
106 |
+
num_outputs = min(num_outputs, 16)
|
107 |
+
images = pixelate_and_quantize(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types)
|
108 |
+
cols = math.ceil(math.sqrt(num_outputs))
|
109 |
+
rows = math.ceil(num_outputs / cols)
|
110 |
+
for i in range(rows):
|
111 |
+
single_row = gr.Row()
|
112 |
+
with single_row:
|
113 |
+
for j in range(cols):
|
114 |
+
single_col = gr.Column()
|
115 |
+
idx = i * cols + j
|
116 |
+
with single_col:
|
117 |
+
if idx < num_outputs:
|
118 |
+
# 计算当前的 pixelation 和 quantization 方法
|
119 |
+
current_pixelation = pixelation_types[idx // len(methods)]
|
120 |
+
current_method = methods[idx % len(methods)]
|
121 |
+
# 更新图片的标签,包含像素化方法和颜色量化方法
|
122 |
+
label = f"像素化: {current_pixelation}, 颜色量化: {current_method}"
|
123 |
+
gr.Image(images[idx], label=label, format="png")
|
124 |
+
idx += 1
|
125 |
+
return demo
|
126 |
+
|
127 |
+
|
128 |
+
# 启动 Gradio 界面
|
129 |
+
if __name__ == "__main__":
|
130 |
+
demo = gradio_interface()
|
131 |
+
demo.launch()
|
pixelation.py
CHANGED
@@ -19,6 +19,7 @@ def pixelate_image(image, pixel_size, interpolation):
|
|
19 |
|
20 |
# 获取原图像的尺寸
|
21 |
width, height = img.size
|
|
|
22 |
|
23 |
# 选择插值方式
|
24 |
if interpolation == "Nearest":
|
@@ -61,6 +62,7 @@ def mosaic_pixelation(image, pixel_size):
|
|
61 |
img = image.convert("RGB")
|
62 |
img_np = np.array(img)
|
63 |
h, w, _ = img_np.shape
|
|
|
64 |
|
65 |
for y in range(0, h, pixel_size):
|
66 |
for x in range(0, w, pixel_size):
|
@@ -85,7 +87,7 @@ def oil_paint_pixelation(image, pixel_size):
|
|
85 |
img = image.convert("RGB")
|
86 |
img_np = np.array(img)
|
87 |
h, w, _ = img_np.shape
|
88 |
-
|
89 |
for y in range(0, h, pixel_size):
|
90 |
for x in range(0, w, pixel_size):
|
91 |
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
@@ -111,6 +113,9 @@ def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
|
|
111 |
img = image.convert("RGB")
|
112 |
img_np = np.array(img)
|
113 |
h, w, _ = img_np.shape
|
|
|
|
|
|
|
114 |
step = max((max_pixel_size - min_pixel_size) // (w // min_pixel_size), 1)
|
115 |
|
116 |
for pixel_size in range(min_pixel_size, max_pixel_size + 1, step):
|
|
|
19 |
|
20 |
# 获取原图像的尺寸
|
21 |
width, height = img.size
|
22 |
+
pixel_size = round(min(width,height)/1024)*pixel_size
|
23 |
|
24 |
# 选择插值方式
|
25 |
if interpolation == "Nearest":
|
|
|
62 |
img = image.convert("RGB")
|
63 |
img_np = np.array(img)
|
64 |
h, w, _ = img_np.shape
|
65 |
+
pixel_size = round(min(w, h) / 1024) * pixel_size
|
66 |
|
67 |
for y in range(0, h, pixel_size):
|
68 |
for x in range(0, w, pixel_size):
|
|
|
87 |
img = image.convert("RGB")
|
88 |
img_np = np.array(img)
|
89 |
h, w, _ = img_np.shape
|
90 |
+
pixel_size = round(min(w, h) / 1024) * pixel_size
|
91 |
for y in range(0, h, pixel_size):
|
92 |
for x in range(0, w, pixel_size):
|
93 |
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
|
|
113 |
img = image.convert("RGB")
|
114 |
img_np = np.array(img)
|
115 |
h, w, _ = img_np.shape
|
116 |
+
min_pixel_size = round(min(w, h) / 1024) * min_pixel_size
|
117 |
+
max_pixel_size = round(min(w, h) / 1024) * max_pixel_size
|
118 |
+
|
119 |
step = max((max_pixel_size - min_pixel_size) // (w // min_pixel_size), 1)
|
120 |
|
121 |
for pixel_size in range(min_pixel_size, max_pixel_size + 1, step):
|