While using the grab function from PIL.ImageGrab, there's a parameter called bbox, which specifies the bounding box of which the screengrab should be taken. And when I want to get the value of a specific pixel, what should the coordinates be? For example:
im = ImageGrab.grab(bbox=(500, 500, 600, 700)
print(im.getpixel(510, 510))
print(im.getpixel(10, 10))
Both the statements give me an error. What am I doing wrong here?
答案 0 :(得分:1)
You've made two mistakes:
Since you've specified a bounding box, the resulting image is smaller than your screen resolution. The bounding box is a (left_x, top_y, right_x, bottom_y)
tuple, so with the values (500, 500, 600, 700)
, the image has a width of 100 pixels and a height of 200 pixels.
That explains why im.getpixel(510, 510)
doesn't work - the coordinate 510 is outside of the image.
The getpixel
method takes the coordinates as a (x, y)
tuple, not as two separate arguments. The correct syntax is im.getpixel((10, 10))
.