mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-11-15 06:07:33 +03:00
Use im.has_transparency_data for webp._save_all
Also: remove _VALID_WEBP_MODES and _VALID_WEBP_LEGACY_MODES consts RGBX is not faster RGB since demands more bandwidth Do not convert to str paths in tests
This commit is contained in:
parent
f30eefaae2
commit
0a8e6dbedb
|
@ -72,7 +72,7 @@ class TestFileWebp:
|
||||||
def _roundtrip(
|
def _roundtrip(
|
||||||
self, tmp_path: Path, mode: str, epsilon: float, args: dict[str, Any] = {}
|
self, tmp_path: Path, mode: str, epsilon: float, args: dict[str, Any] = {}
|
||||||
) -> None:
|
) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
temp_file = tmp_path / "temp.webp"
|
||||||
|
|
||||||
hopper(mode).save(temp_file, **args)
|
hopper(mode).save(temp_file, **args)
|
||||||
with Image.open(temp_file) as image:
|
with Image.open(temp_file) as image:
|
||||||
|
@ -116,7 +116,7 @@ class TestFileWebp:
|
||||||
assert buffer_no_args.getbuffer() != buffer_method.getbuffer()
|
assert buffer_no_args.getbuffer() != buffer_method.getbuffer()
|
||||||
|
|
||||||
def test_save_all(self, tmp_path: Path) -> None:
|
def test_save_all(self, tmp_path: Path) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
temp_file = tmp_path / "temp.webp"
|
||||||
im = Image.new("RGB", (1, 1))
|
im = Image.new("RGB", (1, 1))
|
||||||
im2 = Image.new("RGB", (1, 1), "#f00")
|
im2 = Image.new("RGB", (1, 1), "#f00")
|
||||||
im.save(temp_file, save_all=True, append_images=[im2])
|
im.save(temp_file, save_all=True, append_images=[im2])
|
||||||
|
@ -151,18 +151,16 @@ class TestFileWebp:
|
||||||
|
|
||||||
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
|
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
|
||||||
def test_write_encoding_error_message(self, tmp_path: Path) -> None:
|
def test_write_encoding_error_message(self, tmp_path: Path) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
|
||||||
im = Image.new("RGB", (15000, 15000))
|
im = Image.new("RGB", (15000, 15000))
|
||||||
with pytest.raises(ValueError) as e:
|
with pytest.raises(ValueError) as e:
|
||||||
im.save(temp_file, method=0)
|
im.save(tmp_path / "temp.webp", method=0)
|
||||||
assert str(e.value) == "encoding error 6"
|
assert str(e.value) == "encoding error 6"
|
||||||
|
|
||||||
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
|
@pytest.mark.skipif(sys.maxsize <= 2**32, reason="Requires 64-bit system")
|
||||||
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:
|
def test_write_encoding_error_bad_dimension(self, tmp_path: Path) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
|
||||||
im = Image.new("L", (16384, 16384))
|
im = Image.new("L", (16384, 16384))
|
||||||
with pytest.raises(ValueError) as e:
|
with pytest.raises(ValueError) as e:
|
||||||
im.save(temp_file)
|
im.save(tmp_path / "temp.webp")
|
||||||
assert (
|
assert (
|
||||||
str(e.value)
|
str(e.value)
|
||||||
== "encoding error 5: Image size exceeds WebP limit of 16383 pixels"
|
== "encoding error 5: Image size exceeds WebP limit of 16383 pixels"
|
||||||
|
@ -187,9 +185,8 @@ class TestFileWebp:
|
||||||
def test_no_resource_warning(self, tmp_path: Path) -> None:
|
def test_no_resource_warning(self, tmp_path: Path) -> None:
|
||||||
file_path = "Tests/images/hopper.webp"
|
file_path = "Tests/images/hopper.webp"
|
||||||
with Image.open(file_path) as image:
|
with Image.open(file_path) as image:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
|
||||||
with warnings.catch_warnings():
|
with warnings.catch_warnings():
|
||||||
image.save(temp_file)
|
image.save(tmp_path / "temp.webp")
|
||||||
|
|
||||||
def test_file_pointer_could_be_reused(self) -> None:
|
def test_file_pointer_could_be_reused(self) -> None:
|
||||||
file_path = "Tests/images/hopper.webp"
|
file_path = "Tests/images/hopper.webp"
|
||||||
|
@ -204,15 +201,16 @@ class TestFileWebp:
|
||||||
def test_invalid_background(
|
def test_invalid_background(
|
||||||
self, background: int | tuple[int, ...], tmp_path: Path
|
self, background: int | tuple[int, ...], tmp_path: Path
|
||||||
) -> None:
|
) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
temp_file = tmp_path / "temp.webp"
|
||||||
im = hopper()
|
im = hopper()
|
||||||
with pytest.raises(OSError):
|
with pytest.raises(OSError):
|
||||||
im.save(temp_file, save_all=True, append_images=[im], background=background)
|
im.save(temp_file, save_all=True, append_images=[im], background=background)
|
||||||
|
|
||||||
def test_background_from_gif(self, tmp_path: Path) -> None:
|
def test_background_from_gif(self, tmp_path: Path) -> None:
|
||||||
|
out_webp = tmp_path / "temp.webp"
|
||||||
|
|
||||||
# Save L mode GIF with background
|
# Save L mode GIF with background
|
||||||
with Image.open("Tests/images/no_palette_with_background.gif") as im:
|
with Image.open("Tests/images/no_palette_with_background.gif") as im:
|
||||||
out_webp = str(tmp_path / "temp.webp")
|
|
||||||
im.save(out_webp, save_all=True)
|
im.save(out_webp, save_all=True)
|
||||||
|
|
||||||
# Save P mode GIF with background
|
# Save P mode GIF with background
|
||||||
|
@ -220,11 +218,10 @@ class TestFileWebp:
|
||||||
original_value = im.convert("RGB").getpixel((1, 1))
|
original_value = im.convert("RGB").getpixel((1, 1))
|
||||||
|
|
||||||
# Save as WEBP
|
# Save as WEBP
|
||||||
out_webp = str(tmp_path / "temp.webp")
|
|
||||||
im.save(out_webp, save_all=True)
|
im.save(out_webp, save_all=True)
|
||||||
|
|
||||||
# Save as GIF
|
# Save as GIF
|
||||||
out_gif = str(tmp_path / "temp.gif")
|
out_gif = tmp_path / "temp.gif"
|
||||||
with Image.open(out_webp) as im:
|
with Image.open(out_webp) as im:
|
||||||
im.save(out_gif)
|
im.save(out_gif)
|
||||||
|
|
||||||
|
@ -234,10 +231,10 @@ class TestFileWebp:
|
||||||
assert difference < 5
|
assert difference < 5
|
||||||
|
|
||||||
def test_duration(self, tmp_path: Path) -> None:
|
def test_duration(self, tmp_path: Path) -> None:
|
||||||
|
out_webp = tmp_path / "temp.webp"
|
||||||
|
|
||||||
with Image.open("Tests/images/dispose_bgnd.gif") as im:
|
with Image.open("Tests/images/dispose_bgnd.gif") as im:
|
||||||
assert im.info["duration"] == 1000
|
assert im.info["duration"] == 1000
|
||||||
|
|
||||||
out_webp = str(tmp_path / "temp.webp")
|
|
||||||
im.save(out_webp, save_all=True)
|
im.save(out_webp, save_all=True)
|
||||||
|
|
||||||
with Image.open(out_webp) as reloaded:
|
with Image.open(out_webp) as reloaded:
|
||||||
|
@ -245,7 +242,7 @@ class TestFileWebp:
|
||||||
assert reloaded.info["duration"] == 1000
|
assert reloaded.info["duration"] == 1000
|
||||||
|
|
||||||
def test_roundtrip_rgba_palette(self, tmp_path: Path) -> None:
|
def test_roundtrip_rgba_palette(self, tmp_path: Path) -> None:
|
||||||
temp_file = str(tmp_path / "temp.webp")
|
temp_file = tmp_path / "temp.webp"
|
||||||
im = Image.new("RGBA", (1, 1)).convert("P")
|
im = Image.new("RGBA", (1, 1)).convert("P")
|
||||||
assert im.mode == "P"
|
assert im.mode == "P"
|
||||||
assert im.palette is not None
|
assert im.palette is not None
|
||||||
|
|
|
@ -13,10 +13,6 @@ except ImportError:
|
||||||
SUPPORTED = False
|
SUPPORTED = False
|
||||||
|
|
||||||
|
|
||||||
_VALID_WEBP_MODES = {"RGBX": True, "RGBA": True, "RGB": True}
|
|
||||||
|
|
||||||
_VALID_WEBP_LEGACY_MODES = {"RGB": True, "RGBA": True}
|
|
||||||
|
|
||||||
_VP8_MODES_BY_IDENTIFIER = {
|
_VP8_MODES_BY_IDENTIFIER = {
|
||||||
b"VP8 ": "RGB",
|
b"VP8 ": "RGB",
|
||||||
b"VP8X": "RGBA",
|
b"VP8X": "RGBA",
|
||||||
|
@ -247,27 +243,16 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
|
|
||||||
# Make sure image mode is supported
|
# Make sure image mode is supported
|
||||||
frame = ims
|
frame = ims
|
||||||
rawmode = ims.mode
|
if frame.mode not in ("RGBX", "RGBA", "RGB"):
|
||||||
if ims.mode not in _VALID_WEBP_MODES:
|
frame = frame.convert("RGBA" if im.has_transparency_data else "RGB")
|
||||||
alpha = (
|
|
||||||
"A" in ims.mode
|
|
||||||
or "a" in ims.mode
|
|
||||||
or (ims.mode == "P" and "A" in ims.im.getpalettemode())
|
|
||||||
)
|
|
||||||
rawmode = "RGBA" if alpha else "RGB"
|
|
||||||
frame = ims.convert(rawmode)
|
|
||||||
|
|
||||||
if rawmode == "RGB":
|
|
||||||
# For faster conversion, use RGBX
|
|
||||||
rawmode = "RGBX"
|
|
||||||
|
|
||||||
# Append the frame to the animation encoder
|
# Append the frame to the animation encoder
|
||||||
enc.add(
|
enc.add(
|
||||||
frame.tobytes("raw", rawmode),
|
frame.tobytes(),
|
||||||
round(timestamp),
|
round(timestamp),
|
||||||
frame.size[0],
|
frame.size[0],
|
||||||
frame.size[1],
|
frame.size[1],
|
||||||
rawmode,
|
frame.mode,
|
||||||
lossless,
|
lossless,
|
||||||
quality,
|
quality,
|
||||||
alpha_quality,
|
alpha_quality,
|
||||||
|
@ -310,7 +295,7 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
||||||
method = im.encoderinfo.get("method", 4)
|
method = im.encoderinfo.get("method", 4)
|
||||||
exact = 1 if im.encoderinfo.get("exact") else 0
|
exact = 1 if im.encoderinfo.get("exact") else 0
|
||||||
|
|
||||||
if im.mode not in _VALID_WEBP_LEGACY_MODES:
|
if im.mode not in ("RGB", "RGBA"):
|
||||||
im = im.convert("RGBA" if im.has_transparency_data else "RGB")
|
im = im.convert("RGBA" if im.has_transparency_data else "RGB")
|
||||||
|
|
||||||
data = _webp.WebPEncode(
|
data = _webp.WebPEncode(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user