Spaces:
Running
Running
File size: 4,607 Bytes
b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 b5419dc d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 b4e60c9 d0375d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
from PIL import Image
import numpy as np
from collections import Counter
def pixelate_image(image, pixel_size, interpolation="Nearest"):
"""
对图像进行像素化。
参数:
- image: 输入的 PIL 图像对象
- pixel_size: 像素块大小
- interpolation: 插值方法 ("Nearest", "Bilinear", "Bicubic", "Lanczos")
返回:
- 像素化后的 PIL 图像对象
"""
img = image.convert("RGB")
width, height = img.size
# 使用比例调整 pixel_size,但确保至少为 1
# 基准值 512 可根据需要调整
scale_factor = max(1, min(width, height) // 512)
adjusted_pixel_size = max(1, pixel_size * scale_factor)
if interpolation == "Nearest":
resample_method = Image.NEAREST
elif interpolation == "Bilinear":
resample_method = Image.BILINEAR
elif interpolation == "Bicubic":
resample_method = Image.BICUBIC
elif interpolation == "Lanczos":
resample_method = Image.LANCZOS
else:
raise ValueError(f"未知的插值方法: {interpolation}")
# 确保输出尺寸至少为1x1
small_width = max(1, width // adjusted_pixel_size)
small_height = max(1, height // adjusted_pixel_size)
small_img = img.resize(
(small_width, small_height),
resample=resample_method
)
pixelated_img = small_img.resize(
(width, height),
resample=resample_method
)
return pixelated_img
def mosaic_pixelation(image, pixel_size):
"""
使用马赛克方法对图像进行像素化。
参数:
- image: 输入的 PIL 图像对象
- pixel_size: 像素块大小
返回:
- 马赛克效果的 PIL 图像对象
"""
img = image.convert("RGB")
img_np = np.array(img)
h, w, _ = img_np.shape
# 使用比例调整 pixel_size,但确保至少为 1
scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
adjusted_pixel_size = max(1, pixel_size * scale_factor)
for y in range(0, h, adjusted_pixel_size):
for x in range(0, w, adjusted_pixel_size):
block = img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size]
mean_color = block.mean(axis=(0, 1)).astype(int)
img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size] = mean_color
return Image.fromarray(img_np)
def oil_paint_pixelation(image, pixel_size):
"""
使用油画滤镜方法对图像进行像素化。
参数:
- image: 输入的 PIL 图像对象
- pixel_size: 像素块大小
返回:
- 油画滤镜效果的 PIL 图像对象
"""
img = image.convert("RGB")
img_np = np.array(img)
h, w, _ = img_np.shape
# 使用比例调整 pixel_size,但确保至少为 1
scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
adjusted_pixel_size = max(1, pixel_size * scale_factor)
for y in range(0, h, adjusted_pixel_size):
for x in range(0, w, adjusted_pixel_size):
block = img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size]
block_colors = [tuple(color) for color in block.reshape(-1, 3)]
most_common_color = Counter(block_colors).most_common(1)[0][0]
img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size] = most_common_color
return Image.fromarray(img_np)
def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
"""
使用层次像素化方法对图像进行像素化。
参数:
- image: 输入的 PIL 图像对象
- min_pixel_size: 最小像素块大小
- max_pixel_size: 最大像素块大小
返回:
- 层次像素化效果的 PIL 图像对象
"""
img = image.convert("RGB")
img_np = np.array(img)
h, w, _ = img_np.shape
# 使用比例调整 pixel_size,但确保至少为 1
scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
adjusted_min_pixel_size = max(1, min_pixel_size * scale_factor)
adjusted_max_pixel_size = max(1, max_pixel_size * scale_factor)
# 防止步长为0
step = max((adjusted_max_pixel_size - adjusted_min_pixel_size) // max(w // adjusted_min_pixel_size, 1), 1)
for pixel_size in range(adjusted_min_pixel_size, adjusted_max_pixel_size + 1, step):
for y in range(0, h, pixel_size):
for x in range(0, w, pixel_size):
block = img_np[y:y + pixel_size, x:x + pixel_size]
mean_color = block.mean(axis=(0, 1)).astype(int)
img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
return Image.fromarray(img_np)
|