From 3ec928251cb1e315981b6d582caf1107f1ec2181 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 25 Feb 2022 16:54:53 +1100 Subject: [PATCH] Fixed reading uncompressed BLP2 with alpha --- Tests/test_file_blp.py | 14 +++++++++++++- src/PIL/BlpImagePlugin.py | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Tests/test_file_blp.py b/Tests/test_file_blp.py index 0891d4053..86f208729 100644 --- a/Tests/test_file_blp.py +++ b/Tests/test_file_blp.py @@ -2,7 +2,12 @@ import pytest from PIL import BlpImagePlugin, Image -from .helper import assert_image_equal, assert_image_equal_tofile, hopper +from .helper import ( + assert_image_equal, + assert_image_equal_tofile, + assert_image_similar, + hopper, +) def test_load_blp1(): @@ -33,6 +38,13 @@ def test_save(tmp_path): with Image.open(f) as reloaded: 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) + im = hopper() with pytest.raises(ValueError): im.save(f) diff --git a/src/PIL/BlpImagePlugin.py b/src/PIL/BlpImagePlugin.py index dfd651867..f83f1979e 100644 --- a/src/PIL/BlpImagePlugin.py +++ b/src/PIL/BlpImagePlugin.py @@ -406,7 +406,10 @@ class BLP2Decoder(_BLPBaseDecoder): 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: