[英]Converting an autopy bitmap to a Pillow image
我正在使用 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')
问:是否可以不将位图对象保存到硬盘来实现上述功能?
查看源代码后,似乎没有办法直接访问原始位图。 但是,您可以获得编码副本。
首先,获取其编码表示。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.