I think the format may affect you, it seems that it pnghas three formats ...
>>> help(png)
Boxed row flat pixel::
list([R,G,B, R,G,B, R,G,B],
[R,G,B, R,G,B, R,G,B])
Flat row flat pixel::
[R,G,B, R,G,B, R,G,B,
R,G,B, R,G,B, R,G,B]
Boxed row boxed pixel::
list([ (R,G,B), (R,G,B), (R,G,B) ],
[ (R,G,B), (R,G,B), (R,G,B) ])
Alpha is added at the end of each RGB sequence.
write(self, outfile, rows)
| Write a PNG image to the output file. `rows` should be
| an iterable that yields each row in boxed row flat pixel format.
| The rows should be the rows of the original image, so there
| should be ``self.height`` rows of ``self.width * self.planes`` values.
| If `interlace` is specified (when creating the instance), then
| an interlaced PNG file will be written. Supply the rows in the
| normal image order; the interlacing is carried out internally.
pay attention to each row in boxed row flat pixel format.
Here is a quick example that draws a white square.
>>> rows = [[255 for element in xrange(4) for number_of_pixles in xrange(256)] for number_of_rows in xrange(256)]
>>> import numpy
>>> rows = numpy.zeros((256, 256 * 4), dtype = 'int')
>>> rows[:] = 255
>>> png_writer = png.Writer(width = 256, height = 256, alpha = 'RGBA')
>>> png_writer.write(open('white_panel.png', 'wb'), rows)
, Writer 2 , , , .
| write_array(self, outfile, pixels)
| Write an array in flat row flat pixel format as a PNG file on
| the output file. See also :meth:`write` method.
|
| write_packed(self, outfile, rows)
| Write PNG file to `outfile`. The pixel data comes from `rows`
| which should be in boxed row packed format. Each row should be
| a sequence of packed bytes.
numpy , .
.
, RGB , , (255, 0, 0, 255).
import png
import numpy
rows = numpy.zeros((256, 256, 4), dtype = 'int')
rows[:, :] = [255, 0, 0, 255]
rows[10:40, 10:40] = [0, 255, 255, 255]
locs = numpy.indices(rows.shape[0:2])
rows[(locs[0] - 80)**2 + (locs[1] - 80)**2 <= 20**2] = [255, 255, 0, 255]
png_writer = png.Writer(width = 256, height = 256, alpha = 'RGBA')
png_writer.write(open('colors_panel.png', 'wb'), rows.reshape(rows.shape[0], rows.shape[1]*rows.shape[2]))