摄像头手动截图
import cv2
import os
import time
url = 0
cap = cv2.VideoCapture(url)
index = 1
if os.path.exists("jpg") == False:
os.mkdir("jpg")
while cap.isOpened():
ret, frame = cap.read()
cv2.imshow("Capture_Paizhao", frame)
k = cv2.waitKey(1) & 0xFF
if k == ord('s'):
cv2.imwrite("jpg/" + str(index) + "_" + str(int(time.time())) + ".jpg", frame)
print("图像宽高", int(cap.get(3)), int(cap.get(4)))
print("已保存" + str(index) + ".jpg")
print("-" * 30)
index += 1
elif k == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
视频自动截图
import cv2
import os
o = "18"
n, m = 30, 10
cap = cv2.VideoCapture(o + ".mp4")
if os.path.exists("jpg_" + o) == False:
os.mkdir("jpg_" + o)
l = 0
index = 0
while cap.isOpened():
ret, frame = cap.read()
l += 1
if not ret:
break
if l == (n * m):
l = 0
cv2.imwrite("jpg_" + o + "/" + "q0" + o + "_" + str(index) + ".jpg", frame)
print("图像宽高", int(cap.get(3)), int(cap.get(4)))
print("已保存" + str(index) + ".jpg")
print("-" * 30)
index += 1
cap.release()
cv2.destroyAllWindows()