mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
get pixel size by counting bytes in 1x1 image
This commit is contained in:
parent
5dabc6cf14
commit
fe79ae5653
|
@ -13,7 +13,7 @@ import sysconfig
|
|||
import tempfile
|
||||
from functools import lru_cache
|
||||
from io import BytesIO
|
||||
from typing import Any, Callable, NamedTuple, Sequence
|
||||
from typing import Any, Callable, Sequence
|
||||
|
||||
import pytest
|
||||
from packaging.version import parse as parse_version
|
||||
|
@ -29,39 +29,32 @@ elif "GITHUB_ACTIONS" in os.environ:
|
|||
uploader = "github_actions"
|
||||
|
||||
|
||||
class ImageModeInfo(NamedTuple):
|
||||
name: str
|
||||
pixel_size: int
|
||||
|
||||
|
||||
image_modes = (
|
||||
ImageModeInfo("1", 1),
|
||||
ImageModeInfo("L", 1),
|
||||
ImageModeInfo("LA", 4),
|
||||
ImageModeInfo("La", 4),
|
||||
ImageModeInfo("P", 1),
|
||||
ImageModeInfo("PA", 4),
|
||||
ImageModeInfo("F", 4),
|
||||
ImageModeInfo("I", 4),
|
||||
ImageModeInfo("I;16", 2),
|
||||
ImageModeInfo("I;16L", 2),
|
||||
ImageModeInfo("I;16B", 2),
|
||||
ImageModeInfo("I;16N", 2),
|
||||
ImageModeInfo("RGB", 4),
|
||||
ImageModeInfo("RGBA", 4),
|
||||
ImageModeInfo("RGBa", 4),
|
||||
ImageModeInfo("RGBX", 4),
|
||||
ImageModeInfo("BGR;15", 2),
|
||||
ImageModeInfo("BGR;16", 2),
|
||||
ImageModeInfo("BGR;24", 3),
|
||||
ImageModeInfo("CMYK", 4),
|
||||
ImageModeInfo("YCbCr", 4),
|
||||
ImageModeInfo("HSV", 4),
|
||||
ImageModeInfo("LAB", 4),
|
||||
image_mode_names = (
|
||||
"1",
|
||||
"L",
|
||||
"LA",
|
||||
"La",
|
||||
"P",
|
||||
"PA",
|
||||
"F",
|
||||
"I",
|
||||
"I;16",
|
||||
"I;16L",
|
||||
"I;16B",
|
||||
"I;16N",
|
||||
"RGB",
|
||||
"RGBA",
|
||||
"RGBa",
|
||||
"RGBX",
|
||||
"BGR;15",
|
||||
"BGR;16",
|
||||
"BGR;24",
|
||||
"CMYK",
|
||||
"YCbCr",
|
||||
"HSV",
|
||||
"LAB",
|
||||
)
|
||||
|
||||
image_mode_names = [mode.name for mode in image_modes]
|
||||
|
||||
|
||||
def upload(a: Image.Image, b: Image.Image) -> str | None:
|
||||
if uploader == "show":
|
||||
|
|
|
@ -23,14 +23,12 @@ from PIL import (
|
|||
)
|
||||
|
||||
from .helper import (
|
||||
ImageModeInfo,
|
||||
assert_image_equal,
|
||||
assert_image_equal_tofile,
|
||||
assert_image_similar_tofile,
|
||||
assert_not_all_same,
|
||||
hopper,
|
||||
image_mode_names,
|
||||
image_modes,
|
||||
is_win32,
|
||||
mark_if_feature_version,
|
||||
skip_unless_feature,
|
||||
|
@ -1036,13 +1034,20 @@ class TestImageBytes:
|
|||
reloaded.frombytes(source_bytes)
|
||||
assert reloaded.tobytes() == source_bytes
|
||||
|
||||
@pytest.mark.parametrize("mode", image_modes)
|
||||
def test_getdata_putdata(self, mode: ImageModeInfo) -> None:
|
||||
im = Image.new(mode.name, (2, 2))
|
||||
source_bytes = bytes(range(im.width * im.height * mode.pixel_size))
|
||||
@pytest.mark.parametrize("mode", image_mode_names)
|
||||
def test_getdata_putdata(self, mode: str) -> None:
|
||||
# create an image with 1 pixel to get its 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)
|
||||
|
||||
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())
|
||||
assert_image_equal(im, reloaded)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user