Huxxshadow commited on
Commit
d0375d8
·
verified ·
1 Parent(s): b5419dc

Update pixelation.py

Browse files

Fix adaptive pixel size

Files changed (1) hide show
  1. pixelation.py +36 -17
pixelation.py CHANGED
@@ -16,7 +16,11 @@ def pixelate_image(image, pixel_size, interpolation="Nearest"):
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
@@ -29,8 +33,12 @@ def pixelate_image(image, pixel_size, interpolation="Nearest"):
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
 
@@ -55,13 +63,16 @@ def mosaic_pixelation(image, pixel_size):
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):
62
- block = img_np[y:y + pixel_size, x:x + pixel_size]
 
 
 
 
63
  mean_color = block.mean(axis=(0, 1)).astype(int)
64
- img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
65
 
66
  return Image.fromarray(img_np)
67
 
@@ -79,14 +90,17 @@ def oil_paint_pixelation(image, pixel_size):
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]
 
 
 
 
87
  block_colors = [tuple(color) for color in block.reshape(-1, 3)]
88
  most_common_color = Counter(block_colors).most_common(1)[0][0]
89
- img_np[y:y + pixel_size, x:x + pixel_size] = most_common_color
90
 
91
  return Image.fromarray(img_np)
92
 
@@ -105,16 +119,21 @@ def hierarchical_pixelation(image, min_pixel_size, max_pixel_size):
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
 
113
- for pixel_size in range(min_pixel_size, max_pixel_size + 1, step):
 
 
 
114
  for y in range(0, h, pixel_size):
115
  for x in range(0, w, pixel_size):
116
  block = img_np[y:y + pixel_size, x:x + pixel_size]
117
  mean_color = block.mean(axis=(0, 1)).astype(int)
118
  img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
119
 
120
- return Image.fromarray(img_np)
 
 
16
  """
17
  img = image.convert("RGB")
18
  width, height = img.size
19
+
20
+ # 使用比例调整 pixel_size,但确保至少为 1
21
+ # 基准值 512 可根据需要调整
22
+ scale_factor = max(1, min(width, height) // 512)
23
+ adjusted_pixel_size = max(1, pixel_size * scale_factor)
24
 
25
  if interpolation == "Nearest":
26
  resample_method = Image.NEAREST
 
33
  else:
34
  raise ValueError(f"未知的插值方法: {interpolation}")
35
 
36
+ # 确保输出尺寸至少为1x1
37
+ small_width = max(1, width // adjusted_pixel_size)
38
+ small_height = max(1, height // adjusted_pixel_size)
39
+
40
  small_img = img.resize(
41
+ (small_width, small_height),
42
  resample=resample_method
43
  )
44
 
 
63
  img = image.convert("RGB")
64
  img_np = np.array(img)
65
  h, w, _ = img_np.shape
 
66
 
67
+ # 使用比例调整 pixel_size,但确保至少为 1
68
+ scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
69
+ adjusted_pixel_size = max(1, pixel_size * scale_factor)
70
+
71
+ for y in range(0, h, adjusted_pixel_size):
72
+ for x in range(0, w, adjusted_pixel_size):
73
+ block = img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size]
74
  mean_color = block.mean(axis=(0, 1)).astype(int)
75
+ img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size] = mean_color
76
 
77
  return Image.fromarray(img_np)
78
 
 
90
  img = image.convert("RGB")
91
  img_np = np.array(img)
92
  h, w, _ = img_np.shape
 
93
 
94
+ # 使用比例调整 pixel_size,但确保至少为 1
95
+ scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
96
+ adjusted_pixel_size = max(1, pixel_size * scale_factor)
97
+
98
+ for y in range(0, h, adjusted_pixel_size):
99
+ for x in range(0, w, adjusted_pixel_size):
100
+ block = img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size]
101
  block_colors = [tuple(color) for color in block.reshape(-1, 3)]
102
  most_common_color = Counter(block_colors).most_common(1)[0][0]
103
+ img_np[y:y + adjusted_pixel_size, x:x + adjusted_pixel_size] = most_common_color
104
 
105
  return Image.fromarray(img_np)
106
 
 
119
  img = image.convert("RGB")
120
  img_np = np.array(img)
121
  h, w, _ = img_np.shape
 
 
122
 
123
+ # 使用比例调整 pixel_size,但确保至少为 1
124
+ scale_factor = max(1, min(w, h) // 512) # 根据需要调整基准值
125
+ adjusted_min_pixel_size = max(1, min_pixel_size * scale_factor)
126
+ adjusted_max_pixel_size = max(1, max_pixel_size * scale_factor)
127
 
128
+ # 防止步长为0
129
+ step = max((adjusted_max_pixel_size - adjusted_min_pixel_size) // max(w // adjusted_min_pixel_size, 1), 1)
130
+
131
+ for pixel_size in range(adjusted_min_pixel_size, adjusted_max_pixel_size + 1, step):
132
  for y in range(0, h, pixel_size):
133
  for x in range(0, w, pixel_size):
134
  block = img_np[y:y + pixel_size, x:x + pixel_size]
135
  mean_color = block.mean(axis=(0, 1)).astype(int)
136
  img_np[y:y + pixel_size, x:x + pixel_size] = mean_color
137
 
138
+ return Image.fromarray(img_np)
139
+