# -*- coding: utf-8 -*-
"""
Created on 2019-04-22 22:32:32
@Author: ZHAO Lingfeng
@Version : 0.0.1
"""
import os
import sys

import PySimpleGUI as sg
from PIL import Image


def concatenate_image(image_list, output_name, progress_bar_callback=None, progress_text_callback=None):
    def update_text(text):
        if progress_text_callback:
            progress_text_callback(text)

    def update_progress_bar(i, all):
        if progress_bar_callback:
            progress_bar_callback(i, all)

    images = []
    image_names = []
    for i in image_list:
        try:
            image = Image.open(i)

        except Exception as e:
            update_text(f"Failed on reading {i}: {e}")
        else:
            images.append(image)
            image_names.append(os.path.basename(i))
            update_text(f"Open image {i}")

    image_number = len(images)
    if image_number == 0:
        update_text("No images")
        return

    width = min([i.size[0] for i in images])
    length = 0
    # update_text("Stitching")
    for i in images:
        length += int(width / i.size[0] * i.size[1])

    result = Image.new('RGB', (width, length))

    length_i = 0
    for index, i in enumerate(images):
        update_text(f"Stithing {image_names[index]}")
        update_progress_bar(index + 1, image_number)
        w, h = i.size
        h_ = int(width / w * h)
        result.paste(i.resize((width, h_)), (0, length_i))
        length_i += h_

    update_text('Saving')
    result.save(output_name)


def main():
    layout = [[sg.Text('Select photos to concatenate')],
              [
                  sg.Text('Source images'),
                  sg.Input(),
                  sg.FilesBrowse(file_types=(("Image Files", "*.jpg;*.png;*.jpeg;*.bmp;*.tiff"), ))
              ], [sg.Text('Output image'), sg.Input('result.jpg', )],
              [
                  sg.ProgressBar(1000, orientation='h', size=(40, 20), key='progbar'),
                  sg.Text('', key='progtext', size=(20, 1))
              ], [
                  sg.Submit('Start'),
                  sg.Cancel(),
              ]]

    window = sg.Window('Image concatenate').Layout(layout)

    while True:
        _, files = window.Read()
        if _ == 'Cancel' or _ is None:
            window.Close()
            sys.exit(0)

        if not files[0]:
            sg.Popup("Please select image")
            continue

        source_images = files[0].split(';')
        output_image = files[1]
        print(source_images, output_image)
        concatenate_image(source_images, output_image,
                          window.FindElement('progbar').UpdateBar,
                          window.FindElement('progtext').Update)
        sg.Popup('Done')

    window.Close()


if __name__ == "__main__":
    main()
@@ -129,57 +129,402 @@
 ys%0D%0A
-%0D%0Aimport PySimpleGUI as sg%0D%0Afrom PIL import Image
+import logging%0D%0A%0D%0Aif os.environ.get('debug'):%0D%0A    logging.basicConfig(filename='debug.log', filemode='a', format='%25(asctime)s - %25(levelname)s - %25(message)s',level=logging.DEBUG)%0D%0A%0D%0Adef log(text):%0D%0A    logging.info(text)%0D%0A%0D%0A%0D%0Aimport PySimpleGUI as sg%0D%0Afrom PIL import Image%0D%0A%0D%0Atry:%0D%0A    import distutils%0D%0A    log(%22Import distutils success%22)%0D%0Aexcept Exception as e:%0D%0A    log(f%22Import error %7Be%7D%22)
 %0D%0A%0D%0A
@@ -1057,24 +1057,72 @@
 %7Bi%7D: %7Be%7D%22)%0D%0A
+            log(f%22Failed on reading %7Bi%7D: %7Be%7D%22)%0D%0A
         else
@@ -1254,16 +1254,52 @@
 e %7Bi%7D%22)%0D
+%0A            log(f%22Open image %7Bi%7D%22)%0D
 %0A%0D%0A    i
@@ -1958,24 +1958,48 @@
 ('Saving')%0D%0A
+    log(%22Stitch done%22)%0D%0A
     result.s
@@ -2029,24 +2029,51 @@
 ef main():%0D%0A
+    log(%22program starts%22)%0D%0A
     layout =
@@ -2809,24 +2809,51 @@
 _ is None:%0D%0A
+            log(%22Cancel%22)%0D%0A
             
@@ -2921,16 +2921,44 @@
 es%5B0%5D:%0D%0A
+            log(%22No file%22)%0D%0A
         
@@ -3096,16 +3096,111 @@
 les%5B1%5D%0D%0A
+        log(f%22Source images: %7Bsource_images%7D%22)%0D%0A        log(f%22Output images: %7Boutput_image%7D%22)%0D%0A
         

Add logging function