Python截取屏幕截图并将其保存到缓冲区

标签 python opencv buffer python-imaging-library screenshot

我想截取屏幕截图并将其作为照片(X.jpg) 保存到缓冲区,稍后我可以使用cv2(opencv)从缓冲区中读取相同的图像。
我的行为如下:

编辑我的代码:

from PIL import ImageGrab
from io import BytesIO

ii = ImageGrab.grab()
with BytesIO() as output:
    ii.save(output,format="JPEG")# This line has an error 
    cam = output.getvalue()
result, frame = cv2.imencode('.jpg', cam, encode_param)

我得到这个错误:

TypeError: img is not a numpy array, neither a scalar

谢谢

最佳答案

这是一个演示:

from PIL import ImageGrab
from io import BytesIO
import numpy as np 
import cv2

## (1) Grab the rgb frame as PIL.Image
ii = ImageGrab.grab()
print(type(ii)) # <class 'PIL.Image.Image'>

## (2) Convert PIL.Image to np.array 
rgb = np.array(ii)
print(type(ii)) # <class 'numpy.ndarray'>

## (3) Convert into BGR and display 
bgr = cv2.cvtColor(rgb, cv2.COLOR_RGB2BGR)
cv2.imshow("frame", bgr)
cv2.waitKey()
cv2.destroyAllWindows()

关于Python截取屏幕截图并将其保存到缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53820369/

相关文章:

python - 将非零 numpy 数组元素附加到列表的最快方法

python - 如何计算指定日期内 cpf 的重复次数?

python - Numpy:array1 中同时也是 array2 元素的元素的掩码

android - OnCameraFrame() 调用 TextView/Button setText 错误

python - 使用 tf.estimator 的自定义指标

python - 检测轮廓的形状和内部颜色

android - 无法在 Android 中使用 OpenCV VideoCpature java 类打开视频文件

c++ - 用于 C++ 的 Opencl,buffer.release() 是 protected 成员

javascript - Node.JS 大端 UCS-2

go - 如何在 Go 中有效地处理大型数据数组(超过 10MiB)?