Skip to content

Commit

Permalink
Use getpixel() instead of load()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 3, 2025
1 parent 4d1f874 commit ce1996d
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 25 deletions.
13 changes: 6 additions & 7 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def test_invalid_file() -> None:
def test_l_mode_transparency() -> None:
with Image.open("Tests/images/no_palette_with_transparency.gif") as im:
assert im.mode == "L"
assert im.load()[0, 0] == 128
assert im.getpixel((0, 0)) == 128
assert im.info["transparency"] == 255

im.seek(1)
assert im.mode == "L"
assert im.load()[0, 0] == 128
assert im.getpixel((0, 0)) == 128


def test_l_mode_after_rgb() -> None:
Expand Down Expand Up @@ -311,18 +311,18 @@ def test_loading_multiple_palettes(path: str, mode: str) -> None:
with Image.open(path) as im:
assert im.mode == "P"
first_frame_colors = im.palette.colors.keys()
original_color = im.convert("RGB").load()[0, 0]
original_color = im.convert("RGB").getpixel((0, 0))

im.seek(1)
assert im.mode == mode
if mode == "RGBA":
im = im.convert("RGB")

# Check a color only from the old palette
assert im.load()[0, 0] == original_color
assert im.getpixel((0, 0)) == original_color

# Check a color from the new palette
assert im.load()[24, 24] not in first_frame_colors
assert im.getpixel((24, 24)) not in first_frame_colors


def test_headers_saving_for_animated_gifs(tmp_path: Path) -> None:
Expand Down Expand Up @@ -488,8 +488,7 @@ def test_eoferror() -> None:

def test_first_frame_transparency() -> None:
with Image.open("Tests/images/first_frame_transparency.gif") as im:
px = im.load()
assert px[0, 0] == im.info["transparency"]
assert im.getpixel((0, 0)) == im.info["transparency"]


def test_dispose_none() -> None:
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,7 @@ def test_storage_neg(self) -> None:
def test_one_item_tuple(self) -> None:
for mode in ("I", "F", "L"):
im = Image.new(mode, (100, 100), (5,))
px = im.load()
assert px is not None
assert px[0, 0] == 5
assert im.getpixel((0, 0)) == 5

def test_linear_gradient_wrong_mode(self) -> None:
# Arrange
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_image_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ def test_l_macro_rounding(convert_mode: str) -> None:
im.palette.getcolor((0, 1, 2))

converted_im = im.convert(convert_mode)
px = converted_im.load()
assert px is not None
converted_color = px[0, 0]
converted_color = converted_im.getpixel((0, 0))
if convert_mode == "LA":
assert isinstance(converted_color, tuple)
converted_color = converted_color[0]
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_image_quantize.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,8 @@ 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)
converted_px = converted.load()
assert converted_px is not None
assert converted.palette is not None
assert converted_px[0, 0] == converted.palette.colors[color]
assert converted.getpixel((0, 0)) == converted.palette.colors[color]


def test_small_palette() -> None:
Expand Down
8 changes: 2 additions & 6 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,10 @@ def test_pad() -> None:
def test_pad_round() -> None:
im = Image.new("1", (1, 1), 1)
new_im = ImageOps.pad(im, (4, 1))
px = new_im.load()
assert px is not None
assert px[2, 0] == 1
assert new_im.getpixel((2, 0)) == 1

new_im = ImageOps.pad(im, (1, 4))
px = new_im.load()
assert px is not None
assert px[0, 2] == 1
assert new_im.getpixel((0, 2)) == 1


@pytest.mark.parametrize("mode", ("P", "PA"))
Expand Down
4 changes: 1 addition & 3 deletions Tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ def test_save_tiff_uint16() -> None:
a.shape = TEST_IMAGE_SIZE
img = Image.fromarray(a)

img_px = img.load()
assert img_px is not None
assert img_px[0, 0] == pixel_value
assert img.getpixel((0, 0)) == pixel_value


@pytest.mark.parametrize(
Expand Down

0 comments on commit ce1996d

Please sign in to comment.