-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompressor.py
65 lines (52 loc) · 2.27 KB
/
Compressor.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
##old version
##from PIL import Image
##import os
##from tqdm import tqdm # python3 -m pip install tqdm
##downloadsFolder = "/Users/Gdi87/Downloads/"
##if __name__ == "__main__":
##image_extensions = [".jpg", ".jpeg", ".png"]
##images_to_compress = []
## images list
##for filename in os.listdir(downloadsFolder):
##name, extension = os.path.splitext(downloadsFolder + filename)
## if extension in image_extensions:
## images_to_compress.append(filename)
## compress and progress bar
## for filename in tqdm(images_to_compress, desc="Compressing images"):
## picture = Image.open(downloadsFolder + filename)
## picture.save(downloadsFolder + "compressed_" + filename, optimize=True, quality=60)
from PIL import Image
import os
from tqdm import tqdm
downloadsFolder = "/Users/Gdi87/Downloads/"
def get_output_directory():
while True:
output_dir = input("Enter the output directory path for the compressed images: ")
if os.path.isdir(output_dir):
return output_dir
else:
print("Invalid directory!")
def get_compression_quality():
while True:
quality = input("Enter the compression quality percentage (0-100): ")
if quality.isdigit() and 0 <= int(quality) <= 100:
return int(quality)
else:
print("Invalid input! Please enter a number between 0 and 100.")
if __name__ == "__main__":
outputFolder = get_output_directory()
compression_quality = get_compression_quality()
image_extensions = [".jpg", ".jpeg", ".png"]
images_to_compress = []
for filename in os.listdir(downloadsFolder):
name, extension = os.path.splitext(downloadsFolder + filename)
if extension in image_extensions:
images_to_compress.append(filename)
for filename in tqdm(images_to_compress, desc="Compressing images"):
try:
picture = Image.open(os.path.join(downloadsFolder, filename))
output_path = os.path.join(outputFolder, "compressed_" + filename)
picture.save(output_path, optimize=True, quality=compression_quality)
except Exception as e:
print(f"Error compressing image {filename}: {str(e)}")
print("Image compression completed.")