最近沉迷于Stable Diffusion,以前也没真正系统学过Python,不过现在有了Chatgpt问题迎刃而解感谢Ai带来的便利,有爱自取。
import CV2
from PIL import Image
import numpy as np
cap = CV2.VideoCapture("D:/00/热门音乐.mp4") # 获取视频对象
isOpened = cap.isOpened # 判断是否打开
# 视频信息获取
fps = cap.get(CV2.CAP_PROP_FPS)
imageNum = 0
sum=0
timef=30
while (isOpened):
sum+=1
(frameState, frame) = cap.read() # 记录每帧及获取状态
if frameState == True and (sum % timef==0):
# 格式转变,BGRtoRGB
frame = CV2.cvtColor(frame, CV2.COLOR_BGR2RGB)
# 转变成Image
frame = Image.fromarray(np.uint8(frame))
frame = np.array(frame)
# RGBtoBGR满足opencv显示格式
frame = CV2.cvtColor(frame, CV2.COLOR_RGB2BGR)
imageNum = imageNum + 1
fileName = 'D:/005/in/' + str(imageNum) + '.jpg' # 存储路径
CV2.imwrite(fileName, frame, [CV2.IMWRITE_JPEG_QUALITY, 100])
print(fileName + " successfully write in") # 输出存储状态
elif frameState == False:
break
print('finish!')
cap.release()