Trouble using pypng to read/write png files RGB 8 bits per pixel

1,203 views
Skip to first unread message

rovitotv

unread,
May 20, 2012, 9:09:42 PM5/20/12
to PyPNG Users
Greetings,
I really like pypng it works very well for us when we use it to
create images from numpy. Now I want to read a image make some simple
manipulations then write it back out. Based of the exnumpy.py code
below is the code that I have. The results I see are very odd, it
looks like the color planes are out of whack and the result image is
the transpose of the original. For example if the original image was
640 x 480 then the results image will be 480 x 640. The exnumpy.py
example runs perfectly but it is a square image and is uint16. I
wonder if somebody could take a look and give me a helping hand.
Thanks!


# this is a test of pypng I am using the files in
# /miramar1/LSEL/calibration/sparse_test
import png as png
import numpy as np
import itertools as itertools

r = png.Reader('/miramar1/LSEL/calibration/sparse_test/
sparse_test00000.png')
rowCount, columnCount, pngData, metaData = r.asDirect()
bitDepth=metaData['bitdepth']
planeCount = metaData['planes']
print "BitDepth",
print bitDepth
print "planeCount", planeCount
image_2d = np.vstack(itertools.imap(np.uint8, pngData))
# for easier referencing you could make the image into 3d like so
image_3d = np.reshape(image_2d, (rowCount, columnCount, planeCount))
print image_3d.shape

# draw a diag line in the image data
for x in range(image_3d.shape[0]):
for y in range(image_3d.shape[1]):
if (x == y):
image_3d[x,y,0] = 255;
image_3d[x,y,1] = 0;
image_3d[x,y,2] = 0;

# now lets try and write a new png file
fd = open('testPngWrite.png', 'wb')
rowCount, columnCount, planeCount = image_3d.shape
pngWriter = png.Writer(columnCount, rowCount, greyscale=False,
alpha=False, bitdepth=8)
pngWriter.write(fd,np.reshape(image_3d, (-1, columnCount*planeCount)))

fd.close()

David Jones

unread,
May 25, 2012, 5:21:40 AM5/25/12
to pypng...@googlegroups.com
On 21 May 2012 02:09, rovitotv <rovi...@gmail.com> wrote:
> Greetings,
>    I really like pypng it works very well for us when we use it to
> create images from numpy.

Thanks!

>  Now I want to read a image make some simple
> manipulations then write it back out.  Based of the exnumpy.py code
> below is the code that I have. The results I see are very odd, it
> looks like the color planes are out of whack and the result image is
> the transpose of the original.  For example if the original image was
> 640 x 480 then the results image will be 480 x 640.  The exnumpy.py
> example runs perfectly but it is a square image and is uint16.  I
> wonder if somebody could take a look and give me a helping hand.
> Thanks!

Can you email me an example of the input png that you use? Probably
best to email me directly (d...@pobox.com).

Now that I have a Linux box, I should be able to use numpy more easily.

Cheers,
drj

David Jones

unread,
Jun 1, 2012, 7:06:50 AM6/1/12
to pypng...@googlegroups.com
On 21 May 2012 02:09, rovitotv <rovi...@gmail.com> wrote:
> Greetings,
>    I really like pypng it works very well for us when we use it to
> create images from numpy.  Now I want to read a image make some simple
> manipulations then write it back out.  Based of the exnumpy.py code
> below is the code that I have. The results I see are very odd, it
> looks like the color planes are out of whack and the result image is
> the transpose of the original.  For example if the original image was
> 640 x 480 then the results image will be 480 x 640.  The exnumpy.py
> example runs perfectly but it is a square image and is uint16.  I
> wonder if somebody could take a look and give me a helping hand.
> Thanks!

asDirect returns (width,height,pixels,meta) therefore...

this line:
rowCount, columnCount, pngData, metaData = r.asDirect()
should be:
columnCount, rowCount, pngData, metaData = r.asDirect()

Also in the nested for loops where you draw the diagonal line, you are
indexing the first axis of the numpy array with a variable called 'x',
but the first axis of the numpy array is the vertical axis in the PNG
image data (and so would conventionally be called 'y'). Same for the
'y' variable, but swapped.

I guess I should make that point clearer in the documentation.

Cheers,
drj

Todd Rovito

unread,
Jun 3, 2012, 6:12:26 PM6/3/12
to pypng...@googlegroups.com
On Fri, Jun 1, 2012 at 7:06 AM, David Jones <d...@pobox.com> wrote:
> On 21 May 2012 02:09, rovitotv <rovi...@gmail.com> wrote:
>> Greetings,
>>    I really like pypng it works very well for us when we use it to
>> create images from numpy.  Now I want to read a image make some simple
>> manipulations then write it back out.  Based of the exnumpy.py code
>> below is the code that I have. The results I see are very odd, it
>> looks like the color planes are out of whack and the result image is
>> the transpose of the original.  For example if the original image was
>> 640 x 480 then the results image will be 480 x 640.  The exnumpy.py
>> example runs perfectly but it is a square image and is uint16.  I
>> wonder if somebody could take a look and give me a helping hand.
>> Thanks!
>
> asDirect returns (width,height,pixels,meta) therefore...
>
> this line:
> rowCount, columnCount, pngData, metaData = r.asDirect()
> should be:
> columnCount, rowCount, pngData, metaData = r.asDirect()
Yes, that fixed it!!!! I didn't read the documentation or perhaps
just foobar'ed the order of the parameters. Thanks



> Also in the nested for loops where you draw the diagonal line, you are
> indexing the first axis of the numpy array with a variable called 'x',
> but the first axis of the numpy array is the vertical axis in the PNG
> image data (and so would conventionally be called 'y').  Same for the
> 'y' variable, but swapped.
I see your point the way I wrote that loop was not very clear...


> I guess I should make that point clearer in the documentation.
I actually think the documentation is pretty good but perhaps a few
more examples? For me I can never have enough examples. Thanks for
the assistance I find pypng very useful and especially easy to
install.
Reply all
Reply to author
Forward
0 new messages