Hi, how can I open an image and get the R,G,B values of just the first line?

from PIL import Image

im=Image.open("image.bmp")
img=im.getcolors()
print img

I've tried im.getdata() , im.histogram, etc.... but again, what I'm looking for is more of R,G,B pixel counts for just the 1st line in the image.

First let's create a sample image file:

# drawing with PIL (Python Image Library)
# draw and save a small French flag (blue, white, red)

from PIL import Image, ImageDraw

# create a new 24x16 pixel image surface (default is black bg)
img = Image.new("RGB", (24, 16))

# set up the new image surface for drawing
draw = ImageDraw.Draw (img)

# draw a blue rectangle
# x1, y1 are upper left corner coordinates here
w = 8
h = 16
x1 = 0
y1 = 0
draw.polygon([(x1,y1),
(x1+w,y1), (x1+w,y1+h), (x1,y1+h)], fill='blue')

# draw a white rectangle
w = 8
h = 16
x1 = 8
y1 = 0
draw.polygon([(x1,y1),
(x1+w,y1), (x1+w,y1+h), (x1,y1+h)], fill='white')

# draw a red rectangle
w = 8
h = 16
x1 = 16
y1 = 0
draw.polygon([(x1,y1),
(x1+w,y1), (x1+w,y1+h), (x1,y1+h)], fill='red')

img.save("french_flag.bmp")

Now let's take a look at the first line of (r.g.b) tuples:

# show the list of (r, g, b) tuples of a bitmap image

from PIL import Image

# French flag has a simple blue white red left to right pattern
img = Image.open("french_flag.bmp")
w, h = img.size

print(w, h)  # test --> (24, 16) 

data = img.getdata()

# show list of the first line (w pixels wide) of (r,g,b) tuples
print(list(data)[:w])

"""
my output -->
[(0, 0, 255), (0, 0, 255), (0, 0, 255), (0, 0, 255), (0, 0, 255), 
(0, 0, 255), (0, 0, 255), (0, 0, 255), (255, 255, 255), (255, 255, 255), 
(255, 255, 255), (255, 255, 255), (255, 255, 255), (255, 255, 255), 
(255, 255, 255), (255, 255, 255), (255, 0, 0), (255, 0, 0), (255, 0, 0), 
(255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0), (255, 0, 0)]
"""

Wow....now if that prints out that particular list of tuples, how could you go about locating a pixels x,y position? Let's say one of the R,G,B tuples was this: (255,0,9) and you wanted the position of that blue value?

I guess if you know how many pixels wide the image is, you could just figure out what indices correspond to the cut-off for each row in the list. Like if an image was 10 pixels wide, then items 0 - 9 in the list would correspond to the first row, and 10 - 19 to the second, etc. So item 17 would have the coordinates of 8th from the left, and in the second row? I think haha. Sorry if that doesn't make much sense. I tried :P

Yeah shadwickman is right, i did just that for one of my programs i used this kind of loop:

unsorted = #list of all image values RGB
ROWS = #amount of rows in image
COLLS = #amount of collumns
imagevals = []
count = 0
for f in range(len(unsorted)/ROWS):
    temp = []
    for f in range(COLLS):
        temp.append(unsorted[count])
        count += 1
    imagevals.append(temp)

Note that this is untested code but it should work or be close to it, the main thing i am trying to show is how you would go about doing it.

This article has been dead for over six months. Start a new discussion instead.