Pillow/Tests/test_image_getpalette.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

47 lines
1.1 KiB
Python
Raw Normal View History

from __future__ import annotations
2024-01-20 14:23:03 +03:00
2022-02-17 02:04:43 +03:00
from PIL import Image
from .helper import hopper
def test_palette() -> None:
def palette(mode):
p = hopper(mode).getpalette()
if p:
return p[:10]
return None
2019-06-13 18:54:24 +03:00
assert palette("1") is None
assert palette("L") is None
assert palette("I") is None
assert palette("F") is None
assert palette("P") == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert palette("RGB") is None
assert palette("RGBA") is None
assert palette("CMYK") is None
assert palette("YCbCr") is None
2022-02-17 02:04:43 +03:00
def test_palette_rawmode() -> None:
2022-02-17 02:04:43 +03:00
im = Image.new("P", (1, 1))
im.putpalette((1, 2, 3))
for rawmode in ("RGB", None):
rgb = im.getpalette(rawmode)
2022-02-20 15:54:12 +03:00
assert rgb == [1, 2, 3]
2022-02-17 02:04:43 +03:00
# Convert the RGB palette to RGBA
rgba = im.getpalette("RGBA")
2022-02-20 15:54:12 +03:00
assert rgba == [1, 2, 3, 255]
2022-02-17 02:04:43 +03:00
im.putpalette((1, 2, 3, 4), "RGBA")
# Convert the RGBA palette to RGB
rgb = im.getpalette("RGB")
2022-02-20 15:54:12 +03:00
assert rgb == [1, 2, 3]
2022-02-17 02:04:43 +03:00
for rawmode in ("RGBA", None):
rgba = im.getpalette(rawmode)
2022-02-20 15:54:12 +03:00
assert rgba == [1, 2, 3, 4]