-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressimg.py
122 lines (83 loc) · 2.85 KB
/
compressimg.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
import argparse
import os
import time
from shutil import copytree
from multiprocessing import Pool
import tinify
import config
VALID_EXTENSIONS = ('.jpeg', '.jpg', '.png')
def log(message):
print(message)
def convert(image_file):
done = False
message = ''
try:
log('Compressing {} ...'.format(image_file))
source_file = tinify.from_file(image_file)
source_file.to_file(image_file)
except tinify.AccountError:
message = 'ERROR: Verify your API key and your account limit.'
except tinify.ClientError:
message = 'ERROR: Problem with your request options or your image.'
except tinify.ServerError:
message = 'ERROR: Temporary issue on Tinify API.'
except tinify.ConnectionError:
message = 'ERROR: Network connection problem.'
except Exception:
message = 'ERROR: Something goes wrong and I do not know why.'
else:
done = True
return done, message
def get_convertible_files(directory):
convertible_files = []
for entry in os.scandir(directory):
if entry.is_dir():
get_convertible_files(entry.path)
_, extension = os.path.splitext(entry.path)
if extension.lower() not in VALID_EXTENSIONS:
continue
convertible_files.append(entry.path)
return convertible_files
def convert_files(image_file):
done = False
time_to_retry = 10 # seconds
retries = 10
while retries:
done, message = convert(image_file)
if done:
break
message += ' Time to retry: {}'.format(time_to_retry)
log(message)
retries -= 1
time.sleep(time_to_retry)
if not done:
log(' SKIPPED!')
def valid_directory(directory):
if not os.path.exists(directory):
raise Exception('Directory not found: {}'.format(directory))
if not os.path.isdir(directory):
raise Exception('Invalid directory: {}'.format(directory))
return os.path.normpath(os.path.realpath(directory))
def backup_files(directory, backup_directory):
copytree(directory, backup_directory)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Compress images using TinyPNG')
parser.add_argument(
'directory', help='Directory which contains images to be compressed'
)
parser.add_argument(
'backup_dir',
help='Backup directory for original files'
)
args = parser.parse_args()
try:
tinify.key = config.TINYPNG_KEY
tinify.validate()
except tinify.Error:
message = ('ERROR: Invalid/Expired Key: {}'.format(config.TINYPNG_KEY))
raise Exception(message)
directory = valid_directory(args.directory)
backup_files(directory, args.backup_dir)
pool = Pool(processes=4)
pool.map(convert_files, get_convertible_files(directory))