On 10/29/2012 06:39 AM, ic...@tagyourself.com wrote:
That's very kind of you but I don't think it would be particularly fitted to my needs. 
The program I'm trying to code creates an image as an 2D array of "pixels" 
which is defined by RGBA value. My program needs to access and modifies every component 
of every pixels in the image following a set of rules, kind of like the game of life, 
only more complex.

In fact I only need a library to "push" this array of pixels in a displayable 
format for the GUI and in PNG format to write the image to disk. I don't need to do any 
fancy stuff with the image, just being able to display and write it.


Then, actually, what I am suggesting was *almost* perfect.
To do transparency, you need to write the portable any map (PAM) formation.

Simply print a text header to a file which says:

P7
WIDTH 10
HEIGHT 10
DEPTH 4
MAXVAL 255
TUPLTYPE RGB_ALPHA
ENDHDR

And then dump your 2D array to that same file.
A very quick example in 17 lines of code:

io = open( "anyname.pam","w")
x,y = 10,10
gray=(128,128,128,255) # R,G,B,A value
picture = [ [ gray ] * x ] * y # Make a blank gray canvas 2D array

# Do whatever you want to the 2D picture array here!

io.write( "P7\nWIDTH %d\nHEIGHT %d\nDEPTH 4\nMAXVAL 255\nTUPLTYPE RGB_ALPHA\nENDHDR\n" % (x,y) )

for yi in xrange( y ):
    for xi in xrange( x ):
        pixel = picture[yi][xi]
        io.write( chr(pixel[0]) ) # R value
        io.write( chr(pixel[1]) ) # G value
        io.write( chr(pixel[2]) ) # B value
        io.write( chr(pixel[3]) ) # A value
    io.flush()

io.close()

And that's it. You may of course make this more efficient -- I'm just showing it this way for clarity. Many programs can read PAM directly; but for those that can't you can use nettools, or imagemagick, to convert it to PNG.

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to