【发布时间】:2020-05-22 18:22:31
【问题描述】:
我正在尝试使用 IPython (python 2.7) 从 ppm 图像制作视频。
我写了这段代码:
import cv2
import glob
img1 = cv2.imread('C:/Users/Joseph/image0.ppm')
height, width, layers = img1.shape
video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height))
filenames = glob.glob('C:/Users/Joseph/*.ppm')
for filename in filenames:
print(filename)
img = cv2.imread(filename)
video1.write(img)
cv2.destroyAllWindows()
video1.release()
视频已创建但为空size=0B,无法打开。
没有错误信息。
我怀疑问题出在位置的书写上,因为print(filename) 产生:
C:/Users/Joseph\image0.ppm
C:/Users/Joseph\image1.ppm
C:/Users/Joseph\image2.ppm
C:/Users/Joseph\image2.ppm
而不是我的预期:C:/Users/Joseph/image0.ppm
你能帮帮我吗?
编辑:文件类型为type: GIMP 2.10.14 (.ppm)。问题是否与这种类型的 ppm 有关?
编辑 2: 问题似乎与.ppm 没有直接关系。
确实,我试过了(考虑到Rotem的回答):
import cv2
import glob
i = cv2.imread('C:/Users/Joseph/image0.ppm')
cv2.imwrite('C:/Users/Joseph/image.jpg',i)
img1 = cv2.imread('C:/Users/Joseph/image.jpg')
height, width, layers = img1.shape
# Set FOURCC code to '24BG' - '24BG' is used for creating uncompressed raw video
video1 = cv2.VideoWriter('video1.avi', cv2.VideoWriter_fourcc('D','I','B',' '), 1, (width, height))
filenames = glob.glob('C:/Users/Joseph/*.ppm')
try:
for filename in filenames:
print(filename)
img = cv2.imread(filename)
cv2.imwrite('C:/Users/Joseph/a.jpg',img)
img=cv2.imread('C:/Users/Joseph/a.jpg')
# Display input image for debugging
cv2.imshow('img', img)
cv2.waitKey(1000)
video1.write(img)
except:
print('An error occurred.')
cv2.destroyAllWindows()
video1.release()
而且它也不起作用。而且我没有显示任何图像。
所以这似乎是我的 cv2 视频错误。 jpg 制作的很好。
编辑:解决方案。
本着 rotem 的回答精神,我尝试了: cv2.VideoWriter_fourcc('M','J','P','G') 并且成功了!
【问题讨论】:
标签: python-2.7 opencv video gimp ppm