Surface alpha lost when converting pygame to cv2 to pygame

Viewed 90

I'm making a bloom system as a project in pygame. The code for the bloom works, but with how it is converted from pygame.Surface to np.array, it looses its alpha layer.

This is the code:

def Bloom(canvas: pygame.Surface):
    size = canvas.get_size()
    canvas_zero = canvas
    canvas = pygame.surfarray.array3d(canvas)
    newCanvas = pygame.Surface(size, pygame.SRCALPHA)
    newCanvas.set_colorkey((0,0,0))


    cv2.GaussianBlur(canvas, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=canvas)
    cv2.blur(canvas, ksize=(9, 9), dst=canvas)

    newCanvas.blit(pygame.surfarray.make_surface(canvas), (0,0))
    newCanvas.blit(canvas_zero, (0,0))

    return newCanvas

This is how the system is looking

black outline due to colorkey

As you can see, there is this black outline, created from the set_colorkey, on the particle.

What I wanted was a gradient from white to totally transparent, what I got was a gradient from white to black.

How could I fix this?

(I've tried PIL instead of cv2, but it was too slow (15 fps))

1 Answers

pygame.surfarray.array3d() only returns the RGB channels of the surface.

You have to concatenate the RGB color channels and the Alpha channel. The Alpha channel can be get with pygame.surfarray.array_alpha():

canvas_rgb = pygame.surfarray.array3d(canvas)
canvas_alpha = pygame.surfarray.array_alpha(canvas).reshape((*canvas_rgb.shape[0:2], 1))
canvas_rgba = numpy.concatenate((canvas_rgb, canvas_alpha), 2)

Probably even better performance can be achieved by getting the color values with pygame.surfarray.array2d() and creating a new view of the array with numpy.ndarray.view:

canvas_color = pygame.surfarray.array2d(canvas)
canvas_rgba = canvas_color.view(dtype=numpy.uint8).reshape((*canvas_rgb.shape[0:2], 4))

Bloom function option 1:

def Bloom(canvas: pygame.Surface):
    size = canvas.get_size()
    newCanvas = pygame.Surface(size, pygame.SRCALPHA)
    newCanvas.set_colorkey((0,0,0))

    canvas_rgb = pygame.surfarray.array3d(canvas)
    canvas_alpha = pygame.surfarray.array_alpha(canvas).reshape((*canvas_rgb.shape[0:2], 1))
    canvas_rgba = numpy.concatenate((canvas_rgb, canvas_alpha), 2)

    cv2.GaussianBlur(canvas_rgba, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=canvas_rgba)
    cv2.blur(canvas_rgba, ksize=(9, 9), dst=canvas_rgba)

    newCanvas.blit(pygame.image.frombuffer(canvas_rgba.transpose((1, 0, 2)).copy(order='C'), size, 'RGBA'), (0,0))
    newCanvas.blit(canvas, (0,0))

    return newCanvas

Bloom function option 2:

def Bloom(canvas: pygame.Surface):
    size = canvas.get_size()
    newCanvas = pygame.Surface(size, pygame.SRCALPHA)
    newCanvas.set_colorkey((0,0,0))

    canvas_color = pygame.surfarray.array2d(canvas)
    canvas_rgba = canvas_color.view(dtype=numpy.uint8).reshape((*canvas_color.shape, 4))

    cv2.GaussianBlur(canvas_rgba, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=canvas_rgba)
    cv2.blur(canvas_rgba, ksize=(9, 9), dst=canvas_rgba)

    pygame.surfarray.blit_array(newCanvas, canvas_color)
    newCanvas.blit(canvas, (0,0))

    return newCanvas

Minimal example:

import pygame, cv2, numpy

def Bloom1(canvas: pygame.Surface):
    size = canvas.get_size()
    newCanvas = pygame.Surface(size, pygame.SRCALPHA)
    newCanvas.set_colorkey((0,0,0))

    canvas_rgb = pygame.surfarray.array3d(canvas)
    canvas_alpha = pygame.surfarray.array_alpha(canvas).reshape((*canvas_rgb.shape[0:2], 1))
    canvas_rgba = numpy.concatenate((canvas_rgb, canvas_alpha), 2)

    cv2.GaussianBlur(canvas_rgba, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=canvas_rgba)
    cv2.blur(canvas_rgba, ksize=(9, 9), dst=canvas_rgba)

    newCanvas.blit(pygame.image.frombuffer(canvas_rgba.transpose((1, 0, 2)).copy(order='C'), size, 'RGBA'), (0,0))
    return newCanvas

def Bloom2(canvas: pygame.Surface):
    size = canvas.get_size()
    newCanvas = pygame.Surface(size, pygame.SRCALPHA)
    newCanvas.set_colorkey((0,0,0))

    canvas_color = pygame.surfarray.array2d(canvas)
    canvas_rgba = canvas_color.view(dtype=numpy.uint8).reshape((*canvas_color.shape, 4))

    cv2.GaussianBlur(canvas_rgba, ksize=(9, 9), sigmaX=10, sigmaY=10, dst=canvas_rgba)
    cv2.blur(canvas_rgba, ksize=(9, 9), dst=canvas_rgba)

    pygame.surfarray.blit_array(newCanvas, canvas_color)
    return newCanvas

pygame.init()
window = pygame.display.set_mode((550, 300))
clock = pygame.time.Clock()

font = pygame.font.SysFont(None, 90)
text = font.render("Bloom", True, 0)

surface = pygame.Surface((250, 250))
surface.fill(0)
pygame.draw.circle(surface, (255, 255, 255), surface.get_rect().center, 100)
surface.blit(text, text.get_rect(center = surface.get_rect().center))
surface.set_colorkey(0)
surface = surface.convert_alpha()
surface1 = Bloom1(surface)
surface2 = Bloom1(surface)

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 

    window.fill((64, 64, 64))
    window.blit(surface1, surface1.get_rect(center = (150, 150)), special_flags = pygame.BLEND_PREMULTIPLIED)
    window.blit(surface2, surface2.get_rect(center = (400, 150)), special_flags = pygame.BLEND_PREMULTIPLIED)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
Related