Spaces:
Running
Running
File size: 3,775 Bytes
3b01b55 |
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 |
from PIL import Image
import numpy as np
from collections import Counter
def pixelate_image(image, pixel_size, interpolation):
"""
对图像进行像素化。
参数:
- image: 输入的 PIL 图像对象
- pixel_size: 像素块大小
- interpolation: 插值方法 ("Nearest", "Bilinear", "Bicubic", "Lanczos")
返回:
- 像素化后的 PIL 图像对象
"""
# 将输入图像转为 RGB 模式
img = image.convert("RGB")
# 获取原图像的尺寸
width, height = img.size
# 选择插值方式
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}")
# 第一步:缩小图像,使用邻近插值保持像素块的正方形效果
small_img = img.resize(
(width // pixel_size, height // pixel_size),
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
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)
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
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]
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 + pixel_size, x:x + 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
step = max((max_pixel_size - min_pixel_size) // (w // min_pixel_size), 1)
for pixel_size in range(min_pixel_size, 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) |