Skip to content

Commit 41dfc44

Browse files
Merge branch 'pngquant-fix-draft' of github.com:PyAr/CDPedia into pngquant-fix-draft
2 parents 6068c81 + 59cee51 commit 41dfc44

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/images/download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ def optimize_png(img_path, original_size, current_size):
6666
"""Run pngquant to optimize PNG format."""
6767
temp_fpath = img_path + ".temp"
6868
subprocess.run(["pngquant", "--quality=40-70", "--output={}".format(temp_fpath), img_path])
69+
os.rename(temp_fpath, img_path)
6970
final_size = os.stat(img_path).st_size
7071
logger.debug("Metadata removed from %r: %d(bytes) removed"
7172
" · PNG, Extra clean-up: %d(bytes) removed",
7273
img_path, original_size - current_size, current_size - final_size)
73-
os.rename(temp_fpath, img_path)
7474

7575

7676
def _download(url, fullpath):
56.7 KB
Loading

tests/test_pngquant.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2021 CDPedistas (see AUTHORS.txt)
2+
#
3+
# This program is free software: you can redistribute it and/or modify it
4+
# under the terms of the GNU General Public License version 3, as published
5+
# by the Free Software Foundation.
6+
#
7+
# This program is distributed in the hope that it will be useful, but
8+
# WITHOUT ANY WARRANTY; without even the implied warranties of
9+
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
10+
# PURPOSE. See the GNU General Public License for more details.
11+
#
12+
# You should have received a copy of the GNU General Public License along
13+
# with this program. If not, see <http://www.gnu.org/licenses/>.
14+
#
15+
# For further info, check https://github.com/PyAr/CDPedia/
16+
17+
import os
18+
import shutil
19+
20+
from src.images.download import optimize_png
21+
22+
import pytest
23+
24+
25+
@pytest.fixture
26+
def image_config(tmp_path):
27+
img_test_name = 'tests/fixtures/image_to_optimize.png'
28+
test_path = str(tmp_path / 'image_to_optimize.png')
29+
shutil.copy(img_test_name, test_path)
30+
init_size = os.stat(img_test_name).st_size
31+
return test_path, init_size
32+
33+
34+
def test_optimize(image_config):
35+
img_path, init_size = image_config
36+
optimize_png(img_path, init_size, init_size)
37+
final_size = os.stat(img_path).st_size
38+
assert init_size > final_size

0 commit comments

Comments
 (0)