Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32424

Re: I need help installing pypng in Python 3.3

Date 2012-10-29 07:48 -0700
From Andrew Robinson <andrew3@r3dsolutions.com>
Subject Re: I need help installing pypng in Python 3.3
References <c2254bc0-6dd6-4c4c-b73a-a89ebbe64c4c@googlegroups.com> <mailman.3010.1351516779.27098.python-list@python.org> <b84c9cd0-cd89-42c4-8c3d-24a8bef56830@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.3049.1351547648.27098.python-list@python.org> (permalink)

Show all headers | View raw


On 10/29/2012 06:39 AM, icgwh@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 05:23 -0700
  Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 06:18 -0700
  Re: I need help installing pypng in Python 3.3 Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-28 23:13 -0700
    Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 06:39 -0700
      Re: I need help installing pypng in Python 3.3 Andrew Robinson <andrew3@r3dsolutions.com> - 2012-10-29 07:48 -0700
        Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-30 03:38 -0700
        Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-30 03:38 -0700
    Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 06:39 -0700
  Re: I need help installing pypng in Python 3.3 David Robinow <drobinow@gmail.com> - 2012-10-29 11:08 -0400
    Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 11:16 -0700
    Re: I need help installing pypng in Python 3.3 icgwh@tagyourself.com - 2012-10-29 11:16 -0700

csiph-web