-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress-files.py
54 lines (40 loc) · 1.51 KB
/
compress-files.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
from PIL import Image
import os
from lib.inputs import yesNo
from lib.files import traverse
# Absolute path to the directory you want to search
ROOT = r"C:\Users\[Users]\[pythonFiles]"
PREFIX = ''
SUFFIX = ''
def compressFiles(path):
assert os.path.isdir(path)
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) if str(f).startswith(PREFIX) and str(f).endswith(SUFFIX)]
print("---FILES---")
for file in files:
print(file)
if yesNo("Compress files (y/n): "):
count = 0
for file in files:
try:
maxWidth = 1920
filePath = os.path.join(path, file)
originalSize = os.path.getsize(filePath)
image = Image.open(filePath)
width, height = image.size
aspectRatio = width / height
newHeight = maxWidth / aspectRatio
image = image.resize((maxWidth, round(newHeight)))
image.save(filePath, optimize=True, quality=85)
finalSize = os.path.getsize(filePath)
print(f"Removed {round((originalSize-finalSize)/1024/1024, 3)}MB")
count += 1
except Exception as e:
print(e)
print(f"Compressed {count} files")
input("Press ENTER to exit")
def main():
assert os.path.isdir(ROOT)
sourceDir = traverse(ROOT, "Choose source directory (press ENTER to choose CWD): ")
compressFiles(sourceDir)
if __name__ == '__main__':
main()