-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_resizer.py
84 lines (66 loc) · 2.6 KB
/
magic_resizer.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
from PIL import Image
import os
def get_file_size_inMB(file):
return os.stat(file).st_size / 1048576
def get_folder_size(root):
size = 0
for path, dirs, files in os.walk(root):
for f in files:
size += os.path.getsize(os.path.join(path, f)) / 1048576
return round(size, 2)
path = ""
path = input("Specify the root dir\nLike C:/dir/pics/\nRoot dir: ")
custom_quality = int(input("Quality: "))
custom_quality_png = int(input("Quality(PNG): "))
min_file_size = float(input("Min file size to convert(in MB): "))
max_file_size = float(input("Max file size to convert(in MB): "))
original_folder_size = get_folder_size(path)
def list_all_files():
filepaths = []
for subdir, dirs, files in os.walk(path):
for file in files:
file_size = get_file_size_inMB(subdir + '/' + file)
if min_file_size < file_size < max_file_size:
filepaths.append(subdir + '/' + file)
print("Files found: " + str(len(filepaths)))
return filepaths
files_fullpath = list_all_files()
def resize():
percent = 0
i = 0
for file in files_fullpath:
if percent + 1 < round(100 * i / len(files_fullpath), 2):
percent = round(100 * i / len(files_fullpath), 2)
print(str(percent) + " % done")
if os.path.isfile(file) and file.split('.')[-1].lower() == 'jpg':
try:
im = Image.open(file)
f, e = os.path.splitext(file)
im.save(f + '.jpg', 'JPEG', quality=custom_quality)
except:
print("Problem with image: " + str(file))
i = i + 1
if os.path.isfile(file) and file.split('.')[-1].lower() == 'bmp':
try:
im = Image.open(file)
f, e = os.path.splitext(file)
im.save(f + '.jpg', 'JPEG', quality=custom_quality)
im.close()
os.remove(file)
except:
print("Problem with image: " + str(file))
i = i + 1
if os.path.isfile(file) and file.split('.')[-1].lower() == 'png':
try:
im = Image.open(file).convert('RGB')
f, e = os.path.splitext(file)
im.save(f + '.jpg', 'JPEG', quality=custom_quality_png)
im.close()
os.remove(file)
except:
print("Problem with image: " + str(file))
i = i + 1
resize()
resized_folder_size = get_folder_size(path)
print("Original total size: " + str(original_folder_size) + " MB")
print("Resized total size: " + str(resized_folder_size) + " MB")