473,387 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Mar 9 '06 #1
4 6871
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()

Mar 9 '06 #2

"Brian L. Troutwine" <go*************@gmail.com> wrote in message
news:11**********************@v46g2000cwv.googlegr oups.com...
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


Mar 9 '06 #3
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.

Mar 9 '06 #4
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.

Mar 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chad | last post by:
I have a one dimensional byte array that stores the pixel information of my image. The image is 8-bit grayscale so each byte in the array holds a value from 0(black) to 255(white) for a single...
2
by: Anirudh Saria | last post by:
Does anyone know how to read in a grayscale bitmap image in Visual C++ and store the pixel values in an array ? Thanks, Anirudh
1
by: Sinora | last post by:
I am trying to cahnge pixel value of a grayscale image using Bitmap object . Each pixel is consists of 8 bits. Bitmap bigim(720,480,PixelFormat8bppIndexed) ( I tried 8 instead...
1
by: jty202 | last post by:
I know alot of people have the problem with indexed pixel format. I hope someone can show me the solution to this.I am have problem with graphics with the following code giving the error: "A...
2
by: Harry Simpson | last post by:
I'm having trouble reducing the size of a bitmap programmatically: The file size of the original is 2400k. The picture is 816x1056. 16 million colors i think. I only need grayscale render. ...
13
by: Martijn Mulder | last post by:
I try to define a line with the length of 1 unit (1 pixel) that is, in fact, just a point on the screen. But when I draw it with a wide pen with the appropriate StartCap and EndCap (Round), it will...
30
by: Chaos | last post by:
As my first attempt to loop through every pixel of an image, I used for thisY in range(0, thisHeight): for thisX in range(0, thisWidth): #Actions here for Pixel thisX, thisY But it takes...
8
by: platinumhimani | last post by:
-How to convert any image(8,16,24,32 or 64-bit) to 8-bit grayscale -i have tried to convert a 24-bit image to grayscale using setpixel and getpixel functions, in vb.net but i am unable to save...
8
by: ofiras | last post by:
Hi, I made a "Paint" program, but I couldn't find a method to paint 1 pixel using graphic state ("Graphics g = Graphics.FromHwnd(this.Handle);") How can I paint 1 pixel? I guess I can make a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.