From b33ebc8c71b7132b7e19271720c0073f0e7e5dd5 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 25 Feb 2022 16:26:02 +1100 Subject: [PATCH] Fixed reading uncompressed BLP2 with alpha --- Tests/test_file_blp.py | 16 ++++++++++++++-- src/PIL/BlpImagePlugin.py | 5 ++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Tests/test_file_blp.py b/Tests/test_file_blp.py index 22ddcb69152..c07232dd6bd 100644 --- a/Tests/test_file_blp.py +++ b/Tests/test_file_blp.py @@ -2,7 +2,12 @@ from PIL import BlpImagePlugin, Image -from .helper import assert_image_equal, assert_image_equal_tofile, hopper +from .helper import ( + assert_image_equal, + assert_image_similar, + assert_image_equal_tofile, + hopper, +) def test_load_blp1(): @@ -31,7 +36,14 @@ def test_save(tmp_path): im.save(f) with Image.open(f) as reloaded: - assert_image_equal(im, reloaded.convert("RGB")) + assert_image_equal(im.convert("RGB"), reloaded) + + with Image.open("Tests/images/transparent.png") as im: + f = str(tmp_path / "temp.blp") + im.convert("P").save(f) + + with Image.open(f) as reloaded: + assert_image_similar(im, reloaded, 8) @pytest.mark.parametrize( diff --git a/src/PIL/BlpImagePlugin.py b/src/PIL/BlpImagePlugin.py index dfd6518671f..f83f1979e9b 100644 --- a/src/PIL/BlpImagePlugin.py +++ b/src/PIL/BlpImagePlugin.py @@ -406,7 +406,10 @@ def _load(self): except struct.error: break b, g, r, a = palette[offset] - data.extend((r, g, b)) + d = (r, g, b) + if self._blp_alpha_depth: + d += (a,) + data.extend(d) elif self._blp_encoding == Encoding.DXT: if self._blp_alpha_encoding == AlphaEncoding.DXT1: