-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilenames AdvPNG png.py
87 lines (65 loc) · 2.55 KB
/
filenames AdvPNG png.py
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
#!/usr/bin/env python3
"""
Feeds selected PNG files to advpng.exe for recompression and reducing file size.
advpng.exe is available from https://github.com/amadvance/advancecomp
WARNING:
Source files are replaced! No backup, no renaming!
"""
__author__ = 'Ilya Razmanov'
__copyright__ = '(c) 2024 Ilya Razmanov'
__credits__ = 'Ilya Razmanov'
__license__ = 'unlicense'
__version__ = '2024.02.27'
__maintainer__ = 'Ilya Razmanov'
__email__ = 'ilyarazmanov@gmail.com'
__status__ = 'Production'
import os
import subprocess
from tkinter import Label, Tk, filedialog
# --------------------------------------------------------------
# Creating dialog
sortir = Tk()
sortir.title('Recompressing .PNG...')
sortir.geometry('+100+100')
zanyato = Label(sortir, wraplength=700, text='Starting...', font=('arial', 12), padx=16, pady=10, justify='center')
zanyato.pack()
sortir.withdraw()
# Main dialog created and hidden
# --------------------------------------------------------------
# Open source file list
sourcefilelist = filedialog.askopenfilenames(title='Select PNG files to recompress', filetypes=[('PNG files', '*.png')]) # filtering for PNG
if sourcefilelist == '':
quit()
# --------------------------------------------------------------
# Updating dialog
sortir.deiconify()
zanyato.config(text='Allons-y!')
sortir.update()
sortir.update_idletasks()
# Dialog shown and updated
# --------------------------------------------------------------
# --------------------------------------------------------------
# Creating subprocess startupinfo for hidden console.
#
startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#
# now .popen or .run may be used as below:
# subprocess.run(f'program.exe -switches "{filename}"', startupinfo=startupinfo)
# --------------------------------------------------------------
# Process file list string by string in cycle
for filename in sourcefilelist:
zanyato.config(text=' Processing ' + filename + '... ') # Updating label, showing processed file name
sortir.update()
sortir.update_idletasks()
subprocess.run(f'advpng.exe --recompress --shrink-insane --iter 50 --quiet "{filename}"', startupinfo=startupinfo)
# output in quotes for paths with spaces
# --shrink-insane --iter 50 means Zopfli compression with 50 iterations
# --------------------------------------------------------------
# Destroying dialog
sortir.destroy()
sortir.mainloop()
# Dialog destroyed and closed
# --------------------------------------------------------------