# four.4.py
# Addendum to image viewer to view jpg files

import PySimpleGUI as sg
import os.path
import io             # <----
from PIL import Image # <----
    # must install Pillow package

# changing to a random theme
# see cookbook at pysimplegui.org
sg.theme ("LightGreen10")

# define output for first and third columns
firstCol = [ [sg.Text("Image Folder"), 
              sg.In (size=(25,1), enable_events=True, key="-FOLDER-"), 
              sg.FolderBrowse()], 
             [sg.Listbox(values=[], enable_events=True, size=(40,10), key="-FILE LIST-")],
             [sg.Button ("OK"), sg.Button ("Close")] ]

thirdCol = [ [sg.Text("Choose image from list at left")],
             [sg.Text(size=(50, 1), key="-FILENAME-")],
             [sg.Image(key="-IMAGE-")] ]

# define the layout
layout = [ [sg.Column(firstCol), sg.VSeperator(), sg.Column(thirdCol)] ]
          
# display the window
window = sg.Window ("Image Viewer", layout, margins=(150, 50))

while True:
    event, values = window.read()

    if event == "Close" or event == sg.WIN_CLOSED:
        break

    # User chose a folder after clicking 'Browse' button
    if event == "-FOLDER-":     # uses identifer from first column
        # get actual folder that was chosen
        folder = values["-FOLDER-"]
        try:
            # get list of files in chosen folder
            # this needs: import os.path
            fileList = os.listdir (folder)
            # debugging: print (fileList)
        except:
            # no files found!
            fileList = []   
            # debugging: print (fileList)

        # create a list of filenames that are in the chosen folder
        # and that end with .png or .gif (supported)
        # (jpg not supported)
        fileNames = [
            f
            for f in fileList
            if os.path.isfile (os.path.join (folder, f))
            and f.lower().endswith ((".jpg"))
        ]
        # debugging: print (fileNames)
        # add the filenames to the image file list in first column
        window["-FILE LIST-"].update (fileNames)

    # User chose a file to display from the file list
    if event == "-FILE LIST-":
        try:
            # create full path name of file
            # FILE-LIST is an array of file names; 
            # we want the first (and only) one
            # debugging: print (values)
            fileName = os.path.join(values["-FOLDER-"], values["-FILE LIST-"][0])
            
            # display filename in appropriate spot in right column
            window["-FILENAME-"].update(fileName)

            # convert to png
            pilImage = Image.open (fileName)
            pilImage.save ("tempName.png")

            # read new file
            pngFileName = os.path.join (values["-FOLDER-"], "tempName.png")

            # magic: display the image file!!
            # note this will scale the entire window if the image doesn't fit
            window["-IMAGE-"].update(filename = pngFileName)
        except:
            # don't display anything but keep going
            pass

window.close()


