From 1f3fe6f73350aa9630b236a9f16370aa0c71258d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Mon, 16 Sep 2024 10:21:17 +1000 Subject: [PATCH] Use getim() --- Tests/test_imagemorph.py | 8 ++++---- src/PIL/ImageCms.py | 4 ++-- src/PIL/ImageMath.py | 5 ++--- src/PIL/ImageMorph.py | 6 +++--- src/PIL/ImageTk.py | 2 +- src/PIL/_imagingcms.pyi | 4 +++- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/Tests/test_imagemorph.py b/Tests/test_imagemorph.py index 04860fd616c..9da32aa0124 100644 --- a/Tests/test_imagemorph.py +++ b/Tests/test_imagemorph.py @@ -328,13 +328,13 @@ def test_wrong_mode() -> None: iml = Image.new("L", (10, 10)) with pytest.raises(RuntimeError): - _imagingmorph.apply(bytes(lut), imrgb.im.ptr, iml.im.ptr) + _imagingmorph.apply(bytes(lut), imrgb.getim(), iml.getim()) with pytest.raises(RuntimeError): - _imagingmorph.apply(bytes(lut), iml.im.ptr, imrgb.im.ptr) + _imagingmorph.apply(bytes(lut), iml.getim(), imrgb.getim()) with pytest.raises(RuntimeError): - _imagingmorph.match(bytes(lut), imrgb.im.ptr) + _imagingmorph.match(bytes(lut), imrgb.getim()) # Should not raise - _imagingmorph.match(bytes(lut), iml.im.ptr) + _imagingmorph.match(bytes(lut), iml.getim()) diff --git a/src/PIL/ImageCms.py b/src/PIL/ImageCms.py index 5e256b0ab7f..f028f177533 100644 --- a/src/PIL/ImageCms.py +++ b/src/PIL/ImageCms.py @@ -352,7 +352,7 @@ def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Imag im.load() if imOut is None: imOut = Image.new(self.output_mode, im.size, None) - self.transform.apply(im.im.ptr, imOut.im.ptr) + self.transform.apply(im.getim(), imOut.getim()) imOut.info["icc_profile"] = self.output_profile.tobytes() return imOut @@ -361,7 +361,7 @@ def apply_in_place(self, im: Image.Image) -> Image.Image: if im.mode != self.output_mode: msg = "mode mismatch" raise ValueError(msg) # wrong output mode - self.transform.apply(im.im.ptr, im.im.ptr) + self.transform.apply(im.getim(), im.getim()) im.info["icc_profile"] = self.output_profile.tobytes() return im diff --git a/src/PIL/ImageMath.py b/src/PIL/ImageMath.py index b32742657ba..ff3014a8d5a 100644 --- a/src/PIL/ImageMath.py +++ b/src/PIL/ImageMath.py @@ -65,7 +65,7 @@ def apply( except AttributeError as e: msg = f"bad operand type for '{op}'" raise TypeError(msg) from e - _imagingmath.unop(op, out.im.ptr, im_1.im.ptr) + _imagingmath.unop(op, out.getim(), im_1.getim()) else: # binary operation im_2 = self.__fixup(im2) @@ -87,13 +87,12 @@ def apply( im_2 = im_2.crop((0, 0) + size) out = Image.new(mode or im_1.mode, im_1.size, None) im_1.load() - im_2.load() try: op = getattr(_imagingmath, f"{op}_{im_1.mode}") except AttributeError as e: msg = f"bad operand type for '{op}'" raise TypeError(msg) from e - _imagingmath.binop(op, out.im.ptr, im_1.im.ptr, im_2.im.ptr) + _imagingmath.binop(op, out.getim(), im_1.getim(), im_2.getim()) return _Operand(out) # unary operators diff --git a/src/PIL/ImageMorph.py b/src/PIL/ImageMorph.py index bc7ed66d3fb..f0a066b5bd8 100644 --- a/src/PIL/ImageMorph.py +++ b/src/PIL/ImageMorph.py @@ -213,7 +213,7 @@ def apply(self, image: Image.Image) -> tuple[int, Image.Image]: msg = "Image mode must be L" raise ValueError(msg) outimage = Image.new(image.mode, image.size, None) - count = _imagingmorph.apply(bytes(self.lut), image.im.ptr, outimage.im.ptr) + count = _imagingmorph.apply(bytes(self.lut), image.getim(), outimage.getim()) return count, outimage def match(self, image: Image.Image) -> list[tuple[int, int]]: @@ -229,7 +229,7 @@ def match(self, image: Image.Image) -> list[tuple[int, int]]: if image.mode != "L": msg = "Image mode must be L" raise ValueError(msg) - return _imagingmorph.match(bytes(self.lut), image.im.ptr) + return _imagingmorph.match(bytes(self.lut), image.getim()) def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]: """Get a list of all turned on pixels in a binary image @@ -240,7 +240,7 @@ def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]: if image.mode != "L": msg = "Image mode must be L" raise ValueError(msg) - return _imagingmorph.get_on_pixels(image.im.ptr) + return _imagingmorph.get_on_pixels(image.getim()) def load_lut(self, filename: str) -> None: """Load an operator from an mrl file""" diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index d01af364bb5..c36cf9b8473 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -259,7 +259,7 @@ 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())) - _pyimagingtkcall("PyImagingPhotoGet", photo, im.im.ptr) + _pyimagingtkcall("PyImagingPhotoGet", photo, im.getim()) return im diff --git a/src/PIL/_imagingcms.pyi b/src/PIL/_imagingcms.pyi index 2abd6d0f7a9..ddcf93ab1eb 100644 --- a/src/PIL/_imagingcms.pyi +++ b/src/PIL/_imagingcms.pyi @@ -2,6 +2,8 @@ import datetime import sys from typing import Literal, SupportsFloat, TypedDict +from ._typing import CapsuleType + littlecms_version: str | None _Tuple3f = tuple[float, float, float] @@ -108,7 +110,7 @@ class CmsProfile: def is_intent_supported(self, intent: int, direction: int, /) -> int: ... class CmsTransform: - def apply(self, id_in: int, id_out: int) -> int: ... + def apply(self, id_in: CapsuleType, id_out: CapsuleType) -> int: ... def profile_open(profile: str, /) -> CmsProfile: ... def profile_frombytes(profile: bytes, /) -> CmsProfile: ...