-
Notifications
You must be signed in to change notification settings - Fork 3
/
ImageConcatenate.pyw
129 lines (105 loc) · 3.6 KB
/
ImageConcatenate.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# -*- coding: utf-8 -*-
"""
Created on 2019-04-22 22:32:32
@Author: ZHAO Lingfeng
@Version : 0.0.1
"""
import os
import sys
import logging
if os.environ.get('debug'):
logging.basicConfig(filename='debug.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s',level=logging.DEBUG)
def log(text):
logging.info(text)
import PySimpleGUI as sg
from PIL import Image
try:
import distutils
log("Import distutils success")
except Exception as e:
log(f"Import error {e}")
try:
wd = sys._MEIPASS
except AttributeError:
wd = os.getcwd()
icon_file = os.path.join(wd,'resource/icon.ico')
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}")
log(f"Failed on reading {i}: {e}")
else:
images.append(image)
image_names.append(os.path.basename(i))
update_text(f"Open image {i}")
log(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')
log("Stitch done")
result.save(output_name)
update_text('Done')
def main():
log("program starts")
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',icon=icon_file).Layout(layout)
while True:
_, files = window.Read()
if _ == 'Cancel' or _ is None:
log("Cancel")
window.Close()
sys.exit(0)
if not files[0]:
log("No file")
sg.Popup("Please select image")
continue
source_images = files[0].split(';')
output_image = files[1]
log(f"Source images: {source_images}")
log(f"Output images: {output_image}")
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()