By GermainZ


2012-06-16 15:32:25 8 Comments

Is it possible to get the RGB color of a pixel using PIL? I'm using this code:

im = Image.open("image.gif")
pix = im.load()
print(pix[1,1])

However, it only outputs a number (e.g. 0 or 1) and not three numbers (e.g. 60,60,60 for R,G,B). I guess I'm not understanding something about the function. I'd love some explanation.

Thanks a lot.

4 comments

@Martin Thoma 2016-07-24 09:02:55

Not PIL, but imageio.imread might still be interesting:

import imageio
im = scipy.misc.imread('um_000000.png', flatten=False, mode='RGB')
im = imageio.imread('Figure_1.png', pilmode='RGB')
print(im.shape)

gives

(480, 640, 3)

so it is (height, width, channels). So the pixel at position (x, y) is

color = tuple(im[y][x])
r, g, b = color

Outdated

scipy.misc.imread is deprecated in SciPy 1.0.0 (thanks for the reminder, fbahr!)

@fbahr 2019-05-12 07:52:46

PSA: scipy.misc.imread is deprecated! imread is deprecated in SciPy 1.0.0, and will be removed in 1.2.0. Use imageio.imread instead.

@Martin Thoma 2019-05-12 08:07:00

Thanks for the reminder, fbahr! (I actually was involved in deprecating it - github.com/scipy/scipy/issues/6242 🙈 )

@Levon 2012-06-16 15:52:09

Yes, this way:

im = Image.open('image.gif')
rgb_im = im.convert('RGB')
r, g, b = rgb_im.getpixel((1, 1))

print(r, g, b)
(65, 100, 137)

The reason you were getting a single value before with pix[1, 1] is because GIF pixels refer to one of the 256 values in the GIF color palette.

See also this SO post: Python and PIL pixel values different for GIF and JPEG and this PIL Reference page contains more information on the convert() function.

By the way, your code would work just fine for .jpg images.

@Musixauce3000 2016-04-28 18:04:40

Can this be done for the computer screen, and not just an image file?

@user7969920 2017-11-06 02:39:24

Is Image.getpixel() 0-based or 1-based? I mean, the upper-left-most pixel is (0,0) or is it (1, 1)?

@Nolan 2017-11-24 18:22:44

@NimaBavari It is 0-based.

@Akash Kandpal 2018-01-01 11:40:08

It's working fine for png files too .

@Marcel Wilson 2016-07-07 20:11:31

An alternative to converting the image is to create an RGB index from the palette.

from PIL import Image

def chunk(seq, size, groupByList=True):
    """Returns list of lists/tuples broken up by size input"""
    func = tuple
    if groupByList:
        func = list
    return [func(seq[i:i + size]) for i in range(0, len(seq), size)]


def getPaletteInRgb(img):
    """
    Returns list of RGB tuples found in the image palette
    :type img: Image.Image
    :rtype: list[tuple]
    """
    assert img.mode == 'P', "image should be palette mode"
    pal = img.getpalette()
    colors = chunk(pal, 3, False)
    return colors

# Usage
im = Image.open("image.gif")
pal = getPalletteInRgb(im)

@KobeJohn 2012-06-16 15:52:49

GIFs store colors as one of x number of possible colors in a palette. Read about the gif limited color palette. So PIL is giving you the palette index, rather than the color information of that palette color.

Edit: Removed link to a blog post solution that had a typo. Other answers do the same thing without the typo.

Related Questions

Sponsored Content

39 Answered Questions

[SOLVED] How to get screen dimensions as pixels in Android

11 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 1837871 View
  • 1865 Score
  • 11 Answer
  • Tags:   python list indexing

7 Answered Questions

[SOLVED] How do I get the number of elements in a list?

  • 2009-11-11 00:30:54
  • y2k
  • 3154633 View
  • 1839 Score
  • 7 Answer
  • Tags:   python list

36 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3061630 View
  • 2610 Score
  • 36 Answer
  • Tags:   python datetime time

45 Answered Questions

[SOLVED] RGB to Hex and Hex to RGB

19 Answered Questions

[SOLVED] Formula to determine brightness of RGB color

  • 2009-02-27 19:19:31
  • robmerica
  • 281981 View
  • 365 Score
  • 19 Answer
  • Tags:   image colors rgb

1 Answered Questions

1 Answered Questions

4 Answered Questions

[SOLVED] guessing the rgb gradient from a map?

Sponsored Content