Spaces:
Sleeping
Sleeping
Update pixelation.py
Browse files- pixelation.py +127 -127
pixelation.py
CHANGED
|
@@ -1,128 +1,128 @@
|
|
| 1 |
-
from PIL import Image
|
| 2 |
-
import numpy as np
|
| 3 |
-
from collections import Counter
|
| 4 |
-
|
| 5 |
-
def pixelate_image(image, pixel_size, interpolation="Nearest"):
|
| 6 |
-
"""
|
| 7 |
-
对图像进行像素化。
|
| 8 |
-
|
| 9 |
-
参数:
|
| 10 |
-
- image: 输入的 PIL 图像对象
|
| 11 |
-
- pixel_size: 像素块大小
|
| 12 |
-
- interpolation: 插值方法 ("Nearest", "Bilinear", "Bicubic", "Lanczos")
|
| 13 |
-
|
| 14 |
-
返回:
|
| 15 |
-
- 像素化后的 PIL 图像对象
|
| 16 |
-
"""
|
| 17 |
-
# 将输入图像转为 RGB 模式
|
| 18 |
-
img = image.convert("RGB")
|
| 19 |
-
|
| 20 |
-
# 获取原图像的尺寸
|
| 21 |
-
width, height = img.size
|
| 22 |
-
pixel_size = max(1, round(min(width, height) / 1024) * pixel_size)
|
| 23 |
-
|
| 24 |
-
# 选择插值方式
|
| 25 |
-
if interpolation == "Nearest":
|
| 26 |
-
resample_method = Image.NEAREST
|
| 27 |
-
elif interpolation == "Bilinear":
|
| 28 |
-
resample_method = Image.BILINEAR
|
| 29 |
-
elif interpolation == "Bicubic":
|
| 30 |
-
resample_method = Image.BICUBIC
|
| 31 |
-
elif interpolation == "Lanczos":
|
| 32 |
-
resample_method = Image.LANCZOS
|
| 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
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
return pixelated_img
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
def mosaic_pixelation(image, pixel_size):
|
| 52 |
-
"""
|
| 53 |
-
使用马赛克方法对图像进行像素化。
|
| 54 |
-
|
| 55 |
-
参数:
|
| 56 |
-
- image: 输入的 PIL 图像对象
|
| 57 |
-
- pixel_size: 像素块大小
|
| 58 |
-
|
| 59 |
-
返回:
|
| 60 |
-
- 马赛克效果的 PIL 图像对象
|
| 61 |
-
"""
|
| 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):
|
| 69 |
-
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
| 70 |
-
mean_color = block.mean(axis=(0, 1)).astype(int)
|
| 71 |
-
img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
|
| 72 |
-
|
| 73 |
-
return Image.fromarray(img_np)
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def oil_paint_pixelation(image, pixel_size):
|
| 77 |
-
"""
|
| 78 |
-
使用油画滤镜方法对图像进行像素化。
|
| 79 |
-
|
| 80 |
-
参数:
|
| 81 |
-
- image: 输入的 PIL 图像对象
|
| 82 |
-
- pixel_size: 像素块大小
|
| 83 |
-
|
| 84 |
-
返回:
|
| 85 |
-
- 油画滤镜效果的 PIL 图像对象
|
| 86 |
-
"""
|
| 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]
|
| 94 |
-
block_colors = [tuple(color) for color in block.reshape(-1, 3)]
|
| 95 |
-
most_common_color = Counter(block_colors).most_common(1)[0][0]
|
| 96 |
-
img_np[y:y + pixel_size, x:x + pixel_size] = most_common_color
|
| 97 |
-
|
| 98 |
-
return Image.fromarray(img_np)
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
|
| 102 |
-
"""
|
| 103 |
-
使用层次像素化方法对图像进行像素化。
|
| 104 |
-
|
| 105 |
-
参数:
|
| 106 |
-
- image: 输入的 PIL 图像对象
|
| 107 |
-
- min_pixel_size: 最小像素块大小
|
| 108 |
-
- max_pixel_size: 最大像素块大小
|
| 109 |
-
|
| 110 |
-
返回:
|
| 111 |
-
- 层次像素化效果的 PIL 图像对象
|
| 112 |
-
"""
|
| 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):
|
| 122 |
-
for y in range(0, h, pixel_size):
|
| 123 |
-
for x in range(0, w, pixel_size):
|
| 124 |
-
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
| 125 |
-
mean_color = block.mean(axis=(0, 1)).astype(int)
|
| 126 |
-
img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
|
| 127 |
-
|
| 128 |
return Image.fromarray(img_np)
|
|
|
|
| 1 |
+
from PIL import Image
|
| 2 |
+
import numpy as np
|
| 3 |
+
from collections import Counter
|
| 4 |
+
|
| 5 |
+
def pixelate_image(image, pixel_size, interpolation="Nearest"):
|
| 6 |
+
"""
|
| 7 |
+
对图像进行像素化。
|
| 8 |
+
|
| 9 |
+
参数:
|
| 10 |
+
- image: 输入的 PIL 图像对象
|
| 11 |
+
- pixel_size: 像素块大小
|
| 12 |
+
- interpolation: 插值方法 ("Nearest", "Bilinear", "Bicubic", "Lanczos")
|
| 13 |
+
|
| 14 |
+
返回:
|
| 15 |
+
- 像素化后的 PIL 图像对象
|
| 16 |
+
"""
|
| 17 |
+
# 将输入图像转为 RGB 模式
|
| 18 |
+
img = image.convert("RGB")
|
| 19 |
+
|
| 20 |
+
# 获取原图像的尺寸
|
| 21 |
+
width, height = img.size
|
| 22 |
+
# pixel_size = max(1, round(min(width, height) / 1024) * pixel_size)
|
| 23 |
+
|
| 24 |
+
# 选择插值方式
|
| 25 |
+
if interpolation == "Nearest":
|
| 26 |
+
resample_method = Image.NEAREST
|
| 27 |
+
elif interpolation == "Bilinear":
|
| 28 |
+
resample_method = Image.BILINEAR
|
| 29 |
+
elif interpolation == "Bicubic":
|
| 30 |
+
resample_method = Image.BICUBIC
|
| 31 |
+
elif interpolation == "Lanczos":
|
| 32 |
+
resample_method = Image.LANCZOS
|
| 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
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
return pixelated_img
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def mosaic_pixelation(image, pixel_size):
|
| 52 |
+
"""
|
| 53 |
+
使用马赛克方法对图像进行像素化。
|
| 54 |
+
|
| 55 |
+
参数:
|
| 56 |
+
- image: 输入的 PIL 图像对象
|
| 57 |
+
- pixel_size: 像素块大小
|
| 58 |
+
|
| 59 |
+
返回:
|
| 60 |
+
- 马赛克效果的 PIL 图像对象
|
| 61 |
+
"""
|
| 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):
|
| 69 |
+
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
| 70 |
+
mean_color = block.mean(axis=(0, 1)).astype(int)
|
| 71 |
+
img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
|
| 72 |
+
|
| 73 |
+
return Image.fromarray(img_np)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def oil_paint_pixelation(image, pixel_size):
|
| 77 |
+
"""
|
| 78 |
+
使用油画滤镜方法对图像进行像素化。
|
| 79 |
+
|
| 80 |
+
参数:
|
| 81 |
+
- image: 输入的 PIL 图像对象
|
| 82 |
+
- pixel_size: 像素块大小
|
| 83 |
+
|
| 84 |
+
返回:
|
| 85 |
+
- 油画滤镜效果的 PIL 图像对象
|
| 86 |
+
"""
|
| 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]
|
| 94 |
+
block_colors = [tuple(color) for color in block.reshape(-1, 3)]
|
| 95 |
+
most_common_color = Counter(block_colors).most_common(1)[0][0]
|
| 96 |
+
img_np[y:y + pixel_size, x:x + pixel_size] = most_common_color
|
| 97 |
+
|
| 98 |
+
return Image.fromarray(img_np)
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
|
| 102 |
+
"""
|
| 103 |
+
使用层次像素化方法对图像进行像素化。
|
| 104 |
+
|
| 105 |
+
参数:
|
| 106 |
+
- image: 输入的 PIL 图像对象
|
| 107 |
+
- min_pixel_size: 最小像素块大小
|
| 108 |
+
- max_pixel_size: 最大像素块大小
|
| 109 |
+
|
| 110 |
+
返回:
|
| 111 |
+
- 层次像素化效果的 PIL 图像对象
|
| 112 |
+
"""
|
| 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):
|
| 122 |
+
for y in range(0, h, pixel_size):
|
| 123 |
+
for x in range(0, w, pixel_size):
|
| 124 |
+
block = img_np[y:y + pixel_size, x:x + pixel_size]
|
| 125 |
+
mean_color = block.mean(axis=(0, 1)).astype(int)
|
| 126 |
+
img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
|
| 127 |
+
|
| 128 |
return Image.fromarray(img_np)
|