30

Taking an image as input, how can I get the rgb matrix corresponding to it? I checked out the numpy.asarray function. Does that give me the rgb matrix or some other matrix?

5 Answers 5

24

Note that this answer is outdated as of 2018; scipy has deprecated imread, and you should switch to imageio.imread. See this transition doc about differences between the two. The code below should work with no changes if you just import the new library in place of the old, but I haven’t tested it.


The simplest answer is to use the NumPy and SciPy wrappers around PIL. There's a great tutorial, but the basic idea is:

from scipy import misc
arr = misc.imread('lena.png') # 640x480x3 array
arr[20, 30] # 3-vector for a pixel
arr[20, 30, 1] # green value for a pixel

For a 640x480 RGB image, this will give you a 640x480x3 array of uint8.

Or you can just open the file with PIL (or, rather, Pillow; if you're still using PIL, this may not work, or may be very slow) and pass it straight to NumPy:

import numpy as np
from PIL import Image
img = Image.open('lena.png')
arr = np.array(img) # 640x480x4 array
arr[20, 30] # 4-vector, just like above

This will give you a 640x480x4 array of type uint8 (the 4th is alpha; PIL always loads PNG files as RGBA, even if they have no transparency; see img.getbands() if you're every unsure).

If you don't want to use NumPy at all, PIL's own PixelArray type is a more limited array:

arr = img.load()
arr[20, 30] # tuple of 4 ints

This gives you a 640x480 PixelAccess array of RGBA 4-tuples.

Or you can just call getpixel on the image:

img.getpixel(20, 30) # tuple of 4 ints
2
  • 3
    An update: imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead. source
    – kHarshit
    Aug 18, 2018 at 15:13
  • 1
    @HarshitKumar Thanks for the update; I’ll edit it into the answer. Since this is half a decade old, anything else that should be updated?
    – abarnert
    Aug 18, 2018 at 19:13
14

I have a feeling I'm not doing exactly what you wanted here, so please specify if this is totally off. You could open the image like this and get an array of pixels:

import Image
im = Image.open('Lenna.png')
pixels = list(im.getdata())

This will get you a flat list of RGB data that looks like

[(226, 137, 125), (226, 137, 125), (223, 137, 133), (223, 136, 128), 
 (226, 138, 120), (226, 129, 116), (228, 138, 123), (227, 134, 124), 
 (227, 140, 127), (225, 136, 119), (228, 135, 126), (225, 134, 121),...

Now this will be all pixels in a flat array, if you want a two dimensional array then some additional code would be needed for that. Not sure if there is a direct function for it in PIL.

3
  • Yes this is indeed what I want! Just one question, in this list are the pixels in the order of horizontal traversal or vertical traversal of the image? Or is there no order in this data?
    – Ojas
    Aug 3, 2014 at 7:36
  • 1
    @Ojas: As the docs for Image.getdata say, "values for line one follow directly after the values of line zero, and so on." However, this is not the best way to do this.
    – abarnert
    Aug 3, 2014 at 8:42
  • @Bemmu I'm sorry for replying to your old post. However, I think you might be able to help :) How can I get a numpy array of an RGB image with (m, n) dimensions? For example, two 64X64 pixel RGB images should produce a (2, 12288) array. Thank you in advance! Apr 30, 2016 at 19:58
6

I tried imageio.imread and it worked great, but a minute later stumbled upon a function in matplotlib which worked exactly the same, getting a numpy n by m by 3 array:

from matplotlib import pyplot as plt
image = plt.imread(path)
5

You can do that with Pillow, the getdata method gives you a flat array of the pixels, you can then build a matrix from that using the size of the image.

from PIL import Image

def getPixels(filename):
    img = Image.open(filename, 'r')
    w, h = img.size
    pix = list(img.getdata())
    return [pix[n:n+w] for n in range(0, w*h, w)]
2
  • I had to remove the 'r' argument to open to get this to work. Jul 7, 2021 at 15:55
  • apparently its the default value for the mode argument anyway, so who knows ^^
    – TitouanT
    Jul 8, 2021 at 21:12
1

Also to add, if you or anyone else is using opencv.

 imgc=cv2.imread(file)

or to read in as grayscale

 imgc=cv2.imread(file,0)

If you will be doing some comparison between the images you may want to think about turning the array of pixels into histograms to normalise the data.

   hist = np.histogram(img.flatten(),256,[0,256])[0]

The above line firstly flattens your img array so you do lose the dimensionality of your image. It then produces bins from 0 to 256 (for the grayscale image) and adds the counts from the img to these bins and returns them as hist which can then be plotted. For example, if the 100 bin has a value of 20 it means that 20 pixels in your image had a value of 100.

Hope this adds another possiblity to think about or to anyone looking to get started in opencv.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.