[Python] How can I convert an array into an image in pygame? [duplicate]

Discussão em 'Python' iniciado por Stack, Julho 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to create a character color picker, where the player can pick their own RGB values for parts of their character (using a find and replace on a specific color on the player image). I'm able to turn the original image into an array, and replace the color within it, but I cannot turn it back. I'll include the relevant methods below:

    class Player():
    def __init__(self, image, x, y):
    self.image = pygame.image.load(image).convert_alpha()
    self.hVel = 5
    self.yVel = 0
    self.x = 0
    self.y = 0
    fromColour = (255, 255, 255)
    arr = pygame.PixelArray(self.image)
    arr.replace(fromColour, pcolour)
    self.image = image.fromarray(arr)
    del arr


    def draw(self):
    WINDOW.blit(pygame.transform.scale((self.image), IMAGE_SIZE), (self.x, self.y))


    If you need any more of the code, please let me know.

    The code works up to and including the arr.replace line, but then the window has a not responding message without any actual error. My problem is with converting the updated array back into an image file, as the draw function works fine when not trying to change the character color, and prints work as tests between the lines, up to updating self.image.

    Continue reading...

Compartilhe esta Página