상세 컨텐츠

본문 제목

이미지 슬라이드 쇼 실습

영상처리/OpenCV 실습

by cepiloth 2021. 5. 12. 11:32

본문

728x90
반응형

특정 폴더에 있는 모든 이미지 파일을 이용하여 슬라이드쇼를 수행

구현할 기능

특정 폴더에 있는 이미지 파일 목록 읽기

#os.listdir() 메소드 활용

import os

file_list = os.listdir('.\\images')
img_files = [file for file in file_list if file.endswitch('.jpg')]
#glob.glob() 메소드 활용

import glob
img_files = glob.glob('.\\images\\*.jpg')

 

이미지를 전체 화면으로 출력하기

먼저 cv2.WINDOW_NORMAL 속성의 창을 만든 후, cv2.setWindowProperty() 함수를 사용하여 전체 화면 속성으로 변경

cv2.namedWindw('image', cv2.WINODW_NORMAL)
cv2.setWindowProperty('image', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

 

전체코드

일정 시간동안 이미지를 화면에 출력하고, 다음 이미지로 교체하기 (무한 루프)

import sys
import glob
import cv2


# 이미지 파일을 모두 img_files 리스트에 추가
img_files = glob.glob('.\\images\\*.jpg')

if not img_files:
    print("There are no jpg files in 'images' folder")
    sys.exit()

# 전체 화면으로 'image' 창 생성
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.setWindowProperty('image', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

# 무한 루프
cnt = len(img_files)
idx = 0

while True:
    img = cv2.imread(img_files[idx])

    if img is None:
        print('Image load failed!')
        break

    cv2.imshow('image', img)
    if cv2.waitKey(1000) >= 0:
        break

    idx += 1
    if idx >= cnt:
        idx = 0

cv2.destroyAllWindows()

 

예제 이미지로 만든 이미지 슬라이드쇼

 

728x90
반응형

관련글 더보기

댓글 영역