Spaces:
Running
Running
Huxxshadow
commited on
Update pixelation.py
Browse files- pixelation.py +6 -14
pixelation.py
CHANGED
@@ -14,14 +14,10 @@ def pixelate_image(image, pixel_size, interpolation="Nearest"):
|
|
14 |
返回:
|
15 |
- 像素化后的 PIL 图像对象
|
16 |
"""
|
17 |
-
# 将输入图像转为 RGB 模式
|
18 |
img = image.convert("RGB")
|
19 |
-
|
20 |
-
# 获取原图像的尺寸
|
21 |
width, height = img.size
|
22 |
-
|
23 |
|
24 |
-
# 选择插值方式
|
25 |
if interpolation == "Nearest":
|
26 |
resample_method = Image.NEAREST
|
27 |
elif interpolation == "Bilinear":
|
@@ -33,13 +29,11 @@ def pixelate_image(image, pixel_size, interpolation="Nearest"):
|
|
33 |
else:
|
34 |
raise ValueError(f"未知的插值方法: {interpolation}")
|
35 |
|
36 |
-
# 第一步:缩小图像,使用邻近插值保持像素块的正方形效果
|
37 |
small_img = img.resize(
|
38 |
(width // pixel_size, height // pixel_size),
|
39 |
resample=resample_method
|
40 |
)
|
41 |
|
42 |
-
# 第二步:放大图像,使用用户选择的插值方法
|
43 |
pixelated_img = small_img.resize(
|
44 |
(width, height),
|
45 |
resample=resample_method
|
@@ -47,7 +41,6 @@ def pixelate_image(image, pixel_size, interpolation="Nearest"):
|
|
47 |
|
48 |
return pixelated_img
|
49 |
|
50 |
-
|
51 |
def mosaic_pixelation(image, pixel_size):
|
52 |
"""
|
53 |
使用马赛克方法对图像进行像素化。
|
@@ -62,7 +55,7 @@ def mosaic_pixelation(image, pixel_size):
|
|
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):
|
@@ -72,7 +65,6 @@ def mosaic_pixelation(image, pixel_size):
|
|
72 |
|
73 |
return Image.fromarray(img_np)
|
74 |
|
75 |
-
|
76 |
def oil_paint_pixelation(image, pixel_size):
|
77 |
"""
|
78 |
使用油画滤镜方法对图像进行像素化。
|
@@ -87,7 +79,8 @@ def oil_paint_pixelation(image, 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]
|
@@ -97,7 +90,6 @@ def oil_paint_pixelation(image, pixel_size):
|
|
97 |
|
98 |
return Image.fromarray(img_np)
|
99 |
|
100 |
-
|
101 |
def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
|
102 |
"""
|
103 |
使用层次像素化方法对图像进行像素化。
|
@@ -113,8 +105,8 @@ def hierarchical_pixelation(image, min_pixel_size, max_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 |
|
|
|
14 |
返回:
|
15 |
- 像素化后的 PIL 图像对象
|
16 |
"""
|
|
|
17 |
img = image.convert("RGB")
|
|
|
|
|
18 |
width, height = img.size
|
19 |
+
pixel_size = max(1, round(min(width, height) / 1024) * pixel_size)
|
20 |
|
|
|
21 |
if interpolation == "Nearest":
|
22 |
resample_method = Image.NEAREST
|
23 |
elif interpolation == "Bilinear":
|
|
|
29 |
else:
|
30 |
raise ValueError(f"未知的插值方法: {interpolation}")
|
31 |
|
|
|
32 |
small_img = img.resize(
|
33 |
(width // pixel_size, height // pixel_size),
|
34 |
resample=resample_method
|
35 |
)
|
36 |
|
|
|
37 |
pixelated_img = small_img.resize(
|
38 |
(width, height),
|
39 |
resample=resample_method
|
|
|
41 |
|
42 |
return pixelated_img
|
43 |
|
|
|
44 |
def mosaic_pixelation(image, pixel_size):
|
45 |
"""
|
46 |
使用马赛克方法对图像进行像素化。
|
|
|
55 |
img = image.convert("RGB")
|
56 |
img_np = np.array(img)
|
57 |
h, w, _ = img_np.shape
|
58 |
+
pixel_size = max(1, round(min(w, h) / 1024) * pixel_size)
|
59 |
|
60 |
for y in range(0, h, pixel_size):
|
61 |
for x in range(0, w, pixel_size):
|
|
|
65 |
|
66 |
return Image.fromarray(img_np)
|
67 |
|
|
|
68 |
def oil_paint_pixelation(image, pixel_size):
|
69 |
"""
|
70 |
使用油画滤镜方法对图像进行像素化。
|
|
|
79 |
img = image.convert("RGB")
|
80 |
img_np = np.array(img)
|
81 |
h, w, _ = img_np.shape
|
82 |
+
pixel_size = max(1, round(min(w, h) / 1024) * pixel_size)
|
83 |
+
|
84 |
for y in range(0, h, pixel_size):
|
85 |
for x in range(0, w, pixel_size):
|
86 |
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
|
|
90 |
|
91 |
return Image.fromarray(img_np)
|
92 |
|
|
|
93 |
def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
|
94 |
"""
|
95 |
使用层次像素化方法对图像进行像素化。
|
|
|
105 |
img = image.convert("RGB")
|
106 |
img_np = np.array(img)
|
107 |
h, w, _ = img_np.shape
|
108 |
+
min_pixel_size = max(1, round(min(w, h) / 1024) * min_pixel_size)
|
109 |
+
max_pixel_size = max(1, round(min(w, h) / 1024) * max_pixel_size)
|
110 |
|
111 |
step = max((max_pixel_size - min_pixel_size) // (w // min_pixel_size), 1)
|
112 |
|