[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Re: Grayscale pixel based graphics with pygame and 2D arrays



On Thu, 2006-03-09 at 14:51 -0500, owner-pygame-users@xxxxxxxx wrote:
> I've recently begun to attempt to teach myself pygame for various reasons. At  
> the moment I'm attempting to display binary 1D cellular automata, but I'm a  
> bit stuck on doing grayscale graphics.  
>   
> It had been my hope to display these arrays with pygame, the idea being to use  
> surfarray to map each position of the array onto a pixel of a surface. The  
> only problem with this has been that surfarray takes RGB values, and I'm not  
> quite sure how to use only integers to create grayscale colors. At the moment  
> I can multiply each array by 255 to get bluescale graphics, but that's not  
> ideal.  

Since your are dealing with binary 1s and 0s it will be hard to get more
than black and white (or any two colors, as you see with blue and black)

Pygame colors are 0 to 255 with three colors for red, green, and blue.

It sounds like you are very close if you are already seeing a blue and
black image. Your problem is that you are assigning to one of the 2D
surfarray types. This expects you to prepack your colors into an
integer. In the default case, the lower 8 bits are for the blue channel.

If you use a 3D array with the same code, you can assign 0 and 255
values to the red, green, and blue channels all together. This will
produce black and white images the same as your blue and black version.

To get some shades of grey from your binary data you will need to do
some filtering. The easiest at this point would be to use
pygame.transform.rotozoom(image, 0.0, 0.5). This will make your image
half the size, but it will combine 4 black and white values into 4
shades of grey. You could scale the image by 0.25 for an even smaller
image that gets even better grey coverage.