OpenCV-Python (2) 이미지 슬라이드쇼 프로그램

hjwon·2023년 9월 2일
0

OpenCV-Python

목록 보기
2/6

📍 이미지 슬라이드쇼

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


📍 구현할 기능

  • 특정 폴더에 있는 이미지 파일 목록 읽기
  • 이미지를 전체 화면으로 출력하기
  • 일정 시간 동안 이미지를 화면에 출력하고, 다음 이미지로 교체하기 (무한루프)


📍 특정 폴더에 있는 이미지 파일(*.jpg) 목록 읽기

  • os.listdir()
import os

file_list = os.listdir('.\\images')
img_files = [os.path.join('.\\images', file)
             for file in file_list if file.endswith('.jpg')]
  • glob.glob()
import glob

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



📍 전체 화면 영상 출력 창 만들기

  • 먼저 cv2.WINDOW_NORMAL 속성의 창을 만든 후, cv2.setWindowProperty() 함수를 사용하여 전체 화면 속성으로 변경
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



💻 이미지 슬라이드쇼 전체 코드

import os
import sys
import glob
import cv2

# 이미지 파일을 모두 img_files 리스트에 추가

# os.listdir() 사용 방법
#file_list = os.listdir('.\\images')
#img_files = [os.path.join('.\\images', file) for file in file_list if file.endswith('.jpg')]

# glob.glob() 사용 방법
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()



profile
열공워니

0개의 댓글