Mercurial > hg > pub > prymula > scripts
comparison verysimpleslideshow/verysimpleslideshow.py @ 0:dcd610585610
INIT
author | prymula <prymula76@outlook.com> |
---|---|
date | Thu, 21 Sep 2023 22:32:14 +0200 |
parents | |
children | 82b75cc1e524 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:dcd610585610 |
---|---|
1 #!/usr/bin/env python3 | |
2 # Very Simple Slide Show | |
3 # autor: Prymula (PRP) | |
4 # licencja: Public Domain | |
5 # data 02-09-2023 | |
6 # edytor: Geany | |
7 | |
8 import os, sys | |
9 import tempfile | |
10 import shutil | |
11 from sys import platform | |
12 from PIL import Image | |
13 from itertools import filterfalse | |
14 | |
15 VER = "0.230913-0" | |
16 | |
17 if platform == "linux" or platform == "linux2": | |
18 SLA = '/' | |
19 elif platform == "darwin": | |
20 SLA = '/' | |
21 elif platform == "win32": | |
22 SLA = '\\' | |
23 | |
24 #count = 0 | |
25 tmp_path = tempfile.mkdtemp() | |
26 #mp_path = dir_path = os.getcwd() | |
27 #list_path = dir_path = os.getcwd() | |
28 tmp_list = tmp_path + SLA + "list.txt" | |
29 # NIE USUWAC ! | |
30 #command = "ffmpeg -framerate 0.3 -pattern_type glob -i '"+tmp_path+"/*.jpg' -c:v libopenh264 -profile:v high -crf 20 -pix_fmt yuv420p output.mp4" | |
31 command = "ffmpeg -f concat -y -r 0.2 -safe 0 -i '"+tmp_list+"' -framerate 1 -c:v libx264 -profile:v high -crf 20 -pix_fmt yuv420p -filter:a 'atempo=0.5' output.mp4" | |
32 #command = "ffmpeg -f concat -y -r 0.2 -safe 0 -i '"+tmp_list+"' -framerate 1 -c:v mpeg4 -crf 20 -pix_fmt yuv420p output.mp4" | |
33 | |
34 #num = 0 | |
35 sample = None | |
36 try: | |
37 sample = Image.Resampling.LANCZOS | |
38 print ("Uzywam LANCZOS") | |
39 except AttributeError: | |
40 sample = Image.ANTIALIAS | |
41 print ("Uzywam: ANTIALIAS") | |
42 | |
43 | |
44 class App(): | |
45 | |
46 BASE_APSC = 724 | |
47 LONG = 1080 | |
48 HALF = 540 | |
49 BASE_MICRO = 810 | |
50 BASE = None | |
51 #n_pictures = 0 | |
52 count = 0 | |
53 | |
54 def list_files(self, dir_path): | |
55 res = [] | |
56 try: | |
57 for file_path in os.listdir(dir_path): | |
58 if os.path.isfile(os.path.join(dir_path, file_path)): | |
59 res.append(os.path.join(dir_path, file_path)) | |
60 except FileNotFoundError: | |
61 print(f"The directory {dir_path} does not exist") | |
62 except PermissionError: | |
63 print(f"Permission denied to access the directory {dir_path}") | |
64 except OSError as e: | |
65 print(f"An OS error occurred: {e}") | |
66 return res | |
67 | |
68 def file_exclusion(self, files): | |
69 for f in files[:]: | |
70 if f.rfind('.jpg') == -1: | |
71 #print('usuwam: '+f) | |
72 files.remove(f) | |
73 return files | |
74 | |
75 def get_files(self): | |
76 dir_path = os.getcwd() | |
77 print ("GETCWD: "+dir_path) | |
78 files = self.list_files(dir_path) | |
79 return a.file_exclusion(files) | |
80 | |
81 def create_horizontal_surface(self): | |
82 return Image.new('RGB', (a.LONG, a.BASE)) | |
83 | |
84 def create_vertical_surface(self): | |
85 return Image.new('RGB', (a.BASE, a.LONG)) | |
86 | |
87 def put_horizontal_image(self, image): | |
88 global sample | |
89 new = Image.new("RGB", (a.LONG, a.BASE)) | |
90 if image.size[0] != a.LONG: | |
91 image = image.resize((a.LONG, a.BASE), sample, None, 3.0) | |
92 new.paste(image, (0,0), mask = image) | |
93 return new | |
94 | |
95 def put_vertical_image(self, image): | |
96 global sample | |
97 new = Image.new("RGB", (a.BASE, a.LONG)) | |
98 if image.size[0] != a.BASE: | |
99 image = image.resize((a.BASE, a.LONG), sample, None, 3.0) | |
100 new.paste(image, (0,0), mask = image) | |
101 return new | |
102 | |
103 # dodaje portrety do kraiobrazu | |
104 def join_image_to_horizontal(self, image, destinity): | |
105 global sample # n_pictures | |
106 single = False | |
107 print ("DLUGOSC: "+str(len(image))) | |
108 if len(image) == 1: # and n_pictures > 0: # 1 | |
109 print ("SINGIELEK") | |
110 #num += 1 # tymczasowy | |
111 tmp = image[-1]. resize((a.HALF, a.BASE), sample, None, 3.0) | |
112 destinity.paste(tmp, (300,0), mask = tmp) | |
113 image.pop() | |
114 single = True | |
115 elif len(image) > 0: | |
116 #num += 1 # tymczasowy | |
117 tmp = image[-1].resize((a.HALF, a.BASE), sample, None, 3.0) | |
118 destinity.paste(tmp, (0,0), mask = tmp) | |
119 image.pop() | |
120 if len(image) > 0: | |
121 #num += 1 # tymczasowy | |
122 tmp = image[-1]. resize((a.HALF, a.BASE), sample, None, 3.0) | |
123 destinity.paste(tmp, (a.HALF,0), mask = tmp) | |
124 image.pop() | |
125 | |
126 return destinity, len(image), single | |
127 | |
128 # dodaje kraiobrazy do portretu | |
129 def join_image_to_vertical(self, image, destinity): | |
130 global sample # n_pictures | |
131 single = False | |
132 print ("DLUGOSC: "+str(len(image))) | |
133 if len(image) == 1: # and a.n_pictures > 0: # 1 | |
134 print ("SINGIELEK") | |
135 #num += 1 # tymczasowy | |
136 tmp = image[-1]. resize((a.BASE, a.HALF), sample, None, 3.0) | |
137 destinity.paste(tmp, (0,300), mask = tmp) | |
138 image.pop() | |
139 single = True | |
140 #a.n_pictures -= 1 | |
141 elif len(image) > 0: | |
142 #num += 1 # tymczasowy | |
143 tmp = image[-1].resize((a.BASE, a.HALF), sample, None, 3.0) | |
144 destinity.paste(tmp, (0,0), mask = tmp) | |
145 image.pop() | |
146 #a.n_pictures -= 1 | |
147 if len(image) > 0: | |
148 #num += 1 # tymczasowy | |
149 tmp = image[-1]. resize((a.BASE, a.HALF), sample, None, 3.0) | |
150 destinity.paste(tmp, (0, a.HALF), mask = tmp) | |
151 image.pop() | |
152 #a.n_pictures -= 1 | |
153 | |
154 return destinity, len(image), single | |
155 | |
156 def read_images_from_directory(self, files): | |
157 #global n_pictures | |
158 images_vertical = [] | |
159 images_horizontal = [] | |
160 if len(files) == 0: | |
161 print ('Brak plików JPG !') | |
162 a.cleanup() | |
163 exit (1) | |
164 for f in files: | |
165 image = Image.open(f).convert('RGBA') | |
166 if image.size[1] > image.size[0]: | |
167 s = float(image.size[0] / image.size[1]) | |
168 if s > 0.6 and s < 0.69: | |
169 a.BASE = a.BASE_APSC | |
170 print ("obraz vertical APSC: "+f+" s:"+str(s)) | |
171 images_vertical.insert(-1, image) | |
172 #a.n_pictures += 1 | |
173 elif s > 0.7 and s < 0.79: | |
174 a.BASE = a.BASE_MICRO | |
175 print ("obraz vertical MICRO: "+f+" s:"+str(s)) | |
176 images_vertical.insert(-1, image) | |
177 #a.n_pictures += 1 | |
178 else: | |
179 print ("Ver - Niedozwolony rozmiar zdjęcia s: "+str(s) + " Foto: "+f) | |
180 | |
181 if image.size[0] > image.size[1]: | |
182 s = float(image.size[0] / image.size[1]) | |
183 if s > 1.4 and s <= 1.51: | |
184 a.BASE = a.BASE_APSC | |
185 print ("obraz horizontal APSC: "+f+" s:"+str(s)); | |
186 images_horizontal.insert(-1, image) | |
187 #a.n_pictures += 1 | |
188 elif s > 1.3 and s < 1.39: | |
189 a.BASE = a.BASE_MICRO | |
190 print ("obraz horizontal MICRO: "+f+" s:"+str(s)); | |
191 images_horizontal.insert(-1, image) | |
192 #a.n_pictures += 1 | |
193 else: | |
194 print ("Hor - Niedozwolony rozmiar zdjęcia s: "+str(s) + " Foto: "+f) | |
195 | |
196 | |
197 | |
198 #print ("N_PICTURES: "+str(a.n_pictures)) | |
199 return images_vertical, images_horizontal | |
200 | |
201 def cleanup(self): | |
202 # NIEBEZPIECZNA ! lepiej niczym innym nie nadpisywać tych zmiennych | |
203 shutil.rmtree(tmp_path) | |
204 | |
205 def save_vertical_to_horizontal(self, images_vertical, jpg_names): | |
206 #global count | |
207 for i in images_vertical[:]: | |
208 output, list_size, single = a.join_image_to_horizontal(images_vertical, a.create_horizontal_surface()) | |
209 if list_size != 0 or single == True: | |
210 #output.show() | |
211 output.save(str(tmp_path)+SLA+jpg_names[a.count], format='JPEG') | |
212 a.count += 1 | |
213 | |
214 def save_horizontal_to_vertical(self, images_horizontal, jpg_names): | |
215 #global count | |
216 for i in images_horizontal[:]: | |
217 output, list_size, single = a.join_image_to_vertical(images_horizontal, a.create_vertical_surface()) | |
218 if list_size != 0 or single == True: | |
219 #output.show() | |
220 output.save(str(tmp_path)+SLA+jpg_names[a.count], format='JPEG') | |
221 a.count += 1 | |
222 | |
223 def save_vertical(self, images_vertica, jpg_names): | |
224 #global count | |
225 for i in images_vertical[:]: | |
226 output = a.put_vertical_image(i) | |
227 output.save(str(tmp_path)+SLA+jpg_names[a.count], format='JPEG') | |
228 #output.show() | |
229 images_vertical.pop() | |
230 a.count += 1 | |
231 | |
232 def save_horizontal(self, images_horizontal, jpg_names): | |
233 #global count | |
234 for i in images_horizontal[:]: | |
235 output = a.put_horizontal_image(i) | |
236 output.save(str(tmp_path)+SLA+jpg_names[a.count], format='JPEG') | |
237 #output.show() | |
238 images_horizontal.pop() | |
239 a.count += 1 | |
240 | |
241 def name_jpg_wihout_path(self): | |
242 files_jpg_names = a.file_exclusion(os.listdir()) | |
243 for f in files_jpg_names: | |
244 f = f[0] + "file " | |
245 #print ("Ekstrat: " + str(files_jpg_names)) | |
246 return files_jpg_names | |
247 | |
248 def at_unslash(self, path): | |
249 p = path.replace("\\","/") | |
250 print("PO KONWERSJI: "+p) | |
251 return p | |
252 | |
253 def dump_jpg_list(self, files_jpg_names): | |
254 print("tmp_list") | |
255 with open(tmp_list, 'w') as fp: | |
256 for item in files_jpg_names: | |
257 # FIXME - to nic nie daje dalej wyrzuca "invalid argument" | |
258 if platform == "win32": | |
259 fp.write("file '"+a.at_unslash(str(tmp_path) + SLA +"%s'\r\n" % item)) | |
260 else: | |
261 # write each item on a new line | |
262 fp.write("file '"+str(tmp_path) + SLA +"%s'\r\n" % item) | |
263 | |
264 print("Zapisany plik do list.txt --> "+item) | |
265 return files_jpg_names | |
266 a = App() | |
267 jpg_names = a.dump_jpg_list(a.name_jpg_wihout_path()) | |
268 if len(sys.argv) == 1 or sys.argv[-1] == '-H': | |
269 images_vertical, images_horizontal = a.read_images_from_directory(a.get_files()) | |
270 a.save_horizontal(images_horizontal, jpg_names) | |
271 a.save_vertical_to_horizontal(images_vertical, jpg_names) | |
272 #print ("_---------------N_PIC: "+str(a.n_pictures)) | |
273 | |
274 print(command) | |
275 os.popen(command).read() | |
276 elif sys.argv[-1] == '-V': | |
277 images_vertical, images_horizontal = a.read_images_from_directory(a.get_files()) | |
278 a.save_vertical(images_vertical, jpg_names) | |
279 a.save_horizontal_to_vertical(images_horizontal, jpg_names) | |
280 #print ("_---------------N_PIC: "+str(a.n_pictures)) | |
281 print(command) | |
282 os.popen(command).read() | |
283 else: | |
284 print ("\n\nUżycie:\n\n Uruchom skrypt w katalogu z plikami JPG\n\n 'vsss -H' | 'vsss' dla video horyzontalnego\n 'vsss -V' dla video vertykalnego\n\n \ | |
285 'vsss -h' aby wyświetlić pomoc i numer wersji.\n verysimpleslideshow (vsss) ver. "+VER+"\n SEPT-2023") | |
286 | |
287 | |
288 a.cleanup() |