简体   繁体   中英

for loop skipping pixels in Image

I am using a for loop to iterate over a jpg image. The image is shown below.

The image is of my timetable for this semester in college. As you can see, there are green cells, yellow cells, and light brown cells. I have the RGB values of each of these colors.

My task is to locate the first green or yellow or light brown pixel. Actually, I want to find out the top left corner of the first slot in the time table ( notice : The first class on Monday, which in my case is green but would be yellow for an empty slot.)

在此处输入图片说明

Here is the python code I wrote to iterate over the image and find out the coordinates of the first point that is either green or yellow.

from PIL import Image
tt = Image.open("tt.jpg")
dim = tt.size
filled_slot = (203, 254, 51) #RGB values of green
empty_slot = ((255, 255, 203), (248, 239, 164))
             #RGB of yellow     RGB of light brown

start = [] #coordinates of the first pixel in the timetable

for i in range(dim[0]):
    for j in range(dim[1]):
       rgb = tt.getpixel((i,j))
       if rgb == filled_slot or rgb == empty_slot[0]:
           start = [i,j]
           break

print(start)

The image below is that of the timetable above with two circled regions: the red circle highlights the area where I expect the output coordinates to be, and the blue circle highlights the area where the program actually locates the coordinates to be.

My logic is that whenever a green or yellow pixel is located, where the iteration is being done column-wise, the loop should break and the point where it breaks is my output. The region circled red is where I expect the program to find the coordinates.

在此处输入图片说明

Expected output: around [117,101]
Obtained output: [982, 99]

Why is it happening? Am I getting the image iteration wrong or is it somehow getting randomized ? What changes should I make to obtain the desired output??

You only break the inner loop, but continue to search the columns. Best, write a function and return the result directly:

from PIL import Image
filled_slot = (203, 254, 51) #RGB values of green
empty_slot = ((255, 255, 203), (248, 239, 164))
             #RGB of yellow     RGB of light brown

def find_first_pixel(image, colors):
    dim = image.size
    for i in range(dim[0]):
        for j in range(dim[1]):
            rgb = image.getpixel((i,j))
            if rgb in colors:
                return [i,j]
    return None

tt = Image.open("tt.jpg")
print(find_first_pixel(tt, (filled_slot, empty_slot[0]))

The problem is in your data, not your code. Your iteration is correct (though as Daniel points out, inefficient) and Python is not randomizing your coordinates.

The colours are pixelated around the black digits. If you print out the colour of the pixels to the left and right of start you will see that they are (251, 254, 207) and (255, 255, 201) . Zoom in on the image at 600x or above and you will see there are yellowish pixels around the digits in the lilac and blue boxes.

You probably need to search for larger areas of the same colour, say a row of 5 pixels, before concluding the the colour indicates the box you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM