Skip to content

Commit

Permalink
Use im property
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Aug 10, 2024
1 parent 8da4883 commit 4d0e8e7
Show file tree
Hide file tree
Showing 34 changed files with 30 additions and 145 deletions.
1 change: 0 additions & 1 deletion Tests/test_box_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_imageops_box_blur() -> None:


def box_blur(image: Image.Image, radius: float = 1, n: int = 1) -> Image.Image:
assert image.im is not None
return image._new(image.im.box_blur((radius, radius), n))


Expand Down
9 changes: 0 additions & 9 deletions Tests/test_color_lut.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ def generate_identity_table(

def test_wrong_args(self) -> None:
im = Image.new("RGB", (10, 10), 0)
assert im.im is not None

with pytest.raises(ValueError, match="filter"):
im.im.color_lut_3d(
Expand Down Expand Up @@ -121,7 +120,6 @@ def test_correct_args(
self, lut_mode: str, table_channels: int, table_size: int | tuple[int, int, int]
) -> None:
im = Image.new("RGB", (10, 10), 0)
assert im.im is not None
im.im.color_lut_3d(
lut_mode,
Image.Resampling.BILINEAR,
Expand All @@ -143,7 +141,6 @@ def test_wrong_mode(
) -> None:
with pytest.raises(ValueError, match="wrong mode"):
im = Image.new(image_mode, (10, 10), 0)
assert im.im is not None
im.im.color_lut_3d(
lut_mode,
Image.Resampling.BILINEAR,
Expand All @@ -163,7 +160,6 @@ def test_correct_mode(
self, image_mode: str, lut_mode: str, table_channels: int, table_size: int
) -> None:
im = Image.new(image_mode, (10, 10), 0)
assert im.im is not None
im.im.color_lut_3d(
lut_mode,
Image.Resampling.BILINEAR,
Expand All @@ -180,7 +176,6 @@ def test_identities(self) -> None:
g.transpose(Image.Transpose.ROTATE_180),
],
)
assert im.im is not None

# Fast test with small cubes
for size in [2, 3, 5, 7, 11, 16, 17]:
Expand Down Expand Up @@ -217,7 +212,6 @@ def test_identities_4_channels(self) -> None:
g.transpose(Image.Transpose.ROTATE_180),
],
)
assert im.im is not None

# Red channel copied to alpha
assert_image_equal(
Expand All @@ -242,7 +236,6 @@ def test_copy_alpha_channel(self) -> None:
g.transpose(Image.Transpose.ROTATE_270),
],
)
assert im.im is not None

assert_image_equal(
im,
Expand All @@ -265,7 +258,6 @@ def test_channels_order(self) -> None:
g.transpose(Image.Transpose.ROTATE_180),
],
)
assert im.im is not None

# Reverse channels by splitting and using table
# fmt: off
Expand All @@ -291,7 +283,6 @@ def test_overflow(self) -> None:
g.transpose(Image.Transpose.ROTATE_180),
],
)
assert im.im is not None

# fmt: off
transformed = im._new(im.im.color_lut_3d('RGB', Image.Resampling.BILINEAR,
Expand Down
1 change: 0 additions & 1 deletion Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ def _make_new(
image: Image.Image,
palette_result: ImagePalette.ImagePalette | None = None,
) -> None:
assert image.im is not None
new_image = base_image._new(image.im)
assert new_image.mode == image.mode
assert new_image.size == image.size
Expand Down
1 change: 0 additions & 1 deletion Tests/test_image_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,4 @@ def test_fromarray_palette() -> None:

# Assert that the Python and C palettes match
assert out.palette is not None
assert out.im is not None
assert len(out.palette.colors) == len(out.im.getpalette()) / 3
4 changes: 1 addition & 3 deletions Tests/test_image_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ def test_builtinfilter_p() -> None:
builtin_filter = ImageFilter.BuiltinFilter()

with pytest.raises(ValueError):
im = hopper("P").im
assert im is not None
builtin_filter.filter(im)
builtin_filter.filter(hopper("P").im)


def test_kernel_not_enough_coefficients() -> None:
Expand Down
1 change: 0 additions & 1 deletion Tests/test_image_getim.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ def test_sanity() -> None:
type_repr = repr(type(im.getim()))

assert "PyCapsule" in type_repr
assert im.im is not None
assert isinstance(im.im.id, int)
1 change: 0 additions & 1 deletion Tests/test_image_resample.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def test_overflow(self) -> None:
):
with pytest.raises(MemoryError):
# any resampling filter will do here
assert im.im is not None
im.im.resize((xsize, ysize), Image.Resampling.BILINEAR)

def test_invalid_size(self) -> None:
Expand Down
5 changes: 0 additions & 5 deletions Tests/test_image_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def resize(
) -> Image.Image:
# Image class independent version of resize.
im.load()
assert im.im is not None
return im._new(im.im.resize(size, f))

@pytest.mark.parametrize(
Expand All @@ -38,8 +37,6 @@ def test_nearest_mode(self, mode: str) -> None:
r = self.resize(im, (15, 12), Image.Resampling.NEAREST)
assert r.mode == mode
assert r.size == (15, 12)
assert r.im is not None
assert im.im is not None
assert r.im.bands == im.im.bands

def test_convolution_modes(self) -> None:
Expand All @@ -54,8 +51,6 @@ def test_convolution_modes(self) -> None:
r = self.resize(im, (15, 12), Image.Resampling.BILINEAR)
assert r.mode == mode
assert r.size == (15, 12)
assert r.im is not None
assert im.im is not None
assert r.im.bands == im.im.bands

@pytest.mark.parametrize(
Expand Down
2 changes: 0 additions & 2 deletions Tests/test_imagemorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,6 @@ def test_wrong_mode() -> None:
lut = ImageMorph.LutBuilder(op_name="corner").build_lut()
imrgb = Image.new("RGB", (10, 10))
iml = Image.new("L", (10, 10))
assert imrgb.im is not None
assert iml.im is not None

with pytest.raises(RuntimeError):
_imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id)
Expand Down
2 changes: 0 additions & 2 deletions Tests/test_imageops_usm.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@ def test_blur_accuracy(test_images: dict[str, ImageFile.ImageFile]) -> None:
(4, 3, 2),
(4, 2, 2),
]:
assert i.im is not None
assert i.im.getpixel((x, y))[c] >= 250
# Fuzzy match.

def gp(x: int, y: int) -> tuple[int, ...]:
assert i.im is not None
return i.im.getpixel((x, y))

assert 236 <= gp(7, 4)[0] <= 239
Expand Down
3 changes: 0 additions & 3 deletions Tests/test_lib_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@

def test_setmode() -> None:
im = Image.new("L", (1, 1), 255)
assert im.im is not None
im.im.setmode("1")
assert im.im.getpixel((0, 0)) == 255
im.im.setmode("L")
assert im.im.getpixel((0, 0)) == 255

im = Image.new("1", (1, 1), 1)
assert im.im is not None
im.im.setmode("L")
assert im.im.getpixel((0, 0)) == 255
im.im.setmode("1")
assert im.im.getpixel((0, 0)) == 255

im = Image.new("RGB", (1, 1), (1, 2, 3))
assert im.im is not None
im.im.setmode("RGB")
assert im.im.getpixel((0, 0)) == (1, 2, 3)
im.im.setmode("RGBA")
Expand Down
2 changes: 1 addition & 1 deletion selftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def testimage() -> None:
or you call the "load" method:
>>> im = Image.open("Tests/images/hopper.ppm")
>>> print(im.im) # internal image attribute
>>> print(im._im) # internal image attribute
None
>>> a = im.load()
>>> type(im.im) # doctest: +ELLIPSIS
Expand Down
1 change: 0 additions & 1 deletion src/PIL/BmpImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ def _save(
elif im.mode == "L":
palette = b"".join(o8(i) * 4 for i in range(256))
elif im.mode == "P":
assert im.im is not None
palette = im.im.getpalette("RGB", "BGRX")
colors = len(palette) // 4
else:
Expand Down
1 change: 0 additions & 1 deletion src/PIL/EpsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def Ghostscript(
except OSError:
pass

assert out_im.im is not None
im = out_im.im.copy()
out_im.close()
return im
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/GbrImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def _open(self) -> None:
self._data_size = width * height * color_depth

def load(self) -> Image.core.PixelAccess | None:
if not self.im:
if self._im is None:
self.im = Image.core.new(self.mode, self.size)
self.frombytes(self.fp.read(self._data_size))
return Image.Image.load(self)
Expand Down
11 changes: 3 additions & 8 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def seek(self, frame: int) -> None:
if not self._seek_check(frame):
return
if frame < self.__frame:
self.im = None
self._im = None
self._seek(0)

last_frame = self.__frame
Expand Down Expand Up @@ -307,7 +307,6 @@ def _seek(self, frame: int, update_image: bool = True) -> None:
self.tile = []

if self.dispose:
assert self.im is not None
self.im.paste(self.dispose, self.dispose_extent)

self._frame_palette = palette if palette is not None else self.global_palette
Expand Down Expand Up @@ -335,7 +334,6 @@ def _seek(self, frame: int, update_image: bool = True) -> None:
LOADING_STRATEGY != LoadingStrategy.RGB_AFTER_DIFFERENT_PALETTE_ONLY
or palette
):
assert self.im is not None
if "transparency" in self.info:
self.im.putpalettealpha(self.info["transparency"], 0)
self.im = self.im.convert("RGBA", Image.Dither.FLOYDSTEINBERG)
Expand Down Expand Up @@ -381,7 +379,7 @@ def _rgb(color: int) -> tuple[int, int, int]:
self.dispose = Image.core.fill(dispose_mode, dispose_size, color)
else:
# replace with previous contents
if self.im is not None:
if self._im is not None:
# only dispose the extent in this frame
self.dispose = self._crop(self.im, self.dispose_extent)
elif frame_transparency is not None:
Expand Down Expand Up @@ -437,17 +435,15 @@ def load_prepare(self) -> None:
self._prev_im = self.im
if self._frame_palette:
self.im = Image.core.fill("P", self.size, self._frame_transparency or 0)
assert self.im is not None
self.im.putpalette("RGB", *self._frame_palette.getdata())
else:
self.im = None
self._im = None
self._mode = temp_mode
self._frame_palette = None

super().load_prepare()

def load_end(self) -> None:
assert self.im is not None
if self.__frame == 0:
if self.mode == "P" and LOADING_STRATEGY == LoadingStrategy.RGB_ALWAYS:
if self._frame_transparency is not None:
Expand Down Expand Up @@ -539,7 +535,6 @@ def _normalize_palette(

if im.mode == "P":
if not source_palette:
assert im.im is not None
source_palette = im.im.getpalette("RGB")[:768]
else: # L-mode
if not source_palette:
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/IcnsImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ def read_32(
msg = f"Error reading channel [{repr(bytesleft)} left]"
raise SyntaxError(msg)
band = Image.frombuffer("L", pixel_size, b"".join(data), "raw", "L", 0, 1)
assert im.im is not None
im.im.putband(band.im, band_ix)
return {"RGB": im}

Expand Down Expand Up @@ -309,7 +308,7 @@ def load(self) -> Image.core.PixelAccess | None:
)

px = Image.Image.load(self)
if self.im is not None and self.im.size == self.size:
if self._im is not None and self.im.size == self.size:
# Already loaded
return px
self.load_prepare()
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/IcoImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def size(self, value):
self._size = value

def load(self) -> Image.core.PixelAccess | None:
if self.im is not None and self.im.size == self.size:
if self._im is not None and self.im.size == self.size:
# Already loaded
return Image.Image.load(self)
im = self.ico.getimage(self.size)
Expand Down
1 change: 0 additions & 1 deletion src/PIL/ImImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
fp.write(b"Lut: 1\r\n")
fp.write(b"\000" * (511 - fp.tell()) + b"\032")
if im.mode in ["P", "PA"]:
assert im.im is not None
im_palette = im.im.getpalette("RGB", "RGB;L")
colors = len(im_palette) // 3
palette = b""
Expand Down
Loading

0 comments on commit 4d0e8e7

Please sign in to comment.