简体   繁体   English

ImageGrab.grab(bbox)和Image.getpixel()一起使用

[英]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. 使用PIL.ImageGrab的抓取功能时,有一个名为bbox的参数,该参数指定应使用屏幕抓图的边框。 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? 我在这里做错了什么?

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. 边界框是一个(left_x, top_y, right_x, bottom_y)元组,因此使用值(500, 500, 600, 700) (left_x, top_y, right_x, bottom_y) (500, 500, 600, 700) ,图像的宽度为100像素,高度为200像素。

    That explains why im.getpixel(510, 510) doesn't work - the coordinate 510 is outside of the image. 这就解释了为什么im.getpixel(510, 510)不起作用-坐标510在图像之外。

  2. The getpixel method takes the coordinates as a (x, y) tuple, not as two separate arguments. getpixel方法将坐标作为(x, y)元组,而不作为两个单独的参数。 The correct syntax is im.getpixel((10, 10)) . 正确的语法是im.getpixel((10, 10))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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