From d1384f206fb4ddb6b95b107965db46055df1fa3b Mon Sep 17 00:00:00 2001 From: Alyetama <56323389+Alyetama@users.noreply.github.com> Date: Sun, 17 Jul 2022 14:28:37 -0400 Subject: [PATCH] Fix a bug when .png image size isn't improved after compression --- README.md | 2 +- _config.yml | 1 + pyproject.toml | 2 +- rayim/rayim.py | 17 +++++++++++------ 4 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 _config.yml diff --git a/README.md b/README.md index 84d7a4d..2890a7b 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ positional arguments: path Path to a single file/directory or multiple files/directories -optional arguments: +options: -h, --help show this help message and exit -o OUTPUT_DIR, --output-dir OUTPUT_DIR Output directory (default: next to original file) diff --git a/_config.yml b/_config.yml new file mode 100644 index 0000000..277f1f2 --- /dev/null +++ b/_config.yml @@ -0,0 +1 @@ +theme: jekyll-theme-cayman diff --git a/pyproject.toml b/pyproject.toml index 053a557..e4bfae3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "rayim" -version = "1.5.0" +version = "1.6.0" description = "Fast image compression for large number of images with Ray library" authors = ["Mohammad Alyetama "] license = "MIT" diff --git a/rayim/rayim.py b/rayim/rayim.py index 1ddf481..55e8e4a 100644 --- a/rayim/rayim.py +++ b/rayim/rayim.py @@ -90,8 +90,10 @@ def compress(file: str, if overwrite: out_file = copy.deepcopy(file) + elif to_jpeg: + out_file = f'{file.with_suffix("")}_compressed.jpg' else: - out_file = f'{file.with_suffix("")}_compressed{file.suffix}' + out_file = f'{file.with_suffix("")}_compressed{original_file_suffix}' if output_dir: out_file = f'{file.with_suffix("")}{file.suffix}' @@ -138,11 +140,11 @@ def compress(file: str, change, change_exists = size_change(float(original_size), float(compressed_size)) + if not to_jpeg: + out_file = Path(out_file).with_suffix(original_file_suffix) + else: + out_file = Path(out_file).with_suffix('.jpg') if change_exists: - if not to_jpeg: - out_file = Path(out_file).with_suffix(original_file_suffix) - else: - out_file = Path(out_file).with_suffix('.jpg') save_img(out_file, im, no_subsampling, f_suffix, quality, to_jpeg, optimize) @@ -150,7 +152,10 @@ def compress(file: str, if Path(out_file).name != file.name: file.unlink() - size_2 = Path(out_file).stat().st_size + if Path(out_file).exists(): + size_2 = Path(out_file).stat().st_size + else: + size_2 = size_1 took = round(time.time() - start, 2) if sys.stdout.isatty():