png.from_array on 3D array

293 views
Skip to first unread message

Lewis Anderson

unread,
Nov 22, 2011, 5:51:41 PM11/22/11
to PyPNG Users
Hello,

I am attempting to write a 3D numpy array to a png file (i.e. a color
image). But I get the following error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "visual_cortex.py", line 118, in build_cortex
ret_v1_p.save_as_image('../data/ret_v1.png',ret_v1_n)
File "visual_cortex.py", line 86, in save_as_image
png.fromarray(image,'RGB').save(filename)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/
lib/python2.7/site-packages/png.py", line 1232, in from_array
assert not threed
AssertionError

I then went and looked at the source code, and it looks like this
isn't implemented. Are there plans to finish this functionality?

David Jones

unread,
Nov 23, 2011, 5:34:58 PM11/23/11
to pypng...@googlegroups.com

It's actually really cool that you want this, and i guess it will be
implemented at some point, especially now that i know that at least
one person might use it. And as the cliché goes, patches welcome.

Using numpy.reshape to get a 2D array should be an easy workaround.
Which makes justifying the implementation a bit harder.

Cheers,
drj

Lewis Anderson

unread,
Nov 23, 2011, 6:28:49 PM11/23/11
to pypng...@googlegroups.com
David,

If I were to use numpy.reshape, could I still get a color image? Wouldn't that make the image grayscale? In this case, the goal is color output.

Lewis

David Jones

unread,
Dec 23, 2011, 7:12:08 AM12/23/11
to pypng...@googlegroups.com
On 23 November 2011 23:28, Lewis Anderson <1plus2...@gmail.com> wrote:
> David,
>
> If I were to use numpy.reshape, could I still get a color image? Wouldn't that make the image grayscale? In this case, the goal is color output.

It's intended to work. So yes. For example, this code:

png.from_array([[1,0,0, 0,1,0, 0,0,1]],'RGB;1').save('foo.png')

produces a 3 by 1 colour image, not a 9 by 1 greyscale image.

the mode will force the file to be colour.

Cheers,
drj

Reply all
Reply to author
Forward
0 new messages