简体   繁体   English

捕获单帧 OpenCV-Python

[英]Capture a Single Frame OpenCV-Python

Good afternoon, I have a question about capturing a single image with OpenCV because most of the codes I have seen for videocapture get many frames until they stop but I am only interested in one as if we were taking a picture.下午好,我有一个关于使用 OpenCV 捕获单个图像的问题,因为我看到的大多数视频捕获代码都会获得很多帧,直到它们停止,但我只对一个感兴趣,就好像我们正在拍照一样。 I attach the code that I have been reviewing and I appreciate your advice.我附上了我一直在审查的代码,感谢您的建议。

import cv2

# Opens the Video file
cap= cv2.VideoCapture('C:/New/Videos/Play.mp4')
i=0
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == False:
        break
    cv2.imwrite('kang'+str(i)+'.jpg',frame)
    i+=1

cap.release()
cv2.destroyAllWindows()

You should be able to use a function through pyautogui like screenshot .您应该能够通过 pyautogui 使用功能,例如screenshot In my opinion this is easier.在我看来,这更容易。 PYAutoGUI has some of OPEN-CV built in as well. PYAutoGUI 也内置了一些 OPEN-CV。

Example:例子:

import pyautogui
screenshot = pyautogui.screenshot()
screenshot.save(r'currentscreenshot.jpg')

Hopefully this helps!希望这会有所帮助! (Edit you may or may not have to install pillow, if so run pip install pillow .) (编辑你可能需要也可能不需要安装枕头,如果是这样运行pip install pillow 。)

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

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