[Image-SIG] Array to Image transformations

Joao S. O. Bueno gwidion at mpc.com.br
Wed Feb 27 12:29:15 CET 2008


On Wednesday 27 February 2008, Frédéric Mantegazza wrote:
> On mardi 26 février 2008, Joao S. O. Bueno wrote:
> > On Thu 21 Feb 2008 05:47:22 Sahar Vilan wrote:
> > > Can anyone help me with transformation from numpy array to
> > > image (at PIL library)?
> > > I want to change an array (at numpy) into an image (at Image)
> > > in order to rotate the image and save it in standard image
> > > format.
> >
> > Sorry for the long delay.
> > These convertions in python among data and image representation
> > indeed require some experimentation.
> >
> > (In my todo list, wiht low priority, I have a python module that
> > would seamlessly translate image data across various image
> > handling bindings/ibraries and numerical types).
> >
> > Fortunatey, in this case it is rather simple:
> >
> > For numpy.array -> python image, you do something like:
> >
> > a = numpy.array ("\x0"  (WIDTH  * HEIGHT  * 3), "B")
> > #code to create imag data inside array
> > # (...)
> > i  = Image.new("RGB", (WIDTH, HEIGHT))
> >
> > i.fromstring(a.tostring())
> > i.save(filename)
>
> Hello,
>
> I'm also very interested by such features, because I think it could
> be a nice way to use color management libraries. For example, the
> little cms python wrapper could handle arrays (which it does not
> yet), and then, PIL could convert image from/to arrays.
>
> > i.fromstring(a.tostring())
>
> What is the performances of such transformations?

Hi. I did not measure it, and just tested with a small image.
I think that computing-wise this is not intensive, but I see this 
could need as much as twice the image size in RAM - one for copying 
the array data to a string, and another to copy the same data again 
to a PIL image.

It maybe however that pynum.tostring points to the same data in system 
memory, so you just need to have memory for the Image object to be 
created.

As for CPU performance - this should be fast, as it is simply copying 
data around, and both numpy and PIL should do it the fastest possible 
way.  Before finding out both to/from string method I made some 
trials with list compreensions tgo copy color components around. That 
was very slow, even for a 256x256 test image.


	js
	-><-




More information about the Image-SIG mailing list