From 3c975abc25b6410b325661e6d7014cc08e6c4d84 Mon Sep 17 00:00:00 2001 From: Andros Date: Sun, 29 Oct 2017 20:42:13 +0100 Subject: [PATCH] Upload to pip --- .gitignore | 10 ++++++++ LICENSE.txt | 7 ++++++ README.md | 10 ++++++-- guetzli-recursively.py | 44 ---------------------------------- guetzli_recursively.py | 54 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 19 +++++++++++++++ 6 files changed, 98 insertions(+), 46 deletions(-) create mode 100644 .gitignore create mode 100644 LICENSE.txt delete mode 100644 guetzli-recursively.py create mode 100644 guetzli_recursively.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b0b1bfb --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ + +guetzli_recursively\.egg-info/ + +dist/ + +build/lib/ + +MANIFEST + +setup\.cfg diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..2eaf8e8 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright 2017 + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 6f45b3a..ea386ee 100644 --- a/README.md +++ b/README.md @@ -10,16 +10,22 @@ Guetzli must be installed on your system. Follow the official instructions. and 2.7.10 or Python 3. +After + +```bash +pip3 install guetzli-recursively +``` + # Use ```bash -python3 guetzli-recursively.py [folder] +guetzli_recursively [folder] ``` ## Example ```bash -python3 guetzli-recursively.py img +guetzli_recursively img/ ``` out diff --git a/guetzli-recursively.py b/guetzli-recursively.py deleted file mode 100644 index b39a28f..0000000 --- a/guetzli-recursively.py +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -from os import path, walk, remove, rename -from imghdr import what -from subprocess import call -from sys import argv -top_dir = argv[1] -TEMP_FILE = 'temp.jpg' -TYPES = ('jpeg',) - -for dirpath, dirnames, files in walk(top_dir): - for name in files: - url = path.join(dirpath, name) - # Check type - if what(url) in TYPES: - # Get urls - 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 - # 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') diff --git a/guetzli_recursively.py b/guetzli_recursively.py new file mode 100644 index 0000000..63ba1d4 --- /dev/null +++ b/guetzli_recursively.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Libraries +import click +from os import path, walk, remove, rename +from imghdr import what +from subprocess import call + +# Variables +TEMP_FILE = 'temp.jpg' +TYPES = ('jpeg',) + + +@click.command() +@click.argument('folder', type=click.Path(exists=True)) +def run(folder): + for dirpath, dirnames, files in walk(folder): + for name in files: + url = path.join(dirpath, name) + # Check type + if what(url) in TYPES: + # Get urls + click.echo(url) + url_out = path.join(folder, 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 + # 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) + click.echo('Save ' + str(round(100 - size_acurate, 2)) + '%') + else: + click.echo('It is not necessary to optimize') + + +if __name__ == '__main__': + run() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6849af6 --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +from setuptools import setup +setup( + name = 'guetzli_recursively', + py_modules=['guetzli_recursively'], + version = '1.1.0', + description = 'Script in Python to convert all the jpeg of a folder recursively with Guetzli.', + author = 'Andros Fenollosa', + author_email = 'andros@fenollosa.email', + url = 'https://github.com/tanrax/guetzli-recursively', + keywords = ['guetzli', 'jpeg', 'recursive'], + classifiers = [], + install_requires=[ + 'Click>=6.7', + ], + entry_points=''' + [console_scripts] + guetzli_recursively=guetzli_recursively:run + ''' +)