Skip to content

Commit

Permalink
get pixel size by counting bytes in 1x1 image
Browse files Browse the repository at this point in the history
  • Loading branch information
Yay295 committed Apr 6, 2024
1 parent d19430e commit 0869248
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 38 deletions.
55 changes: 24 additions & 31 deletions Tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,39 +28,32 @@
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":
Expand Down
19 changes: 12 additions & 7 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
)

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,
Expand Down Expand Up @@ -1036,13 +1034,20 @@ def test_roundtrip_bytes_method(self, mode: str) -> None:
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)

Expand Down

0 comments on commit 0869248

Please sign in to comment.