#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
###############################################################################
#
#  Name:         opencv_read_show_folder.py
#  Purpose:      Program to read and show images from a folder
#  Author:       weigu.lu
#  Date:         2020-11-22
#
#  Copyright 2020 weigu <weigu@weigu.lu>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
#  More infos on weigu.lu/other_projects
#
###############################################################################
"""

# pylint: disable-msg=C0103
# pylint: disable-msg=R0912
# pylint: disable-msg=R0914
# pylint: disable-msg=R0915
# pylint: disable-msg=W0105

import os
import glob
import cv2 as cv # to run code even if version changes
#import numpy as np
#import pandas as pd

DIR_NAME_IMAGES = '/savit/programming/python/opencv/images'

def show_image(pimg, show_flag_time):
    ''' Show image during x ms if flag is set. Parameter show_flag_time is a
        tuple e.g (1,2000) to show picture for 2s or (0,2000) to prevent the
        show. (1,0) waits on keypress '''
    if show_flag_time[0] == 1:
        cv.imshow('image', pimg)      # cv.imshow(window_name, image)
        cv.waitKey(show_flag_time[1]) # show picture for x ms (x=0 for keypress)
        cv.destroyAllWindows()

# flags and times in ms to show images
flag = {'1':(1, 1500), '2':(0, 1500), '3':(1, 0)}

os.chdir(DIR_NAME_IMAGES)                # change directory
img_list = glob.glob('*.jpg')            # get list with jpg images
img_list.extend(glob.glob('*.png'))      # and png images
img_list.sort()                          # sort the list
print(img_list)
if img_list == []:
    print("error: no images!")

i = 0
for img_name in img_list:
    i += 1
    img = cv.imread(img_name)            # read the image
    show_image(img, flag[str(i)])
    