George Willcox
George Willcox

Reputation: 673

Colouring an image Pygame

I'd like to create a game, similar to that of Geometry Dash. I have all the images for the cubes, but they are all grey and white - this is to allow the user to select the colours.

I have two variables, colour_1 and colour_2. colour_1 should be in the grey, and colour_2 should be in the white. If I say what the variables are, how would I modify the image to have the right colours?

Cube01

The colours on the images are not all the same, the edges blend, so that the image is smoother. This may cause complications.

Upvotes: 2

Views: 528

Answers (2)

Nicolas
Nicolas

Reputation: 7081

I found this on the website Fishstick proposed enter image description here

Here's a working code based on it

img_surface = pygame.image.load("image.gif")              # Load image on a surface
img_array = pygame.surfarray.array3d(img_surface)         # Convert it into an 3D array
colored_img = numpy.array(img_array)                      # Array thing
colored_img[:, :, 0] = 255    # <-- Red
colored_img[:, :, 1] = 128    # <-- Green
colored_img[:, :, 2] = 0      # <-- Blue
img_surface = pygame.surfarray.make_surface(colored_img)  # Convert it back to a surface
screen.blit(img_surface, (0, 0))                          # Draw it on the screen

This change the color value of each pixel. If you set red to 255, it will add 255 to red for all pixels. But if you set 255 to all colors the image will be white.

Note that you need to install NumPy to use this and you can do so by doing this:

pip install numpy

Also you could try replacing the last two lines for

pygame.surfarray.blit_array(screen, colored_img)

Which should be faster but it didn't work for me so I converted the array into a surface then blitted it on the screen.

If that doesn't answer your question maybe these will:

Upvotes: 1

Dennis Lukas
Dennis Lukas

Reputation: 593

Write classes and instantiate the objects with color code variables.

You can write a method that will draw the shape/image in combination with state-specific data.

Upvotes: 0

Related Questions