From e36774617c6e6cac2540678b28858f9ed6783e1d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 25 Feb 2022 23:58:13 +1100 Subject: [PATCH] Added BLP1 saving --- Tests/test_file_blp.py | 20 +++++++++++--------- docs/handbook/image-file-formats.rst | 21 ++++++++++++++------- src/PIL/BlpImagePlugin.py | 13 +++++++++---- 3 files changed, 34 insertions(+), 20 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/docs/handbook/image-file-formats.rst b/docs/handbook/image-file-formats.rst index d1a5ba33982..17808dbc463 100644 --- a/docs/handbook/image-file-formats.rst +++ b/docs/handbook/image-file-formats.rst @@ -26,6 +26,20 @@ Fully supported formats .. contents:: +BLP +^^^ + +BLP is the Blizzard Mipmap Format, a texture format used in World of +Warcraft. Pillow supports reading ``JPEG`` Compressed or raw ``BLP1`` +images, and all types of ``BLP2`` images. + +Pillow supports writing BLP images. The :py:meth:`~PIL.Image.Image.save` method +can take the following keyword arguments: + +**blp_version** + If present and set to "BLP1", images will be saved as BLP1. Otherwise, images + will be saved as BLP2. + BMP ^^^ @@ -1042,13 +1056,6 @@ Pillow reads and writes X bitmap files (mode ``1``). Read-only formats ----------------- -BLP -^^^ - -BLP is the Blizzard Mipmap Format, a texture format used in World of -Warcraft. Pillow supports reading ``JPEG`` Compressed or raw ``BLP1`` -images, and all types of ``BLP2`` images. - CUR ^^^ diff --git a/src/PIL/BlpImagePlugin.py b/src/PIL/BlpImagePlugin.py index ffd0412e91c..779fddea860 100644 --- a/src/PIL/BlpImagePlugin.py +++ b/src/PIL/BlpImagePlugin.py @@ -439,7 +439,7 @@ def _load(self): self.set_as_raw(bytes(data)) -class BLP2Encoder(ImageFile.PyEncoder): +class BLPEncoder(ImageFile.PyEncoder): _pushes_fd = True def _write_palette(self): @@ -472,15 +472,20 @@ 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("