grayscale pixel based graphics with pygame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian L. Troutwine

    #1

    grayscale pixel based graphics with pygame

    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?

  • Kamilche

    #2
    Re: grayscale pixel based graphics with pygame

    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.surfarra y.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.lin es(pic, WHITE, 1, data)

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

    main()

    Comment

    • Terry Reedy

      #3
      Re: grayscale pixel based graphics with pygame


      "Brian L. Troutwine" <goofyheadedpun k@gmail.com> wrote in message
      news:1141872392 .131004.247580@ v46g2000cwv.goo glegroups.com.. .[color=blue]
      > 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.[/color]

      I presume you are simply recoding 1 as 255 to get visual contrast with 0.
      Was you assumption correct?
      [color=blue]
      >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.[/color]

      Questions about pygame are better directed to the pygame mailing list, also
      accessible as newsgroup gmane.comp.pyth on.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).
      [color=blue]
      > But I find the tutorials and manuals confusing[/color]

      For python, numeric, or pygame?
      [color=blue]
      >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?[/color]

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

      Terry Jan Reedy




      Comment

      • Brian L. Troutwine

        #4
        Re: grayscale pixel based graphics with pygame

        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.

        Comment

        • Brian L. Troutwine

          #5
          Re: grayscale pixel based graphics with pygame

          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.

          Comment

          Working...