Skip to content

Commit

Permalink
Fix a bug when .png image size isn't improved after compression
Browse files Browse the repository at this point in the history
  • Loading branch information
Alyetama committed Jul 17, 2022
1 parent 0ea2001 commit d1384f2
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions _config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
theme: jekyll-theme-cayman
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MIT"
Expand Down
17 changes: 11 additions & 6 deletions rayim/rayim.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down Expand Up @@ -138,19 +140,22 @@ 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)

if to_jpeg and overwrite:
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():
Expand Down

0 comments on commit d1384f2

Please sign in to comment.