473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6911
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()

Mar 9 '06 #2

"Brian L. Troutwine" <go************ *@gmail.com> wrote in message
news:11******** **************@ v46g2000cwv.goo glegroups.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.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).
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
3862
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 pixel. I need a way to save this this byte array as a gif. Does anyone have any suggestions???
2
10941
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
5060
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 PixelFormat8bppIndexed) I wanted to assign the values of each pixel using bigim.SetPixel(x,y,integer value between 0 to 255)
1
7172
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 Graphics object cannot be created from an image that has an indexed pixel format." 'My code
2
3831
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. Public Sub ConvertBadBitmap(ByVal FilePath As String) Dim objPicture As New Bitmap(816, 1056, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale) objPicture = objPicture.FromFile(FilePath) objPicture.Save("C:\ConvertPHOTO.bmp")
13
2386
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 appear as a dot. I can think of a way to define a 1-pixel long line but it is ugly and clumsy. I subtract .5 from the given pixel's X and Y coordinate for the first System.Drawing.PointF structure, and add .5 to the other PointF stucture. It...
30
9069
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 450-1000 milliseconds I want speeds less than 10 milliseconds
8
47939
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 that image in grayscale format.How do i do that.It still saves in true color format. -i used picturebox1.image.save() method -i tried to convert an 8-bit image into grayscale but was unable to do it.
8
3547
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 Bitmap, change one pixel color and show it, but I'm sure there is another way of doing it. Please help, Ofir.
0
8752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9174
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.