Using colourmap from matplotlib

Hi,

I am trying to create an image from an array using PIL, numpy and a
colourmap from matplotlib.

I got the colourmap by using ipython and the following code

colourmap = []
for i in xrange(256):
    r,g,b,a = cm.jet(i)
    r = int(round((r * 255),0))
    g = int(round((g * 255),0))
    b = int(round((b* 255),0))
    colourmap.append((r,g,b))

The following is used to plot an array:

def array2image(array, scale="log", filename="tmp"):
  
# create image of the correct size
  new_image = Image.new("RGB", array.shape)
# scan through a matrix and use Image.putdata() to put it in.
  x_size, y_size = array.shape
  largest = array.max()
  a_list = array.ravel()
  new_list = []
  
  for x in a_list:
    if x == 0:
      new_list.append((0,0,0))
    else:
      value = (log(x)/log(largest))*255
      value = int(round(value,0))
      value = blues_colourmap[value]
      #print value
      new_list.append(value)

  new_image.putdata(new_list)

  new_image.save(filename+".bmp")

The image that is outputted is certainly correct for the data, however
it is just no where near as "pretty" as the image you get from using

plt.imshow(am_array)

I'd like to get something that looks the same. I don't think the
problems are because of the colourmap but rather because of my log
scaling. Could someone please explain how matplotlib scales the image
to make it look so nice?

Regards,

Ciarán

2010/3/18 Ciarán Mooney <general.mooney@...982...>:

                       value = (log(x)/log(largest))*255

Just two thoughts:

1) I doubt the statement cited above is not correct, as it may also
yield negative values as soon as 0 < x < 1. In fact, you are
calculating log_{largest}(x). This crosses zero for x = 1. What I
deem correct instead, is a linear mapping of the log space (logmin,
log(largest)) onto (0, 1). There logmin may be negative.

2) When matplotlib does the job, maybe use:

axes = fig.add_axes([0, 0, 1, 1])
axes.imshow(...)

This lets the axes fill the whole figure space, without margins. You
can also turn off the ticks if you want.

Friedrich

I haven't tried it, but maybe it's to do with the fact that you're quantising the colourmap to 256 values; I think matplotlib computes the exact rgb values using interpolation. If the only reason you're using PIL is to get a .bmp file, maybe you could save the file straight from matplotlib as a .png then externally convert it to a .bmp

Gary R.

Ciarán Mooney wrote:

Hi,

I am trying to create an image from an array using PIL, numpy and a
colourmap from matplotlib.

<snip>

···

I'd like to get something that looks the same. I don't think the
problems are because of the colourmap but rather because of my log
scaling. Could someone please explain how matplotlib scales the image
to make it look so nice?

Regards,

Ciarán

Hi,

I haven't tried it, but maybe it's to do with the fact that you're
quantising the colourmap to 256 values; I think matplotlib computes the
exact rgb values using interpolation. If the only reason you're using
PIL is to get a .bmp file, maybe you could save the file straight from
matplotlib as a .png then externally convert it to a .bmp

I thought so until I put in cm.jet[255] and it maxed out. The
granularity of the 255 colours that I get from exporting the RGB
values is more than enough for the ranges of data I use. So I am happy
with that.

I am using PIL because I plan to plug in a Tkinter interface which can
directly accept PIL image instances.

Ciarán

2010/3/20 Ciarán Mooney <general.mooney@...982...>:

I am using PIL because I plan to plug in a Tkinter interface which can
directly accept PIL image instances.

You can render matplotlib figures to PIL using following code:

figure.set_size_inches(float(shape[0]) / figure.dpi, float(shape[1]) /
figure.dpi)

canvas = matplotlib.backends.backend_agg.FigureCanvasAgg(figure)
canvas.draw()
image_string = canvas.tostring_rgb()

image = Image.fromstring("RGB", shape, image_string)

fwiw,
Friedrich