반응형

영상 파일을 이미지 파일로 변경한 것을 다시 영상 파일로 바꿀 필요가 있어서 코드를 작성해보았습니다. 



2023. 2. 14  최초작성

2024. 4. 20



동영상 파일을 일련 번호 붙인 이미지 파일로 저장하는 것은 다음 포스트를 참고하세요.

 

OpenCV Python - 동영상 파일을 일련번호 붙은 이미지 파일로 저장하기

https://webnautes.tistory.com/2323 




일련 번호 붙은 이미지 파일을 동영상 파일로 다시 바꾸어봅니다. 

 

import cv2
import os


def get_files(path):
    for root, subdirs, files in os.walk(path):
     
        list_files = []

        if len(files) > 0:
            for f in files:
                fullpath = root + '/' + f
                list_files.append(fullpath)

    return list_files


image_files = get_files('./img')

image_files.sort() # 정렬을 해줘야 합니다.


img = cv2.imread(image_files[0])
height,width,channel = img.shape # 이미지 크기를 가져옵니다.
fps = 25 # fps를 25로 합니다.

# 현재 소스코드가 있는 경로에 mp4 포맷으로 저장하도록 합니다.
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
writer = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))


for file in image_files:

    img = cv2.imread(file)

    writer.write(img)

    cv2.imshow("result", img)


    key = cv2.waitKey(int(1000/fps)) # fps를 사용하여 delay 설정

    if key == 27:     # ESC키 누르면 중지
        break

writer.release()
cv2.destroyAllWindows()



반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts