Load imageΒΆ

Date:2008-12-08 (last modified), 2006-10-10 (created)

Image processing often works on gray scale images that were stored as PNG files. How do we import / export that file into {{{python}}}?

  • Here is a recipy to do this with Matplotlib using the {{{imread}}} function (your image is called {{{lena.png}}}).

from pylab import imread, imshow, gray, mean
a = imread('lena.png')
 # generates a RGB image, so do
aa=mean(a,2) # to get a 2-D array
imshow(aa)
gray()

This permits to do some processing for further exporting such as for [:Cookbook/Matplotlib/converting_a_matrix_to_a_raster_image:converting a matrix to a raster image]. In the newest version of pylab (check that your {{{pylab.matplotlib.version}}} is superior to {{{'0.98.0'}}}) you get directly a 2D numpy array if the image is grayscale.

  • to write an image, do import Image mode = 'L' size= (256, 256) imNew=Image.new(mode , size) mat = numpy.random.uniform(size = size) data = numpy.ravel(mat) data = numpy.floor(data * 256)

imNew.putdata(data) imNew.save("rand.png") </code>

  • this kind of functions live also under {{{scipy.misc}}}, see for instance {{{scipy.misc.imsave}}} to create a color image: from scipy.misc import imsave import numpy a = numpy.zeros((4,4,3)) a[0,0,:] = [128, 0 , 255] imsave('graybackground_with_a_greyish_blue_square_on_top.png',a)

Section author: LaurentPerrinet