[SciPy-user] toimage

Stefan van der Walt stefan at sun.ac.za
Wed Jun 7 07:35:53 CDT 2006


Hi Maik

On Wed, Jun 07, 2006 at 11:42:08AM +0200, Maik Trömel wrote:
> i've got some problems creating a RGB-picture with "toimage".
> I have createt an array with integer values betweeen 0 and 4095.
> I tried with:
> 
> i = scipy.misc.toimage(arr, high = 0, low = 255, cmax=4095, cmin=0, pal=3, mode='P')

'pal' is the image colourmap.  If you look at the toimage docstring,
it says:

    For 2-D arrays, if pal is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then mode='P', otherwise mode='L', unless mode is given
    as 'F' or 'I' in which case a float and/or integer array is made

For example, let's generate a square image that has a linear gradient:

import numpy as N
im = N.empty((100,100),dtype=N.Float)
im[:] = N.linspace(0,4096,100)

If you have matplotlib installed, you can view this using

import pylab as P
P.imshow(im,cmap=P.cm.gray)

To convert the array im to a PIL image, you can now do

I = scipy.misc.toimage(im,high=255,low=0,cmax=4095,cmin=0)

and display it using

I.show()

Now, if we'd like to change the colourmap, we'll have to generate one.
A colourmap is of shape (N,3), i.e. the RGB values for N indeces.  The
following commands will generate a "cool" colourmap.

map = N.empty((256,3),dtype=N.uint8)
map[:,0] = N.arange(0,256,dtype=N.uint8)
map[:,1] = map[:,0][::-1]
map[:,2] = map[:,0] + map[:,1]

We create the PIL image using

I = scipy.misc.toimage(im,high=255,low=0,cmax=4095,cmin=0,pal=map,mode='P')

after which we can display it using

I.show()

Regards
Stéfan



More information about the SciPy-user mailing list