Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
923 views
in Technique[技术] by (71.8m points)

python - ImageGrab.grab(bbox) and Image.getpixel() Used together

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You've made two mistakes:

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

  2. The getpixel method takes the coordinates as a (x, y) tuple, not as two separate arguments. The correct syntax is im.getpixel((10, 10)).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...