-
Notifications
You must be signed in to change notification settings - Fork 0
/
join.txt
executable file
·211 lines (147 loc) · 6.49 KB
/
join.txt
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 24 13:34:08 2023
@author: adam178
"""
from PIL import Image
import os
from time import sleep
from tkinter import filedialog
class Join:
def __init__(self):
self.dir = 'joined_images'
self.folder_path = ''
self.name_count = 1
self.extension = 0
def select_folder(self):
self.folder_path = filedialog.askdirectory(initialdir = '.', title = 'Select a folder.')
return self.folder_path
def create_dir(self):
dir = self.dir
try:
os.mkdir(self.dir)
except OSError:
print("The directory already existed $s " % dir)
else:
print("Successfully created the directory %s " % dir)
def rename(self, old_name):
extension = '_' + str(self.extension) + '.jpg'
new_name = os.path.join(str(self.dir), str(self.name_count) + str(extension))
os.rename(old_name, new_name)
def join_2(self, image1, image2):
im1 = Image.open(image1)
im2 = Image.open(image2)
new_im = Image.new('RGB', (im1.width, im1.height + im2.height))
new_im.paste(im1, (0, 0))
new_im.paste(im2, (0, im1.height))
pre_name = 'asdfasdf.jpg'
new_im.save(pre_name)
return pre_name
def join_3(self, image1, image2, image3):
im1 = Image.open(image1)
im2 = Image.open(image2)
im3 = Image.open(image3)
new_im = Image.new('RGB', (im1.width, im1.height + im2.height + im3.height))
new_im.paste(im1, (0, 0))
new_im.paste(im2, (0, im1.height))
new_im.paste(im3, (0, im1.height + im2.height))
pre_name = "asdfasdf.jpg"
new_im.save(pre_name)
return pre_name
def join_images(self):
images = os.listdir(self.folder_path)
images = [i for i in images if i.endswith('.jpg')]
images.sort()
#၂ နဲ့ ၃ နဲ့စားပြီးအကြွင်းကိုယူ
check_factor_of2 = len(images) % 2
check_factor_of3 = len(images) % 3
# files အရေအတွက်ကိုပြပြီးတော့ မေးမယ်။
number_pictures = len(images)
print(f"\nThe number of pictures are {number_pictures}")
method = input('\nDo you want to join by 2 or 3?(2/3) - pressing other keys will end the program: ')
# method 2 and variables.
if method == '2' and check_factor_of2 == 0:
for i in range(0, len(images), 2):
image1 = self.folder_path + '/' + images[i]
image2 = self.folder_path + '/' + images[i+1]
old_name = self.join_2(image1, image2)
os.remove(image1)
os.remove(image2)
self.rename(old_name)
self.name_count += 1
elif method == '2' and check_factor_of2 == 1:
theone = images.pop()
theone = self.folder_path + '/' + theone
for i in range(0, len(images), 2):
image1 = self.folder_path + '/' + images[i]
image2 = self.folder_path + '/' + images[i+1]
old_name = self.join_2(image1, image2)
os.remove(image1)
os.remove(image2)
self.rename(old_name)
self.name_count += 1
self.rename(theone)
# method 3 and variables.
elif method == '3' and check_factor_of3 == 0:
for i in range(0, len(images), 3):
image1 = self.folder_path + '/' + images[i]
image2 = self.folder_path + '/' + images[i+1]
image3 = self.folder_path + '/' + images[i+2]
old_name = self.join_3(image1, image2,image3)
os.remove(image1)
os.remove(image2)
os.remove(image3)
self.rename(old_name)
self.name_count += 1
elif method == '3' and check_factor_of3 == 1:
theone = images.pop()
theone = self.folder_path + '/' + theone
for i in range(0, len(images), 3):
image1 = self.folder_path + '/' + images[i]
image2 = self.folder_path + '/' + images[i+1]
image3 = self.folder_path + '/' + images[i+2]
old_name = self.join_3(image1, image2,image3)
os.remove(image1)
os.remove(image2)
os.remove(image3)
self.rename(old_name)
self.name_count += 1
self.rename(theone)
elif method == '3' and check_factor_of3 == 2:
second = images.pop()
first = images.pop()
for i in range(0, len(images), 3):
image1 = self.folder_path + '/' + images[i]
image2 = self.folder_path + '/' + images[i+1]
image3 = self.folder_path + '/' + images[i+2]
old_name = self.join_3(image1, image2,image3)
os.remove(image1)
os.remove(image2)
os.remove(image3)
self.rename(old_name)
self.name_count += 1
old_name = self.join_2(first, second)
self.rename(old_name)
# တစ်ခြားဟာနှိပ်ရင် else ထွက်မယ်။
else:
print('\nYou ended the program.')
def join_process():
jn = Join()
jn.extension = extension
print("\nThis program will only work for jpg images, so please format the pictures before using it.")
sleep(1)
folder_path = jn.select_folder()
new_dir = input('\nDo you want to save the pictures to a "joined images" folder or replace the original folder?(new/replace):')
if new_dir == 'new':
jn.create_dir()
jn.join_images()
elif new_dir == 'replace':
jn.dir = str(folder_path)
jn.join_images()
extension = 0
run_again = ''
while run_again == '' or run_again == 'y' or run_again == 'Y':
if __name__ == "__main__":
while True:
join_process()