使用 python 将一系列 ppm 图像转换为视频
Converting a sequence of ppm images to video with python
我正在尝试使用 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')
成功了!
获取空视频文件的原因有多种,但路径看起来是正确的。
在Windows系统中C:/Users/Joseph\image0.ppm
和C:/Users/Joseph/image0.ppm
是一样的。
- 手动删除
video1.avi
个文件,只是为了确保文件没有被锁定。
我认为问题涉及 video codec,但我不能确定。
在命令video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height))
中,第二个参数是FOURCC代码,select是视频编码器的视频编解码器。
将值设置为 -1
时,将打开一个对话框,让您 select 编解码器。
在旧版本的 OpenCV 中,它并不总是有效。
尝试将 FOURCC 设置为 'DIB '
,应用 "Basic Windows bitmap format"。
使用它来创建原始(未压缩的)AVI 视频文件。
代码如下:
import cv2
import glob
img1 = cv2.imread('C:/Users/Joseph/image0.ppm')
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('*.ppm')
try:
for filename in filenames:
print(filename)
img = cv2.imread(filename)
# 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.imshow('img', img)
来帮助您调试问题,以防它不是编解码器问题。
- 确保您没有收到任何异常。
如果我的回答解决了您的问题,请拒绝。
我正在尝试使用 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')
成功了!
获取空视频文件的原因有多种,但路径看起来是正确的。
在Windows系统中C:/Users/Joseph\image0.ppm
和C:/Users/Joseph/image0.ppm
是一样的。
- 手动删除
video1.avi
个文件,只是为了确保文件没有被锁定。
我认为问题涉及 video codec,但我不能确定。
在命令video1 = cv2.VideoWriter('video1.avi', -1, 1, (width, height))
中,第二个参数是FOURCC代码,select是视频编码器的视频编解码器。
将值设置为 -1
时,将打开一个对话框,让您 select 编解码器。
在旧版本的 OpenCV 中,它并不总是有效。
尝试将 FOURCC 设置为 'DIB '
,应用 "Basic Windows bitmap format"。
使用它来创建原始(未压缩的)AVI 视频文件。
代码如下:
import cv2
import glob
img1 = cv2.imread('C:/Users/Joseph/image0.ppm')
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('*.ppm')
try:
for filename in filenames:
print(filename)
img = cv2.imread(filename)
# 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.imshow('img', img)
来帮助您调试问题,以防它不是编解码器问题。 - 确保您没有收到任何异常。
如果我的回答解决了您的问题,请拒绝。