Language - Python(Opencv)

Python Opencv - #2 Image Slide Show

KimTory 2021. 11. 21. 13:34

▶ Slide Show 구현은 폴더 내 다수의 Image 저장 후 while문 돌면서 1개씩 Image Show 해주는 방식

 

 

import sys
import glob
import cv2

# 이미지 파일을 모두 img_files 리스트에 추가
# 특정 패턴의 문자열에 있는 파일들을 모두 불러옴 (.jpg)
img_files = glob.glob('.\\images\\*.jpg')

# os.listdir 이용 시,,,
file_list = os.listdir('.\\images')
# jpg로 되어 있는 파일 모두를 불러옴
img_files = [file for file in file_list if file.endwith('.jpg')] 
#

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

# WINDOW_NORMAL, 전체 화면으로 '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 FAIL" )
        break

    cv2.imshow("image",img)
    # 1초마다 갱신
    if cv2.waitKey(1000) >= 0:
        break
        
    # total image slide show 종료 시, 다시 처음으로...
    idx += 1
    if idx >= cnt: # cnt는 폴더 내, 이미지 총 개수
        idx = 0

cv2.destroyAllWindows()