-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcompressor.py
38 lines (31 loc) · 983 Bytes
/
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
#!/usr/bin/env
import tinify
import os
import sys
class Compressor(object):
def __init__(self, API_KEY):
tinify.key = API_KEY
tinify.validate()
def compress(self, image_path, replace=True, save_path=None):
try:
source = tinify.from_file(image_path)
if replace:
source.to_file(image_path)
else:
source.to_file(save_path)
except tinify.errors.AccountError as err:
print(err)
except Exception as e:
print(e)
def url_compress(self, image_url, save_path):
try:
source = tinify.from_url(image_url)
IMAGE = image_url.rsplit('/',1)[1]
path = save_path + "/" + IMAGE
source.to_file(path)
except tinify.errors.AccountError as err:
print(err)
except Exception as e:
print(e)
def quota_left(self):
return 500 - tinify.compression_count