get pixel size by counting bytes in 1x1 image

This commit is contained in:
Yay295 2024-04-06 10:48:38 -05:00
parent 5dabc6cf14
commit fe79ae5653
2 changed files with 37 additions and 39 deletions

View File

@ -13,7 +13,7 @@ import sysconfig
import tempfile import tempfile
from functools import lru_cache from functools import lru_cache
from io import BytesIO from io import BytesIO
from typing import Any, Callable, NamedTuple, Sequence from typing import Any, Callable, Sequence
import pytest import pytest
from packaging.version import parse as parse_version from packaging.version import parse as parse_version
@ -29,39 +29,32 @@ elif "GITHUB_ACTIONS" in os.environ:
uploader = "github_actions" uploader = "github_actions"
class ImageModeInfo(NamedTuple): image_mode_names = (
name: str "1",
pixel_size: int "L",
"LA",
"La",
image_modes = ( "P",
ImageModeInfo("1", 1), "PA",
ImageModeInfo("L", 1), "F",
ImageModeInfo("LA", 4), "I",
ImageModeInfo("La", 4), "I;16",
ImageModeInfo("P", 1), "I;16L",
ImageModeInfo("PA", 4), "I;16B",
ImageModeInfo("F", 4), "I;16N",
ImageModeInfo("I", 4), "RGB",
ImageModeInfo("I;16", 2), "RGBA",
ImageModeInfo("I;16L", 2), "RGBa",
ImageModeInfo("I;16B", 2), "RGBX",
ImageModeInfo("I;16N", 2), "BGR;15",
ImageModeInfo("RGB", 4), "BGR;16",
ImageModeInfo("RGBA", 4), "BGR;24",
ImageModeInfo("RGBa", 4), "CMYK",
ImageModeInfo("RGBX", 4), "YCbCr",
ImageModeInfo("BGR;15", 2), "HSV",
ImageModeInfo("BGR;16", 2), "LAB",
ImageModeInfo("BGR;24", 3),
ImageModeInfo("CMYK", 4),
ImageModeInfo("YCbCr", 4),
ImageModeInfo("HSV", 4),
ImageModeInfo("LAB", 4),
) )
image_mode_names = [mode.name for mode in image_modes]
def upload(a: Image.Image, b: Image.Image) -> str | None: def upload(a: Image.Image, b: Image.Image) -> str | None:
if uploader == "show": if uploader == "show":

View File

@ -23,14 +23,12 @@ from PIL import (
) )
from .helper import ( from .helper import (
ImageModeInfo,
assert_image_equal, assert_image_equal,
assert_image_equal_tofile, assert_image_equal_tofile,
assert_image_similar_tofile, assert_image_similar_tofile,
assert_not_all_same, assert_not_all_same,
hopper, hopper,
image_mode_names, image_mode_names,
image_modes,
is_win32, is_win32,
mark_if_feature_version, mark_if_feature_version,
skip_unless_feature, skip_unless_feature,
@ -1036,13 +1034,20 @@ class TestImageBytes:
reloaded.frombytes(source_bytes) reloaded.frombytes(source_bytes)
assert reloaded.tobytes() == source_bytes assert reloaded.tobytes() == source_bytes
@pytest.mark.parametrize("mode", image_modes) @pytest.mark.parametrize("mode", image_mode_names)
def test_getdata_putdata(self, mode: ImageModeInfo) -> None: def test_getdata_putdata(self, mode: str) -> None:
im = Image.new(mode.name, (2, 2)) # create an image with 1 pixel to get its pixel size
source_bytes = bytes(range(im.width * im.height * mode.pixel_size)) im = Image.new(mode, (1, 1))
pixel_size = len(im.tobytes())
# create a new image with incrementing byte values
im = Image.new(mode, (2, 2))
source_bytes = bytes(range(im.width * im.height * pixel_size))
im.frombytes(source_bytes) im.frombytes(source_bytes)
reloaded = Image.new(mode.name, im.size) # copy the data from the previous image to a new image
# and check that they are the same
reloaded = Image.new(mode, im.size)
reloaded.putdata(im.getdata()) reloaded.putdata(im.getdata())
assert_image_equal(im, reloaded) assert_image_equal(im, reloaded)