반응형
영상 파일을 이미지 파일로 변경한 것을 다시 영상 파일로 바꿀 필요가 있어서 코드를 작성해보았습니다.
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() |
반응형
'OpenCV > OpenCV 강좌' 카테고리의 다른 글
두 점 사이의 각도를 구하는 OpenCV Python 예제 코드 (1) | 2024.04.25 |
---|---|
OpenCV Python - 동영상 파일을 일련번호 붙은 이미지 파일로 저장하기 (0) | 2024.04.20 |
OpenCV Python 검은색 빈 공간 채우기 (0) | 2024.04.19 |
Matplotlib의 컬러맵을 화면에 출력하는 OpenCV 예제 (0) | 2024.04.18 |
OpenCV Python kmeans 예제 (0) | 2024.04.10 |