PythonでWebカメラで撮った画像を保存

これを使うと、製品出荷時のエビデンスとして画像記録を保存する場合などに使えそう。
以下でタイムスタンプ付きで画像を保存する。
import numpy as np
import cv2
import datetime

cap = cv2.VideoCapture(0)
print("width:" + str(cap.get(3)) + " height:" + str(cap.get(4)) )

# Capture frame-by-frame
ret, frame = cap.read()

## Our operations on the frame come here
#gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',gray)
cv2.imwrite("img" + datetime.datetime.now().strftime('%Y%m%d%H%M%S')+'.png',frame)    #画像を保存

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

コメント

このブログの人気の投稿

Python OpenCVとWebカメラでバーコードリーダー

OpenCV 画像の足し算

OpenCV3とPython3で形状のある物体の輪郭と方向を認識する(主成分分析:PCA、固有ベクトル)