From 4d0e8e74b4998422141e77525d9cfe30ccb7dc2e Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Sat, 10 Aug 2024 10:20:08 +1000 Subject: [PATCH] Use im property --- Tests/test_box_blur.py | 1 - Tests/test_color_lut.py | 9 ------ Tests/test_image.py | 1 - Tests/test_image_array.py | 1 - Tests/test_image_filter.py | 4 +-- Tests/test_image_getim.py | 1 - Tests/test_image_resample.py | 1 - Tests/test_image_resize.py | 5 --- Tests/test_imagemorph.py | 2 -- Tests/test_imageops_usm.py | 2 -- Tests/test_lib_image.py | 3 -- selftest.py | 2 +- src/PIL/BmpImagePlugin.py | 1 - src/PIL/EpsImagePlugin.py | 1 - src/PIL/GbrImagePlugin.py | 2 +- src/PIL/GifImagePlugin.py | 11 ++----- src/PIL/IcnsImagePlugin.py | 3 +- src/PIL/IcoImagePlugin.py | 2 +- src/PIL/ImImagePlugin.py | 1 - src/PIL/Image.py | 63 ++++++++++-------------------------- src/PIL/ImageChops.py | 17 ---------- src/PIL/ImageCms.py | 3 -- src/PIL/ImageFile.py | 4 +-- src/PIL/ImageFont.py | 1 - src/PIL/ImageMath.py | 5 --- src/PIL/ImageMorph.py | 4 --- src/PIL/ImageTk.py | 4 --- src/PIL/JpegImagePlugin.py | 1 - src/PIL/PcdImagePlugin.py | 3 -- src/PIL/PcxImagePlugin.py | 2 -- src/PIL/PngImagePlugin.py | 10 ++---- src/PIL/TgaImagePlugin.py | 2 -- src/PIL/TiffImagePlugin.py | 1 - src/PIL/WalImageFile.py | 2 +- 34 files changed, 30 insertions(+), 145 deletions(-) diff --git a/Tests/test_box_blur.py b/Tests/test_box_blur.py index dd5fe0290..1f6ed6127 100644 --- a/Tests/test_box_blur.py +++ b/Tests/test_box_blur.py @@ -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)) diff --git a/Tests/test_color_lut.py b/Tests/test_color_lut.py index 4b083d689..36ab187f2 100644 --- a/Tests/test_color_lut.py +++ b/Tests/test_color_lut.py @@ -47,7 +47,6 @@ class TestColorLut3DCoreAPI: 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( @@ -121,7 +120,6 @@ class TestColorLut3DCoreAPI: 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, @@ -143,7 +141,6 @@ class TestColorLut3DCoreAPI: ) -> 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, @@ -163,7 +160,6 @@ class TestColorLut3DCoreAPI: 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, @@ -180,7 +176,6 @@ class TestColorLut3DCoreAPI: 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]: @@ -217,7 +212,6 @@ class TestColorLut3DCoreAPI: g.transpose(Image.Transpose.ROTATE_180), ], ) - assert im.im is not None # Red channel copied to alpha assert_image_equal( @@ -242,7 +236,6 @@ class TestColorLut3DCoreAPI: g.transpose(Image.Transpose.ROTATE_270), ], ) - assert im.im is not None assert_image_equal( im, @@ -265,7 +258,6 @@ class TestColorLut3DCoreAPI: g.transpose(Image.Transpose.ROTATE_180), ], ) - assert im.im is not None # Reverse channels by splitting and using table # fmt: off @@ -291,7 +283,6 @@ class TestColorLut3DCoreAPI: 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, diff --git a/Tests/test_image.py b/Tests/test_image.py index d4ab8ad38..12b6c1fdd 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -695,7 +695,6 @@ class TestImage: 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 diff --git a/Tests/test_image_array.py b/Tests/test_image_array.py index 4d8ffbf4a..2b9ccd04e 100644 --- a/Tests/test_image_array.py +++ b/Tests/test_image_array.py @@ -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 diff --git a/Tests/test_image_filter.py b/Tests/test_image_filter.py index ea5438f3d..412ab44c3 100644 --- a/Tests/test_image_filter.py +++ b/Tests/test_image_filter.py @@ -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: diff --git a/Tests/test_image_getim.py b/Tests/test_image_getim.py index e62d6e0ec..9afa02b0a 100644 --- a/Tests/test_image_getim.py +++ b/Tests/test_image_getim.py @@ -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) diff --git a/Tests/test_image_resample.py b/Tests/test_image_resample.py index 4a5901d47..ce6209c0d 100644 --- a/Tests/test_image_resample.py +++ b/Tests/test_image_resample.py @@ -27,7 +27,6 @@ class TestImagingResampleVulnerability: ): 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: diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 5700b3e06..c9e304512 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -27,7 +27,6 @@ class TestImagingCoreResize: ) -> 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( @@ -38,8 +37,6 @@ class TestImagingCoreResize: 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: @@ -54,8 +51,6 @@ class TestImagingCoreResize: 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( diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index b40de7f98..4363f456e 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -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) diff --git a/Tests/test_imageops_usm.py b/Tests/test_imageops_usm.py index 64116bf70..920012d86 100644 --- a/Tests/test_imageops_usm.py +++ b/Tests/test_imageops_usm.py @@ -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 diff --git a/Tests/test_lib_image.py b/Tests/test_lib_image.py index 0a36af8b6..31548bbc9 100644 --- a/Tests/test_lib_image.py +++ b/Tests/test_lib_image.py @@ -7,21 +7,18 @@ from PIL import Image 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") diff --git a/selftest.py b/selftest.py index 9e049367e..e9b5689a0 100755 --- a/selftest.py +++ b/selftest.py @@ -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 diff --git a/src/PIL/BmpImagePlugin.py b/src/PIL/BmpImagePlugin.py index ce2ee464a..da9f2eba5 100644 --- a/src/PIL/BmpImagePlugin.py +++ b/src/PIL/BmpImagePlugin.py @@ -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: diff --git a/src/PIL/EpsImagePlugin.py b/src/PIL/EpsImagePlugin.py index c5ca8dadc..28aa9689d 100644 --- a/src/PIL/EpsImagePlugin.py +++ b/src/PIL/EpsImagePlugin.py @@ -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 diff --git a/src/PIL/GbrImagePlugin.py b/src/PIL/GbrImagePlugin.py index 3c8feea5f..f319d7e84 100644 --- a/src/PIL/GbrImagePlugin.py +++ b/src/PIL/GbrImagePlugin.py @@ -89,7 +89,7 @@ class GbrImageFile(ImageFile.ImageFile): 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) diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 55f927d40..74aebcb1f 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -155,7 +155,7 @@ class GifImageFile(ImageFile.ImageFile): if not self._seek_check(frame): return if frame < self.__frame: - self.im = None + self._im = None self._seek(0) last_frame = self.__frame @@ -307,7 +307,6 @@ class GifImageFile(ImageFile.ImageFile): 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 @@ -335,7 +334,6 @@ class GifImageFile(ImageFile.ImageFile): 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) @@ -381,7 +379,7 @@ class GifImageFile(ImageFile.ImageFile): 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: @@ -437,17 +435,15 @@ class GifImageFile(ImageFile.ImageFile): 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: @@ -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: diff --git a/src/PIL/IcnsImagePlugin.py b/src/PIL/IcnsImagePlugin.py index 58d4a90a0..ca66aa0fd 100644 --- a/src/PIL/IcnsImagePlugin.py +++ b/src/PIL/IcnsImagePlugin.py @@ -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} @@ -309,7 +308,7 @@ class IcnsImageFile(ImageFile.ImageFile): ) 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() diff --git a/src/PIL/IcoImagePlugin.py b/src/PIL/IcoImagePlugin.py index c891024f5..4613379b6 100644 --- a/src/PIL/IcoImagePlugin.py +++ b/src/PIL/IcoImagePlugin.py @@ -328,7 +328,7 @@ class IcoImageFile(ImageFile.ImageFile): 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) diff --git a/src/PIL/ImImagePlugin.py b/src/PIL/ImImagePlugin.py index 9d60011be..2fb7ecd52 100644 --- a/src/PIL/ImImagePlugin.py +++ b/src/PIL/ImImagePlugin.py @@ -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"" diff --git a/src/PIL/Image.py b/src/PIL/Image.py index 7532a49a2..5ec416081 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -535,7 +535,7 @@ class Image: def __init__(self) -> None: # FIXME: take "new" parameters / other image? # FIXME: turn mode and size into delegating properties? - self.im: core.ImagingCore | None = None + self._im: core.ImagingCore | DeferredError | None = None self._mode = "" self._size = (0, 0) self.palette: ImagePalette.ImagePalette | None = None @@ -543,6 +543,17 @@ class Image: self.readonly = 0 self._exif: Exif | None = None + @property + def im(self) -> core.ImagingCore: + if isinstance(self._im, DeferredError): + raise self._im.ex + assert self._im is not None + return self._im + + @im.setter + def im(self, im: core.ImagingCore) -> None: + self._im = im + @property def width(self) -> int: return self.size[0] @@ -617,12 +628,11 @@ class Image: # Instead of simply setting to None, we're setting up a # deferred error that will better explain that the core image # object is gone. - self.im = DeferredError(ValueError("Operation on closed image")) # type: ignore[assignment] + self._im = DeferredError(ValueError("Operation on closed image")) def _copy(self) -> None: self.load() - if self.im: - self.im = self.im.copy() + self.im = self.im.copy() self.readonly = 0 def _ensure_mutable(self) -> None: @@ -649,7 +659,6 @@ class Image: self.load() if not format or format == "PPM": - assert self.im is not None self.im.save_ppm(filename) else: self.save(filename, format, **options) @@ -798,7 +807,6 @@ class Image: # unpack data e = _getencoder(self.mode, encoder_name, encoder_args) - assert self.im is not None e.setimage(self.im) bufsize = max(65536, self.size[0] * 4) # see RawEncode.c @@ -865,7 +873,6 @@ class Image: # unpack data d = _getdecoder(self.mode, decoder_name, decoder_args) - assert self.im is not None d.setimage(self.im) s = d.decode(data) @@ -891,7 +898,7 @@ class Image: :returns: An image access object. :rtype: :py:class:`.PixelAccess` """ - if self.im is not None and self.palette and self.palette.dirty: + if self._im is not None and self.palette and self.palette.dirty: # realize palette mode, arr = self.palette.getdata() self.im.putpalette(self.palette.mode, mode, arr) @@ -908,7 +915,7 @@ class Image: self.palette.mode, self.palette.mode ) - if self.im is not None: + if self._im is not None: return self.im.pixel_access(self.readonly) return None @@ -979,7 +986,6 @@ class Image: deprecate(mode, 12) self.load() - assert self.im is not None has_transparency = "transparency" in self.info if not mode and self.mode == "P": @@ -1094,7 +1100,6 @@ class Image: new_im = self._new(im) from . import ImagePalette - assert new_im.im is not None new_im.palette = ImagePalette.ImagePalette( "RGB", new_im.im.getpalette("RGB") ) @@ -1226,7 +1231,6 @@ class Image: ) raise ValueError(msg) - assert self.im is not None if palette: # use palette from reference image palette.load() @@ -1265,7 +1269,6 @@ class Image: :returns: An :py:class:`~PIL.Image.Image` object. """ self.load() - assert self.im is not None return self._new(self.im.copy()) __copy__ = copy @@ -1294,7 +1297,6 @@ class Image: raise ValueError(msg) self.load() - assert self.im is not None return self._new(self._crop(self.im, box)) def _crop( @@ -1348,7 +1350,6 @@ class Image: if ymargin is None: ymargin = xmargin self.load() - assert self.im is not None return self._new(self.im.expand(xmargin, ymargin)) if TYPE_CHECKING: @@ -1373,7 +1374,6 @@ class Image: raise TypeError(msg) multiband = isinstance(filter, ImageFilter.MultibandFilter) - assert self.im is not None if self.im.bands == 1 or multiband: return self._new(filter.filter(self.im)) @@ -1409,7 +1409,6 @@ class Image: """ self.load() - assert self.im is not None return self.im.getbbox(alpha_only) def getcolors( @@ -1429,7 +1428,6 @@ class Image: """ self.load() - assert self.im is not None if self.mode in ("1", "L", "P"): h = self.im.histogram() out: list[tuple[int, float]] = [(h[i], i) for i in range(256) if h[i]] @@ -1457,7 +1455,6 @@ class Image: """ self.load() - assert self.im is not None if band is not None: return self.im.getband(band) return self.im # could be abused @@ -1473,7 +1470,6 @@ class Image: """ self.load() - assert self.im is not None if self.im.bands > 1: return tuple(self.im.getband(i).getextrema() for i in range(self.im.bands)) return self.im.getextrema() @@ -1612,7 +1608,6 @@ class Image: """ self.load() - assert self.im is not None return self.im.ptr def getpalette(self, rawmode: str | None = "RGB") -> list[int] | None: @@ -1629,7 +1624,6 @@ class Image: """ self.load() - assert self.im is not None try: mode = self.im.getpalettemode() except ValueError: @@ -1697,7 +1691,6 @@ class Image: """ self.load() - assert self.im is not None return self.im.getpixel(tuple(xy)) def getprojection(self) -> tuple[list[int], list[int]]: @@ -1709,7 +1702,6 @@ class Image: """ self.load() - assert self.im is not None x, y = self.im.getprojection() return list(x), list(y) @@ -1737,7 +1729,6 @@ class Image: :returns: A list containing pixel counts. """ self.load() - assert self.im is not None if mask: mask.load() return self.im.histogram((0, 0), mask.im) @@ -1766,7 +1757,6 @@ class Image: :returns: A float value representing the image entropy """ self.load() - assert self.im is not None if mask: mask.load() return self.im.entropy((0, 0), mask.im) @@ -1856,7 +1846,6 @@ class Image: if self.mode != "RGB" or im.mode not in ("LA", "RGBA", "RGBa"): # should use an adapter for this! im = im.convert(self.mode) - assert im.im is not None source = im.im elif isinstance(im, tuple): source = im @@ -1864,7 +1853,6 @@ class Image: source = cast(float, im) self._ensure_mutable() - assert self.im is not None if mask: mask.load() @@ -1960,7 +1948,6 @@ class Image: if isinstance(lut, ImagePointHandler): return lut.point(self) - assert self.im is not None if callable(lut): # if it isn't a list, it should be a function if self.mode in ("I", "I;16", "F"): @@ -1997,20 +1984,17 @@ class Image: if self.mode not in ("LA", "PA", "RGBA"): # attempt to promote self to a matching alpha mode - assert self.im is not None try: mode = getmodebase(self.mode) + "A" try: self.im.setmode(mode) except (AttributeError, ValueError) as e: # do things the hard way - assert self.im is not None im = self.im.convert(mode) if im.mode not in ("LA", "PA", "RGBA"): msg = "alpha channel could not be added" raise ValueError(msg) from e # sanity check self.im = im - assert self.im is not None self._mode = self.im.mode except KeyError as e: msg = "illegal image mode" @@ -2021,7 +2005,6 @@ class Image: else: band = 3 - assert self.im is not None if isImageType(alpha): # alpha layer if alpha.mode not in ("1", "L"): @@ -2064,7 +2047,6 @@ class Image: self._ensure_mutable() - assert self.im is not None self.im.putdata(data, scale, offset) def putpalette( @@ -2150,7 +2132,6 @@ class Image: assert self.palette is not None palette_index = self.palette.getcolor(tuple(value), self) value = (palette_index, alpha) if self.mode == "PA" else palette_index - assert self.im is not None return self.im.putpixel(xy, value) def remap_palette( @@ -2177,7 +2158,6 @@ class Image: if source_palette is None: if self.mode == "P": self.load() - assert self.im is not None palette_mode = self.im.getpalettemode() if palette_mode == "RGBA": bands = 4 @@ -2225,7 +2205,6 @@ class Image: # m_im.putpalette(mapping_palette, 'L') # converts to 'P' # or just force it. # UNDONE -- this is part of the general issue with palettes - assert m_im.im is not None m_im.im.putpalette(palette_mode, palette_mode + ";L", m_im.palette.tobytes()) m_im = m_im.convert("L") @@ -2371,7 +2350,6 @@ class Image: (box[3] - reduce_box[1]) / factor_y, ) - assert self.im is not None return self._new(self.im.resize(size, resample, box)) def reduce( @@ -2406,7 +2384,7 @@ class Image: return im.convert(self.mode) self.load() - assert self.im is not None + return self._new(self.im.reduce(factor, box)) def rotate( @@ -2684,7 +2662,6 @@ class Image: """ self.load() - assert self.im is not None if self.im.bands == 1: return (self.copy(),) return tuple(map(self._new, self.im.split())) @@ -2709,7 +2686,6 @@ class Image: msg = f'The image has no channel "{channel}"' raise ValueError(msg) from e - assert self.im is not None return self._new(self.im.getband(channel)) def tell(self) -> int: @@ -2814,7 +2790,6 @@ class Image: self.im = im.im self._size = final_size - assert self.im is not None self._mode = self.im.mode self.readonly = 0 @@ -2999,7 +2974,6 @@ class Image: if image.mode in ("1", "P"): resample = Resampling.NEAREST - assert self.im is not None self.im.transform(box, image.im, method, data, resample, fill) def transpose(self, method: Transpose) -> Image: @@ -3014,7 +2988,6 @@ class Image: """ self.load() - assert self.im is not None return self._new(self.im.transpose(method)) def effect_spread(self, distance: int) -> Image: @@ -3024,7 +2997,6 @@ class Image: :param distance: Distance to spread pixels. """ self.load() - assert self.im is not None return self._new(self.im.effect_spread(distance)) def toqimage(self): @@ -3258,7 +3230,6 @@ def frombuffer( if mode == "P": from . import ImagePalette - assert im.im is not None im.palette = ImagePalette.ImagePalette("RGB", im.im.getpalette("RGB")) im.readonly = 1 return im diff --git a/src/PIL/ImageChops.py b/src/PIL/ImageChops.py index b1b616d5c..29a5c995f 100644 --- a/src/PIL/ImageChops.py +++ b/src/PIL/ImageChops.py @@ -48,7 +48,6 @@ def invert(image: Image.Image) -> Image.Image: """ image.load() - assert image.im is not None return image._new(image.im.chop_invert()) @@ -64,7 +63,6 @@ def lighter(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_lighter(image2.im)) @@ -80,7 +78,6 @@ def darker(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_darker(image2.im)) @@ -96,7 +93,6 @@ def difference(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_difference(image2.im)) @@ -114,7 +110,6 @@ def multiply(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_multiply(image2.im)) @@ -129,7 +124,6 @@ def screen(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_screen(image2.im)) @@ -142,7 +136,6 @@ def soft_light(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_soft_light(image2.im)) @@ -155,7 +148,6 @@ def hard_light(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_hard_light(image2.im)) @@ -168,7 +160,6 @@ def overlay(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_overlay(image2.im)) @@ -186,7 +177,6 @@ def add( image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_add(image2.im, scale, offset)) @@ -204,7 +194,6 @@ def subtract( image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_subtract(image2.im, scale, offset)) @@ -218,7 +207,6 @@ def add_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_add_modulo(image2.im)) @@ -232,7 +220,6 @@ def subtract_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_subtract_modulo(image2.im)) @@ -251,7 +238,6 @@ def logical_and(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_and(image2.im)) @@ -267,7 +253,6 @@ def logical_or(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_or(image2.im)) @@ -283,7 +268,6 @@ def logical_xor(image1: Image.Image, image2: Image.Image) -> Image.Image: image1.load() image2.load() - assert image1.im is not None return image1._new(image1.im.chop_xor(image2.im)) @@ -324,5 +308,4 @@ def offset(image: Image.Image, xoffset: int, yoffset: int | None = None) -> Imag if yoffset is None: yoffset = xoffset image.load() - assert image.im is not None return image._new(image.im.offset(xoffset, yoffset)) diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 3a6d52949..ec10230f1 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -352,8 +352,6 @@ class ImageCmsTransform(Image.ImagePointHandler): im.load() if imOut is None: imOut = Image.new(self.output_mode, im.size, None) - assert im.im is not None - assert imOut.im is not None self.transform.apply(im.im.id, imOut.im.id) imOut.info["icc_profile"] = self.output_profile.tobytes() return imOut @@ -363,7 +361,6 @@ class ImageCmsTransform(Image.ImagePointHandler): if im.mode != self.output_mode: msg = "mode mismatch" raise ValueError(msg) # wrong output mode - assert im.im is not None self.transform.apply(im.im.id, im.im.id) im.info["icc_profile"] = self.output_profile.tobytes() return im diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index babf453cb..24b3a9eb1 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -315,7 +315,7 @@ class ImageFile(Image.Image): def load_prepare(self) -> None: # create image memory if necessary - if not self.im or self.im.mode != self.mode or self.im.size != self.size: + if self._im is None or self.im.mode != self.mode or self.im.size != self.size: self.im = Image.core.new(self.mode, self.size) # create palette (optional) if self.mode == "P": @@ -475,7 +475,6 @@ class Parser: d, e, o, a = im.tile[0] im.tile = [] self.decoder = Image._getdecoder(im.mode, d, a, im.decoderconfig) - assert im.im is not None self.decoder.setimage(im.im, e) # calculate decoder offset @@ -567,7 +566,6 @@ def _encode_tile( fp.seek(offset) encoder = Image._getencoder(im.mode, encoder_name, args, im.encoderconfig) try: - assert im.im is not None encoder.setimage(im.im, extents) if encoder.pushes_fd: encoder.setfd(fp) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 2d71b4df4..2ab65bfef 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -142,7 +142,6 @@ class ImageFont: raise TypeError(msg) image.load() - assert image.im is not None self.font = Image.core.font(image.im, data) diff --git a/src/PIL/ImageMath.py b/src/PIL/ImageMath.py index c8f0406ed..191cc2a5f 100644 --- a/src/PIL/ImageMath.py +++ b/src/PIL/ImageMath.py @@ -65,8 +65,6 @@ class _Operand: except AttributeError as e: msg = f"bad operand type for '{op}'" raise TypeError(msg) from e - assert out.im is not None - assert im_1.im is not None _imagingmath.unop(op, out.im.id, im_1.im.id) else: # binary operation @@ -95,9 +93,6 @@ class _Operand: except AttributeError as e: msg = f"bad operand type for '{op}'" raise TypeError(msg) from e - assert out.im is not None - assert im_1.im is not None - assert im_2.im is not None _imagingmath.binop(op, out.im.id, im_1.im.id, im_2.im.id) return _Operand(out) diff --git a/src/PIL/ImageMorph.py b/src/PIL/ImageMorph.py index cd4fa3cfe..6a43983d3 100644 --- a/src/PIL/ImageMorph.py +++ b/src/PIL/ImageMorph.py @@ -213,8 +213,6 @@ class MorphOp: msg = "Image mode must be L" raise ValueError(msg) outimage = Image.new(image.mode, image.size, None) - assert image.im is not None - assert outimage.im is not None count = _imagingmorph.apply(bytes(self.lut), image.im.id, outimage.im.id) return count, outimage @@ -231,7 +229,6 @@ class MorphOp: if image.mode != "L": msg = "Image mode must be L" raise ValueError(msg) - assert image.im is not None return _imagingmorph.match(bytes(self.lut), image.im.id) def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]: @@ -243,7 +240,6 @@ class MorphOp: if image.mode != "L": msg = "Image mode must be L" raise ValueError(msg) - assert image.im is not None return _imagingmorph.get_on_pixels(image.im.id) def load_lut(self, filename: str) -> None: diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index 6344220ef..a788c9d1f 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -43,7 +43,6 @@ def _pilbitmap_check() -> int: if _pilbitmap_ok is None: try: im = Image.new("1", (1, 1)) - assert im.im is not None tkinter.BitmapImage(data=f"PIL:{im.im.id}") _pilbitmap_ok = 1 except tkinter.TclError: @@ -188,7 +187,6 @@ class PhotoImage: # convert to blittable im.load() image = im.im - assert image is not None if image.isblock() and im.mode == self.__mode: block = image else: @@ -230,7 +228,6 @@ class BitmapImage: if _pilbitmap_check(): # fast way (requires the pilbitmap booster patch) image.load() - assert image.im is not None kw["data"] = f"PIL:{image.im.id}" self.__im = image # must keep a reference else: @@ -277,7 +274,6 @@ def getimage(photo: PhotoImage) -> Image.Image: """Copies the contents of a PhotoImage to a PIL image memory.""" im = Image.new("RGBA", (photo.width(), photo.height())) block = im.im - assert block is not None _pyimagingtkcall("PyImagingPhotoGet", photo, block.id) diff --git a/src/PIL/JpegImagePlugin.py b/src/PIL/JpegImagePlugin.py index 04fe8ba85..fc897e2b9 100644 --- a/src/PIL/JpegImagePlugin.py +++ b/src/PIL/JpegImagePlugin.py @@ -473,7 +473,6 @@ class JpegImageFile(ImageFile.ImageFile): except OSError: pass - assert self.im is not None self._mode = self.im.mode self._size = self.im.size diff --git a/src/PIL/PcdImagePlugin.py b/src/PIL/PcdImagePlugin.py index 64898e235..d9970405c 100644 --- a/src/PIL/PcdImagePlugin.py +++ b/src/PIL/PcdImagePlugin.py @@ -52,10 +52,7 @@ class PcdImageFile(ImageFile.ImageFile): def load_end(self) -> None: if self.tile_post_rotate: # Handle rotated PCDs - assert self.im is not None - self.im = self.im.rotate(self.tile_post_rotate) - assert self.im is not None self._size = self.im.size diff --git a/src/PIL/PcxImagePlugin.py b/src/PIL/PcxImagePlugin.py index dd42003b5..2b8ced8af 100644 --- a/src/PIL/PcxImagePlugin.py +++ b/src/PIL/PcxImagePlugin.py @@ -202,8 +202,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: if im.mode == "P": # colour palette - assert im.im is not None - fp.write(o8(12)) palette = im.im.getpalette("RGB", "RGB") palette += b"\x00" * (768 - len(palette)) diff --git a/src/PIL/PngImagePlugin.py b/src/PIL/PngImagePlugin.py index 59b5d0a8b..a0ac24571 100644 --- a/src/PIL/PngImagePlugin.py +++ b/src/PIL/PngImagePlugin.py @@ -869,7 +869,7 @@ class PngImageFile(ImageFile.ImageFile): self._fp.seek(self.__rewind) self.png.rewind() self.__prepare_idat = self.__rewind_idat - self.im = None + self._im = None self.info = self.png.im_info self.tile = self.png.im_tile self.fp = self._fp @@ -887,7 +887,6 @@ class PngImageFile(ImageFile.ImageFile): # ensure previous frame was loaded self.load() - assert self.im is not None if self.dispose: self.im.paste(self.dispose, self.dispose_extent) @@ -1050,7 +1049,6 @@ class PngImageFile(ImageFile.ImageFile): self.png = None else: if self._prev_im and self.blend_op == Blend.OP_OVER: - assert self.im is not None updated = self._crop(self.im, self.dispose_extent) if self.im.mode == "RGB" and "transparency" in self.info: mask = updated.convert_transparent( @@ -1405,7 +1403,6 @@ def _save( chunk(fp, cid, data) if im.mode == "P": - assert im.im is not None palette_byte_number = colors * 3 palette_bytes = im.im.getpalette("RGB")[:palette_byte_number] while len(palette_bytes) < palette_byte_number: @@ -1436,9 +1433,8 @@ def _save( # and it's in the info dict. It's probably just stale. msg = "cannot use transparency for this mode" raise OSError(msg) - elif im.mode == "P": - assert im.im is not None - if im.im.getpalettemode() == "RGBA": + else: + if im.mode == "P" and im.im.getpalettemode() == "RGBA": alpha = im.im.getpalette("RGBA", "A") alpha_bytes = colors chunk(fp, b"tRNS", alpha[:alpha_bytes]) diff --git a/src/PIL/TgaImagePlugin.py b/src/PIL/TgaImagePlugin.py index 39104aece..c2a0ee4e9 100644 --- a/src/PIL/TgaImagePlugin.py +++ b/src/PIL/TgaImagePlugin.py @@ -158,7 +158,6 @@ class TgaImageFile(ImageFile.ImageFile): def load_end(self) -> None: if self._flip_horizontally: - assert self.im is not None self.im = self.im.transpose(Image.Transpose.FLIP_LEFT_RIGHT) @@ -200,7 +199,6 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None: warnings.warn("id_section has been trimmed to 255 characters") if colormaptype: - assert im.im is not None palette = im.im.getpalette("RGB", "BGR") colormaplength, colormapentry = len(palette) // 3, 24 else: diff --git a/src/PIL/TiffImagePlugin.py b/src/PIL/TiffImagePlugin.py index 2b53c7dfa..d9d1bab5a 100644 --- a/src/PIL/TiffImagePlugin.py +++ b/src/PIL/TiffImagePlugin.py @@ -1313,7 +1313,6 @@ class TiffImageFile(ImageFile.ImageFile): decoder = Image._getdecoder( self.mode, "libtiff", tuple(args), self.decoderconfig ) - assert self.im is not None try: decoder.setimage(self.im, extents) except ValueError as e: diff --git a/src/PIL/WalImageFile.py b/src/PIL/WalImageFile.py index ec5c74900..87e32878b 100644 --- a/src/PIL/WalImageFile.py +++ b/src/PIL/WalImageFile.py @@ -54,7 +54,7 @@ class WalImageFile(ImageFile.ImageFile): self.info["next_name"] = next_name 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.size[0] * self.size[1])) self.putpalette(quake2palette)