【发布时间】:2021-12-22 02:28:27
【问题描述】:
【问题讨论】:
-
你能分享预期的图像吗?
-
我已经包含了一个具有预期结果的示例。谢谢!
标签: python mask image-segmentation
【问题讨论】:
标签: python mask image-segmentation
以下代码适用于提供的图像。可能不适用于其他人。我正在使用copy-paste 方法。
from PIL import Image
# Create "patch" image
image = Image.open('image.png')
width, height = image.size
start_position = None
end_position = None
line_width = 0
line_width_count = True
for x in range(width):
for y in range(height):
r, g, b = image.getpixel((x, y))
if all([r, g, b]) and not start_position:
start_position = x, y
if line_width_count and all([r, g, b]) and start_position:
line_width += 1
if line_width and all([not r, not g, not b]):
line_width_count = False
if all([r, g, b]):
end_position = x, y
patch = Image.open('image.png')
patch = patch.crop((start_position[0], start_position[1], end_position[0], end_position[1]))
patch_width, patch_height = patch.size
# Add top patches
end_pos_x = start_position[0] - patch_width + line_width * 2
end_pos_y = start_position[1] - patch_height + line_width
for i in range(5):
image.paste(patch, (end_pos_x, end_pos_y))
end_pos_x -= patch_width - line_width * 2
end_pos_y -= patch_height - line_width
# Add bottom patches
end_pos_x = end_position[0]-line_width*2
end_pos_y = end_position[1]-line_width
for i in range(5):
image.paste(patch, (end_pos_x, end_pos_y))
end_pos_x += patch_width - line_width * 2
end_pos_y += patch_height - line_width
image.save('result.png')
结果:
【讨论】: