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.gif")
pix = im.load()

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.


@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')


(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


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 - 🙈 )

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

Yes, this way:

im ='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.

@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.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

40 Answered Questions

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

14 Answered Questions

[SOLVED] How do I get a substring of a string in Python?

12 Answered Questions

[SOLVED] Getting the last element of a list

  • 2009-05-30 19:28:53
  • Janusz
  • 2158312 View
  • 2098 Score
  • 12 Answer
  • Tags:   python list indexing

9 Answered Questions

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

  • 2009-11-11 00:30:54
  • y2k
  • 3280021 View
  • 1953 Score
  • 9 Answer
  • Tags:   python list

42 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3340753 View
  • 2943 Score
  • 42 Answer
  • Tags:   python datetime time

20 Answered Questions

[SOLVED] Formula to determine brightness of RGB color

  • 2009-02-27 19:19:31
  • robmerica
  • 307758 View
  • 389 Score
  • 20 Answer
  • Tags:   image colors rgb

19 Answered Questions

[SOLVED] How do I resize an image using PIL and maintain its aspect ratio?

49 Answered Questions

[SOLVED] RGB to Hex and Hex to RGB

1 Answered Questions

[SOLVED] Pillow - Change every pixel's RGB value

4 Answered Questions

[SOLVED] guessing the rgb gradient from a map?

Sponsored Content