forked from DarkSouL11/ImageCompressor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
48 lines (41 loc) · 1.46 KB
/
utils.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
import os
def check(path1, path2, replace, is_folder):
if is_folder and os.path.isdir(path1):
if replace:
return True
elif os.path.isdir(path2):
return True
else:
return False
elif not is_folder and os.path.exists(path1):
if replace:
return True
elif os.path.isdir(path2):
return True
else:
return False
else:
return False
def folder_convert(compressor, input_path, replace=True, output_path=None):
for content in os.listdir(input_path):
if is_valid(content):
if replace:
compressor.compress(os.path.join(input_path, content))
else:
ip = os.path.join(input_path, content)
op = os.path.join(output_path, content)
compressor.compress(ip, replace=False, save_path=op)
def file_convert(compressor, input_path, replace=True, output_path=None):
if is_valid(input_path):
if replace:
compressor.compress(input_path)
else:
fname = input_path.rsplit('/', 1)[1]
op = os.path.join(output_path, fname)
compressor.compress(input_path, replace=False, save_path=op)
def url_convert(compressor, url, output_path):
if is_valid(url):
compressor.url_compress(url, output_path)
def is_valid(path):
p = path.lower()
return p.endswith('.jpg') or p.endswith('.jpeg') or p.endswith('.png')