grayscale pixel based graphics with pygame

  • Thread starter Brian L. Troutwine
  • Start date
B

Brian L. Troutwine

I've recently begun to teach myself pygame by making a bunch of small
toys. My current toy is cellular automata displayer and I've gotten a
bit stuck on the displaying bit. (If automata isn't the plural of
automaton please forgive me.) The current automata are only binary and
are calculated using 2D Numeric arrays. It had been my assumption that
once the automata was calculated I could then multiply the entire
array by 255 to get a nice array of grayscale pixel values. I had also
hoped to feed the new grayscale array straight into surfarray as the
manual talks in many places about 2Dpixel arrays taking integer pixel
values. But I find the tutorials and manuals confusing and usually end
up with black and blue displays. Could someone provide an explanation,
and maybe a code example, as to how to create grayscale pixel based
graphics with pygame using only 2D arrays?
 
K

Kamilche

import pygame

def main():
SIZE = (300, 200)
WHITE = (255, 255, 255)
pygame.init()

# Create a new grayscale surface
pic = pygame.Surface(SIZE, 0, 8)
palette = tuple([(i, i, i) for i in range(256)])
pic.set_palette(palette)

# Fill it with a gradient
array = pygame.surfarray.pixels2d(pic)
factor = 256.0/SIZE[1]
for i in range(SIZE[1]):
array[:, i] = int(factor * i)

# Draw a star on it
data = (144, 18), (199,166), (63,73), (221, 71), (79,159)
pygame.draw.lines(pic, WHITE, 1, data)

# Save the image and quit
pygame.image.save(pic, 'temp.bmp')
pygame.quit()

main()
 
T

Terry Reedy

Brian L. Troutwine said:
I've recently begun to teach myself pygame by making a bunch of small
toys. My current toy is cellular automata displayer and I've gotten a
bit stuck on the displaying bit. (If automata isn't the plural of
automaton please forgive me.) The current automata are only binary and
are calculated using 2D Numeric arrays. It had been my assumption that
once the automata was calculated I could then multiply the entire
array by 255 to get a nice array of grayscale pixel values.

I presume you are simply recoding 1 as 255 to get visual contrast with 0.
Was you assumption correct?
I had also
hoped to feed the new grayscale array straight into surfarray as the
manual talks in many places about 2Dpixel arrays taking integer pixel
values.

Questions about pygame are better directed to the pygame mailing list, also
accessible as newsgroup gmane.comp.python.pygame at news.gmane.org. The
helpful folks there should also be able to answer simple graphics-related
questions on using numeric (although there is also a numeric/numpy list).
But I find the tutorials and manuals confusing

For python, numeric, or pygame?
>and usually end
up with black and blue displays. Could someone provide an explanation,
and maybe a code example, as to how to create grayscale pixel based
graphics with pygame using only 2D arrays?

See pygame list. There might already be an example that you missed.

Terry Jan Reedy
 
B

Brian L. Troutwine

Thank you, but that wasn't quite what I was looking for. I do admit,
however, that my post wasn't very clear (writting while exceptionally
tired is not a very clever thing to do.) As Terry mentioned below this
should be a question for the pygame mailing lists, so I'll take it
there.

Thanks again though.
 
B

Brian L. Troutwine

Geez, I apologize for my post being so vague. I was terribly tired when
I wrote that, and should have known better than to post.

I was not aware of the pygame mailing list. I will take this question
there.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,289
Messages
2,571,435
Members
48,121
Latest member
ColinHibne

Latest Threads

Top