1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
from __future__ import annotations
import pytest
from PIL import Image
from .helper import (
assert_image_similar,
has_feature_version,
hopper,
is_ppc64le,
skip_unless_feature,
)
def test_sanity() -> None:
image = hopper()
converted = image.quantize()
assert converted.mode == "P"
assert_image_similar(converted.convert("RGB"), image, 10)
image = hopper()
converted = image.quantize(palette=hopper("P"))
assert converted.mode == "P"
assert_image_similar(converted.convert("RGB"), image, 60)
@skip_unless_feature("libimagequant")
def test_libimagequant_quantize() -> None:
image = hopper()
if is_ppc64le() and not has_feature_version("libimagequant", "4"):
pytest.skip("Fails with libimagequant earlier than 4.0.0 on ppc64le")
converted = image.quantize(100, Image.Quantize.LIBIMAGEQUANT)
assert converted.mode == "P"
assert_image_similar(converted.convert("RGB"), image, 15)
colors = converted.getcolors()
assert colors is not None
assert len(colors) == 100
def test_octree_quantize() -> None:
image = hopper()
converted = image.quantize(100, Image.Quantize.FASTOCTREE)
assert converted.mode == "P"
assert_image_similar(converted.convert("RGB"), image, 20)
colors = converted.getcolors()
assert colors is not None
assert len(colors) == 100
def test_rgba_quantize() -> None:
image = hopper("RGBA")
with pytest.raises(ValueError):
image.quantize(method=0)
assert image.quantize().convert().mode == "RGBA"
def test_quantize() -> None:
with Image.open("Tests/images/caption_6_33_22.png") as image:
converted = image.convert("RGB")
converted = converted.quantize()
assert converted.mode == "P"
assert_image_similar(converted.convert("RGB"), image, 1)
def test_quantize_no_dither() -> None:
image = hopper()
with Image.open("Tests/images/caption_6_33_22.png") as palette:
palette_p = palette.convert("P")
converted = image.quantize(dither=Image.Dither.NONE, palette=palette_p)
assert converted.mode == "P"
assert converted.palette is not None
assert palette_p.palette is not None
assert converted.palette.palette == palette_p.palette.palette
def test_quantize_no_dither2() -> None:
im = Image.new("RGB", (9, 1))
im.putdata([(p,) * 3 for p in range(0, 36, 4)])
palette = Image.new("P", (1, 1))
data = (0, 0, 0, 32, 32, 32)
palette.putpalette(data)
quantized = im.quantize(dither=Image.Dither.NONE, palette=palette)
assert quantized.palette is not None
assert tuple(quantized.palette.palette) == data
px = quantized.load()
assert px is not None
for x in range(9):
assert px[x, 0] == (0 if x < 5 else 1)
def test_quantize_dither_diff() -> None:
image = hopper()
with Image.open("Tests/images/caption_6_33_22.png") as palette:
palette_p = palette.convert("P")
dither = image.quantize(dither=Image.Dither.FLOYDSTEINBERG, palette=palette_p)
nodither = image.quantize(dither=Image.Dither.NONE, palette=palette_p)
assert dither.tobytes() != nodither.tobytes()
@pytest.mark.parametrize(
"method", (Image.Quantize.MEDIANCUT, Image.Quantize.MAXCOVERAGE)
)
def test_quantize_kmeans(method: Image.Quantize) -> None:
im = hopper()
no_kmeans = im.quantize(kmeans=0, method=method)
kmeans = im.quantize(kmeans=1, method=method)
assert kmeans.tobytes() != no_kmeans.tobytes()
with pytest.raises(ValueError):
im.quantize(kmeans=-1, method=method)
@skip_unless_feature("libimagequant")
def test_resize() -> None:
im = hopper().resize((100, 100))
converted = im.quantize(100, Image.Quantize.LIBIMAGEQUANT)
colors = converted.getcolors()
assert colors is not None
assert len(colors) == 100
def test_colors() -> None:
im = hopper()
colors = 2
converted = im.quantize(colors)
assert converted.palette is not None
assert len(converted.palette.palette) == colors * len("RGB")
def test_transparent_colors_equal() -> None:
im = Image.new("RGBA", (1, 2), (0, 0, 0, 0))
px = im.load()
assert px is not None
px[0, 1] = (255, 255, 255, 0)
converted = im.quantize()
converted_px = converted.load()
assert converted_px is not None
assert converted_px[0, 0] == converted_px[0, 1]
@pytest.mark.parametrize(
"method, color",
(
(Image.Quantize.MEDIANCUT, (0, 0, 0)),
(Image.Quantize.MAXCOVERAGE, (0, 0, 0)),
(Image.Quantize.FASTOCTREE, (0, 0, 0)),
(Image.Quantize.FASTOCTREE, (0, 0, 0, 0)),
),
)
def test_palette(method: Image.Quantize, color: tuple[int, ...]) -> None:
im = Image.new("RGBA" if len(color) == 4 else "RGB", (1, 1), color)
converted = im.quantize(method=method)
assert converted.palette is not None
assert converted.getpixel((0, 0)) == converted.palette.colors[color]
def test_small_palette() -> None:
# Arrange
im = hopper()
colors = (255, 0, 0, 0, 0, 255)
p = Image.new("P", (1, 1))
p.putpalette(colors)
# Act
im = im.quantize(palette=p)
# Assert
quantized_colors = im.getcolors()
assert quantized_colors is not None
assert len(quantized_colors) == 2
|