Huxxshadow commited on
Commit
25e22d8
·
verified ·
1 Parent(s): 9d405be

Upload 4 files

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -0
  2. Example.png +3 -0
  3. app_en_private.py +133 -0
  4. 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

  • SHA256: 0b1fa4de209d7e84e75da98fa8e92af3c88f3cf195a1eceb77d2edc49cb53ae9
  • Pointer size: 132 Bytes
  • Size of remote file: 2.49 MB
app_en_private.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Define the pixelation and color quantization processing function
13
+ def pixelate_and_quantize(image, pixel_size, n_colors, methods, interpolation, pixelation_types):
14
+ results = []
15
+
16
+ # Perform pixelation based on the user's selected pixelation method
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"Unknown pixelation method: {pixelation_type}")
28
+
29
+ # Perform color quantization based on the user's selected method and add the quantized image to the list
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"Unknown quantization method: {method}")
41
+
42
+ # Return only the quantized images
43
+ results.append(quantized_img)
44
+
45
+ return results
46
+
47
+
48
+ # Gradio user interface
49
+ def gradio_interface():
50
+ with gr.Blocks(title="Image Pixelation Tool") as demo:
51
+ gr.Markdown("## Image Pixelation Tool (AI Pixel Repair)")
52
+ with gr.Row():
53
+ with gr.Column(scale=4):
54
+ gr.Markdown("""
55
+ This tool provides a variety of image pixelation effects and color quantization algorithms,
56
+ including classic nearest neighbor pixelation, mosaic effect, oil painting effect, and hierarchical pixelation.
57
+ Users can choose different pixelation methods and perform color quantization on the image.
58
+
59
+ ### License Agreement
60
+ This tool is released under the **MPL 2.0 (Mozilla Public License 2.0)** license. The license allows modification, distribution,
61
+ and commercial use, as long as the modified part of the source code remains open-source.
62
+
63
+ - **Allowed**: Modification, distribution, commercial use.
64
+ - **Restriction**: Modified source code must remain open-source.
65
+
66
+ [Read the full MPL 2.0 License](https://www.mozilla.org/en-US/MPL/2.0/)
67
+
68
+ **Note: Please contact the author for further authorization before using this tool for commercial purposes.**
69
+
70
+ Contact: [email protected]
71
+
72
+ Copyright © 2024
73
+ """)
74
+ with gr.Column(scale=1):
75
+ gr.Image(value="https://gravatar.com/avatar/483611663515c5a569c10664f3abf78c8d01498febd864a3413ecc8fe01dd740.jpg?s=256", show_download_button=False,interactive=False,show_fullscreen_button=False)
76
+ with gr.Row():
77
+ gr.Image(value="Example.png", show_download_button=True,interactive=False,show_fullscreen_button=False)
78
+ with gr.Row():
79
+ with gr.Column():
80
+ image_input = gr.Image(type="pil", label="Upload Image")
81
+
82
+ with gr.Column():
83
+ pixel_size_slider = gr.Slider(minimum=2, maximum=100, value=10, step=1, label="Choose Pixel Block Size")
84
+ color_slider = gr.Slider(minimum=2, maximum=128, value=16, step=1, label="Choose Number of Colors")
85
+
86
+ method_checkboxes = gr.CheckboxGroup(
87
+ choices=["K-Means", "Median Cut", "Floyd-Steinberg Dithering", "MedianCut(PerceptualWeighting)"],
88
+ value=["K-Means"],
89
+ label="Select Color Quantization Methods"
90
+ )
91
+ pixelation_checkboxes = gr.CheckboxGroup(
92
+ choices=["Classic Nearest Neighbor", "Mosaic", "Oil Painting", "Hierarchical"],
93
+ value=["Classic Nearest Neighbor"],
94
+ label="Select Pixelation Methods"
95
+ )
96
+ interpolation_radio = gr.Radio(
97
+ choices=["Nearest", "Bilinear", "Bicubic", "Lanczos"],
98
+ value="Nearest",
99
+ label="Choose Interpolation Method (Only for Classic Nearest Neighbor)"
100
+ )
101
+
102
+ btn = gr.Button("Generate Pixelated Image")
103
+
104
+ @gr.render(inputs=[image_input, pixel_size_slider, color_slider, method_checkboxes, interpolation_radio,
105
+ pixelation_checkboxes], triggers=[btn.click])
106
+ def show_pictures(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types):
107
+ num_outputs = len(methods) * len(pixelation_types)
108
+ num_outputs = min(num_outputs, 16)
109
+ images = pixelate_and_quantize(img_input, pixel_size, n_colors, methods, interpolation, pixelation_types)
110
+ cols = math.ceil(math.sqrt(num_outputs))
111
+ rows = math.ceil(num_outputs / cols)
112
+ for i in range(rows):
113
+ single_row = gr.Row()
114
+ with single_row:
115
+ for j in range(cols):
116
+ single_col = gr.Column()
117
+ idx = i * cols + j
118
+ with single_col:
119
+ if idx < num_outputs:
120
+ # Calculate the current pixelation and quantization method
121
+ current_pixelation = pixelation_types[idx // len(methods)]
122
+ current_method = methods[idx % len(methods)]
123
+ # Update the image label with pixelation and color quantization methods
124
+ label = f"Pixelation: {current_pixelation}, Color Quantization: {current_method}"
125
+ gr.Image(images[idx], label=label, format="png")
126
+ idx += 1
127
+ return demo
128
+
129
+
130
+ # Launch Gradio interface
131
+ if __name__ == "__main__":
132
+ demo = gradio_interface()
133
+ 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):