[Numpy-discussion] Images and numpy

Nadav Horesh nadavh at visionsense.com
Sun Oct 19 02:08:46 EDT 2008


I encountered an exception: "nonstandard" format like 16 bits tiff I/O works only via the tostring/fromstring interface.

  Nadav


-----הודעה מקורית-----
מאת: numpy-discussion-bounces at scipy.org בשם Lane Brooks
נשלח: א 19-אוקטובר-08 06:45
אל: Discussion of Numerical Python
נושא: Re: [Numpy-discussion] Images and numpy
 
Robert Kern wrote:
> On Sat, Oct 18, 2008 at 23:07, Lane Brooks <lbrooks at mit.edu> wrote:
>   
>> What are the preferred ways to get images, like jpgs and pngs, from disk
>> into a numpy array and from a numpy array to disk?
>>
>> I did some google searches and found a PEP thread where Travis was
>> proposing an extended buffer protocol that would make for easier
>> interoperability with libraries such as PIL.  Did that materialize?
>>     
>
> Yes. There are two protocols, a Python level one, and a C level one.
> Python 2.6 is the first release where the C one is standard. Recent
> PILs support the Python level one.
>
>
> In [9]: import Image
>
> In [10]: img = Image.open('icon.png')
>
> In [11]: import numpy
>
> In [12]: numpy.asarray(img)
> Out[12]:
> array([[[  0,   0,   0,   0],
>         [  0,   0,   0,   0],
>         [  0,   0,   0,   0],
>         ...,
>         [  0,   0,   0,   0],
>         [  0,   0,   0,   0],
>         [  0,   0,   0,   0]],
> ...
>
> In [14]: img2 = Image.fromarray(Out[12])
>
> In [15]: img2
> Out[15]: <Image.Image instance at 0x1c0baa8>
>
> In [16]: img2.size
> Out[16]: (48, 48)
>
>
> You may also want to look at scipy's scipy.misc.pilutil module. It
> doesn't use these newer APIs, but it does have extra functionality
> like scaling float arrays to [0..255] to write out to PNGs and such.
>
>   
Great!  Thanks for the info.  Just what I need!

Lane

-------------- next part --------------
A non-text attachment was scrubbed...
Name: winmail.dat
Type: application/ms-tnef
Size: 3620 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20081019/106b6664/attachment.bin>


More information about the NumPy-Discussion mailing list