MCPcopy Create free account
hub / github.com/InternRobotics/G2VLM / shuffle_patch

Function shuffle_patch

data/transforms.py:259–307  ·  view source on GitHub ↗

将图像分割为块(允许尺寸不整除),随机打乱后拼接,块间保留间隙

(image, num_splits, gap_size=2)

Source from the content-addressed store, hash-verified

257
258
259def shuffle_patch(image, num_splits, gap_size=2):
260 """将图像分割为块(允许尺寸不整除),随机打乱后拼接,块间保留间隙"""
261 h_splits, w_splits = num_splits
262 img_w, img_h = image.size
263
264 base_patch_h = img_h // h_splits
265 patch_heights = [base_patch_h] * (h_splits - 1)
266 patch_heights.append(img_h - sum(patch_heights))
267
268 base_patch_w = img_w // w_splits
269 patch_widths = [base_patch_w] * (w_splits - 1)
270 patch_widths.append(img_w - sum(patch_widths))
271
272 patches = []
273 current_y = 0
274 for i in range(h_splits):
275 current_x = 0
276 patch_h = patch_heights[i]
277 for j in range(w_splits):
278 patch_w = patch_widths[j]
279 patch = image.crop((current_x, current_y, current_x + patch_w, current_y + patch_h))
280 patches.append(patch)
281 current_x += patch_w
282 current_y += patch_h
283
284 random.shuffle(patches)
285
286 total_width = sum(patch_widths) + (w_splits - 1) * gap_size
287 total_height = sum(patch_heights) + (h_splits - 1) * gap_size
288 new_image = Image.new(image.mode, (total_width, total_height), color=(255, 255, 255))
289
290 current_y = 0 # 当前行的起始 Y 坐标
291 patch_idx = 0 # 当前处理的块索引
292 for i in range(h_splits):
293 current_x = 0 # 当前列的起始 X 坐标
294 patch_h = patch_heights[i] # 当前行块的高度
295 for j in range(w_splits):
296 # 取出打乱后的块
297 patch = patches[patch_idx]
298 patch_w = patch_widths[j] # 当前列块的宽度
299 # 粘贴块(左上角坐标为 (current_x, current_y))
300 new_image.paste(patch, (current_x, current_y))
301 # 更新 X 坐标(下一个块的起始位置 = 当前块宽度 + 间隙)
302 current_x += patch_w + gap_size
303 patch_idx += 1
304 # 更新 Y 坐标(下一行的起始位置 = 当前行高度 + 间隙)
305 current_y += patch_h + gap_size
306
307 return new_image
308
309
310def inpainting(image, num_splits, blank_ratio=0.3, blank_color=(255, 255, 255)):

Callers

nothing calls this directly

Calls 1

cropMethod · 0.80

Tested by

no test coverage detected