3

我正在使用 Autopy 和 Pillow 在 Python 中开发屏幕刮板。

是否可以将位图对象转换为枕头图像对象?

我目前的解决方案是将位图对象保存为图像文件,然后使用路径创建 Pillow 图像对象。由于硬盘驱动器 I/O,这种方法真的很慢。

我目前(非常慢)的解决方案:

from PIL import Image
import autopy

bitmap_object = autopy.bitmap.capture_screen()
bitmap_object.save('some/path.png') # VERY SLOW!
img = Image.open('some/path.png')

问题:如果不将位图对象保存到硬盘驱动器,是否可以实现上述功能?

4

2 回答 2

2

查看源代码后,似乎无法直接访问原始位图。但是,您可以获得编码副本。

首先,获取其编码表示。

bitmap_encoded = bitmap_object.to_string()

这被编码为“b”,后跟宽度、逗号、高度、逗号和 zlib 压缩原始字节的 base64 编码。解析编码数据:

import base64
import zlib

# b3840,1080,eNrsf...H1ooKAs=
#      ^    ^
first_comma = bitmap_encoded.find(',')
second_comma = bitmap_encoded.find(',', first_comma + 1)

# b3840,1080,eNrsf...H1ooKAs=
#  ^  ^
width = int(bitmap_encoded[1:first_comma])

# b3840,1080,eNrsf...H1ooKAs=
#       ^  ^
height = int(bitmap_encoded[first_comma+1:second_comma])

# b3840,1080,eNrsf...H1ooKAs=
#            ^
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)
于 2018-02-02T22:41:25.803 回答
0

看起来现在这有一个来自 autopy 的解决方案

import autopy
import PIL.Image

bmp = autopy.bitmap.capture_screen()
width, height = int(round(bmp.width * bmp.scale)), int(round(bmp.height * bmp.scale))
img = PIL.Image.frombytes('RGB', (width, height), bytes(bmp))
于 2020-10-11T22:24:53.883 回答