fixbug type file

This commit is contained in:
Andros Fenollosa 2017-04-18 19:32:11 +02:00
parent c70ed2bbf5
commit 7b2c882f66

View File

@ -1,43 +1,43 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from os import path, walk, remove, rename from os import path, walk, remove, rename
from imghdr import what
from subprocess import call from subprocess import call
from sys import argv from sys import argv
top_dir = argv[1] top_dir = argv[1]
TEMP_FILE = 'temp.jpg' TEMP_FILE = 'temp.jpg'
extensions = ('jpeg', 'jpg') TYPES = ('jpeg',)
for dirpath, dirnames, files in walk(top_dir): for dirpath, dirnames, files in walk(top_dir):
for name in files: for name in files:
for extension in extensions: if what(path.join(dirpath, name)) in TYPES:
if name.lower().endswith(extension): # Get urls
# Get urls url = path.join(dirpath, name)
url = path.join(dirpath, name) print(url)
print(url) url_out = path.join(top_dir, TEMP_FILE)
url_out = path.join(top_dir, TEMP_FILE) # Remove temp image
# 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
# Check if it is cost effective to replace it
if size_acurate < 100:
# Remove source
try: try:
remove(url_out) remove(url)
except: except:
pass pass
# Execute guetzli # Move temp to source
call(['guetzli', url, url_out]) rename(url_out, url)
# Print your have saved print('Save ' + str(round(100 - size_acurate, 2)) + '%')
size_source = path.getsize(url) else:
try: print('It is not necessary to optimize')
size_out = path.getsize(url_out)
except:
size_out = size_source
size_acurate = 100 * size_out / size_source
# Check if it is cost effective to replace it
if size_acurate < 100:
# Remove source
try:
remove(url)
except:
pass
# Move temp to source
rename(url_out, url)
print('Save ' + str(round(100 - size_acurate, 2)) + '%')
else:
print('It is not necessary to optimize')