gpt4 book ai didi

python - 如何在 Python 中保存 OpenCV 由图片制作的电影

转载 作者:太空宇宙 更新时间:2023-11-03 21:41:15 25 4
gpt4 key购买 nike

我用 OpenCV 编写了一个 Python 程序:

  1. 扫描文件夹中的图片
  2. 根据他们的订单对他们进行一些操作
  3. 返回已处理图片的IMG列表

现在我想做的是将新图片列表IMG保存为例如.avi 电影。如何做这样的操作?我有意想在过滤等整个过程后保存列表中的图片。

import os
import cv2
import numpy as np

folder_name = 'trial1'
extension = '.bmp'
path = os.getcwd()

GB_kernel = (13,13)

""" List all .bmp images names from a folder """
def folder_content(folder_name, extension):

dir_content = os.listdir(folder_name)
folder_content_ext = []
for file in dir_content:
if file.endswith(extension):
folder_content_ext.append(file)

return folder_content_ext

IMG_names = folder_content(folder_name, extension)

""" Some OpenCV operations on images """
""" Loop over all images. Iterator i is important in the future """
IMGs = []
for i in range(len(IMG_names)):

""" Define path to basic image """
img = path + '\\' + folder_name + '\\' + IMG_names[i]
""" read this image """
image = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
""" some operation - gaussian blur """
pre_filt = cv2.GaussianBlur(image, GB_kernel, 0)
""" contain blurred img in a list """
IMGs.append(pre_filt)

cv2.imshow('Gaussian Blur', pre_filt)
WKey = 1
if cv2.waitKey(WKey) & 0xFF == ord('q'):
break

cv2.destroyAllWindows()

""" HOW?? """
save_list_of_matrix_as_movie(IMGs)

感谢您的帮助和提示。

最佳答案

您可以尝试下面的代码。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""

Create video from images in a list.

Idea from:
http://tsaith.github.io/combine-images-into-a-video-with-python-3-and-opencv-3.html

"""

import os
import time
import cv2

folder_name = 'trial1'
extension = '.bmp'
video_file = 'out.avi'
path = os.getcwd()

GB_kernel = (13, 13)


# %%
def folder_content():
"""
List all images names with given extension from a folder.
"""
dir_content = os.listdir(folder_name)
folder_content_ext = []
for file in dir_content:
if file.endswith(extension):
folder_content_ext.append(file)

return folder_content_ext


# %%
def img_2_matrix(img_names):
"""
Some OpenCV operations on images
Loop over all images. Iterator i is important in the future
"""
imgs = []
for i in range(len(img_names)):

# Define path to basic image
img = os.path.join(path, folder_name, img_names[i])
# read this image
image = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
# some operation - gaussian blur
pre_filt = cv2.GaussianBlur(image, GB_kernel, 0)
# contain blurred img in a list
imgs.append(pre_filt)

cv2.imshow('Gaussian Blur', pre_filt)
wkey = 1
if cv2.waitKey(wkey) & 0xFF == ord('q'):
break

return imgs


# %%
def save_list_of_matrix_as_movie(imgs):
"""

"""
shape = (imgs[0].shape[1], imgs[0].shape[0])
fourcc = cv2.VideoWriter_fourcc(*"XVID") # XVID for avi, mp4v for mp4
out = cv2.VideoWriter(video_file, fourcc, 20.0, shape, 0)

print("\nHit 'q' to exit")
for frame in imgs:
pre_filt = cv2.GaussianBlur(frame, GB_kernel, 0)
out.write(pre_filt) # Write out frame to video
cv2.imshow("video", pre_filt)
if(cv2.waitKey(1) & 0xFF) == ord("q"): # Hit `q` to exit
break

return out


# %%
def release(out):
"""
Release everything if job is finished
"""
cv2.destroyAllWindows()
out.release()


# %%
if __name__ == "__main__":

# %%
TIC = time.time()

# %%
img_names = folder_content()
imgs = img_2_matrix(img_names)
out = save_list_of_matrix_as_movie(imgs)
release(out)

# %%
print("Time elapsed: %0.2f s" % (time.time() - TIC))

关于python - 如何在 Python 中保存 OpenCV 由图片制作的电影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52024946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com