如何在 Pygame 表面中实施洪水填充

How to implement flood fill in a Pygame surface

我想知道填充 Pygame 表面的一部分的好方法。 我想要的最好例子是油漆桶在 MS Paint 中的工作方式。

例如,如果在白色表面上用黑色绘制了一个圆圈,我想填充圆圈内的白色(或者任何形状)。

为了让您了解我在做什么,我正在制作一个像素艺术工具,并且正在开发一个类似于 MS Paint 中的桶的功能。 (观看:http://imgur.com/a/ogtPV

我试过使用 Surface.get_at()Surface.set_at() 一堆来填充,但是一旦你填充了大约 100x100 像素的区域,它就会滞后太多。

我也愿意接受任何其他不会滞后的方法。

我找到了一种方法,它在 100x100 区域大约需要 60 ms,在 1000x1000 区域大约需要 2000 ms。代码中的解释。

import random
import pygame
pygame.init()

screen = pygame.display.set_mode((1024, 640))
clock = pygame.time.Clock()

image = pygame.image.load('delete_image.png').convert()


def fill(surface, position, fill_color):
    fill_color = surface.map_rgb(fill_color)  # Convert the color to mapped integer value.
    surf_array = pygame.surfarray.pixels2d(surface)  # Create an array from the surface.
    current_color = surf_array[position]  # Get the mapped integer color value.

    # 'frontier' is a list where we put the pixels that's we haven't checked. Imagine that we first check one pixel and 
    # then expand like rings on the water. 'frontier' are the pixels on the edge of the pool of pixels we have checked.
    #
    # During each loop we get the position of a pixel. If that pixel contains the same color as the ones we've checked
    # we paint it with our 'fill_color' and put all its neighbours into the 'frontier' list. If not, we check the next
    # one in our list, until it's empty.

    frontier = [position]
    while len(frontier) > 0:
        x, y = frontier.pop()
        try:  # Add a try-except block in case the position is outside the surface.
            if surf_array[x, y] != current_color:
                continue
        except IndexError:
            continue
        surf_array[x, y] = fill_color
        # Then we append the neighbours of the pixel in the current position to our 'frontier' list.
        frontier.append((x + 1, y))  # Right.
        frontier.append((x - 1, y))  # Left.
        frontier.append((x, y + 1))  # Down.
        frontier.append((x, y - 1))  # Up.

    pygame.surfarray.blit_array(surface, surf_array)


while True:
    clock.tick(30)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            quit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                color = random.choice(tuple(pygame.color.THECOLORS.values()))
                print('Running')
                time = pygame.time.get_ticks()
                fill(image, event.pos, color)
                print('Finished in {} ms'.format(pygame.time.get_ticks() - time))

    screen.blit(image, (0, 0))
    pygame.display.update()

这是我试验过的图片(如果你想卖图片,我会收取收益):

这个算法叫做"flood fill"。 Pygame 没有 built-in 填充功能。最简单的选择可能是使用 OpenCV 库。 Google "install opencv" 适用于您的平台。

然后你可以将它包装在一个函数中 Pygame surface:

def Fill(Surf, Point, Color):
    arr = pygame.surfarray.array3d(Surf)    # copy array from surface
    swapPoint = (Point[1], Point[0])        # swap X and Y
    cv2.floodFill(arr, None, swapPoint, Color)
    pygame.surfarray.blit_array(Surf, arr)

这将用颜色填充点处的连接区域。