在查看源代码后,似乎没有直接访问原始位图的方法。但是,您可以获得编码副本。
首先,获取其编码表示。
bitmap_encoded = bitmap_object.to_string()
这段文本被编码为“b”,后面跟着宽度、逗号、高度、逗号和zlib压缩的原始字节的base64编码。解析编码数据:
import base64
import zlib
first_comma = bitmap_encoded.find(',')
second_comma = bitmap_encoded.find(',', first_comma + 1)
width = int(bitmap_encoded[1:first_comma])
height = int(bitmap_encoded[first_comma+1:second_comma])
bitmap_bytes = zlib.decompress(base64.b64decode(bitmap_encoded[second_comma+1:]))
当我在我的计算机上测试时,红色和蓝色通道是反过来的,因此我假设autopy中的位图采用RGB编码,而不是BMP文件通常使用的BGR编码,这也是PIL所期望的。最后,使用PIL加载图像:
img = PIL.Image.frombytes('RGB', (width, height), bitmap_bytes, 'raw', 'BGR', 0, 1)
如果要正常加载图像而不交换红色和蓝色通道,请执行以下操作:
img = PIL.Image.frombytes('RGB', (width, height), bitmap_bytes)
PIL
中的ImageGrab
,它与autopy.bitmap.capture_screen()
执行相同的操作。链接在此。 - Vasilis G.img = PIL.Image.fromArray(bitmap, mode=xx)
吗?这对于我来说可以处理numpy数组。我看到Autopy.bitmap有一个to_string()
方法,不确定它是否可以获取原始数据。 - bivouac0