mirror of
https://github.com/tanrax/guetzli-recursively.git
synced 2024-12-22 20:25:35 +01:00
First commit
This commit is contained in:
commit
0bf39e2cd7
22
README.md
Normal file
22
README.md
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
Guetzli is a Google program to optimize JPEG images. Unfortunately, it only works one file at a time. With this script in Python you can do it recursively a whole folder.
|
||||
|
||||
# Install
|
||||
|
||||
Guetzli must be installed on your system. Follow the official instructions.
|
||||
[Guetzli](https://github.com/google/guetzli)
|
||||
|
||||
and Python 3.
|
||||
|
||||
# Use
|
||||
|
||||
```bash
|
||||
python guetzli-recursively.py [folder]
|
||||
```
|
||||
|
||||
Example
|
||||
|
||||
```bash
|
||||
python guetzli-recursively.py img
|
||||
```
|
||||
|
42
guetzli-recursively.py
Normal file
42
guetzli-recursively.py
Normal file
@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from os import path, walk, remove, rename
|
||||
from subprocess import call
|
||||
from sys import argv
|
||||
top_dir = argv[1]
|
||||
TEMP_FILE = 'temp.jpg'
|
||||
extensions = ('jpeg', 'jpg')
|
||||
|
||||
for dirpath, dirnames, files in walk(top_dir):
|
||||
for name in files:
|
||||
for extension in extensions:
|
||||
if name.lower().endswith(extension):
|
||||
# Get urls
|
||||
url = path.join(dirpath, name)
|
||||
print(url)
|
||||
url_out = path.join(top_dir, TEMP_FILE)
|
||||
# Remove temp image
|
||||
try:
|
||||
remove(url_out)
|
||||
except:
|
||||
pass
|
||||
# Execute guetzli
|
||||
call(['guetzli', url, url_out])
|
||||
# Print your have saved
|
||||
size_source = path.getsize(url)
|
||||
try:
|
||||
size_out = path.getsize(url_out)
|
||||
except:
|
||||
size_out = size_source
|
||||
size_acurate = 100 * size_out / size_source
|
||||
# Remove source
|
||||
try:
|
||||
remove(url)
|
||||
except:
|
||||
pass
|
||||
if size_acurate < 100:
|
||||
# Move temp to source
|
||||
rename(url_out, url)
|
||||
print('Save ' + str(100 - size_acurate) + '%')
|
||||
else:
|
||||
print('It is not necessary to optimize')
|
Loading…
Reference in New Issue
Block a user