From d61470b734782da60988903b2c3b13df0977dc89 Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 25 Feb 2022 17:38:37 +1100 Subject: [PATCH] Added BLP1 saving --- Tests/test_file_blp.py | 20 +++++++++++--------- src/PIL/BlpImagePlugin.py | 7 ++++++- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Tests/test_file_blp.py b/Tests/test_file_blp.py index 86f208729f7..c1fae44ca35 100644 --- a/Tests/test_file_blp.py +++ b/Tests/test_file_blp.py @@ -31,19 +31,21 @@ def test_load_blp2_dxt1a(): def test_save(tmp_path): - im = hopper("P") f = str(tmp_path / "temp.blp") - im.save(f) - 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) + for version in ("BLP1", "BLP2"): + im = hopper("P") + im.save(f, blp_version=version) with Image.open(f) as reloaded: - assert_image_similar(im, reloaded, 8) + 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, blp_version=version) + + with Image.open(f) as reloaded: + assert_image_similar(im, reloaded, 8) im = hopper() with pytest.raises(ValueError): diff --git a/src/PIL/BlpImagePlugin.py b/src/PIL/BlpImagePlugin.py index bfa5c78e922..d28471a6f68 100644 --- a/src/PIL/BlpImagePlugin.py +++ b/src/PIL/BlpImagePlugin.py @@ -479,13 +479,18 @@ def _save(im, fp, filename, save_all=False): if im.mode != "P": raise ValueError("Unsupported BLP image mode") - fp.write(b"BLP2") + magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2" + fp.write(magic) + fp.write(struct.pack("