Skip to content

Commit

Permalink
Merge pull request #145 from radarhere/use-ptr
Browse files Browse the repository at this point in the history
Use getim()
  • Loading branch information
homm authored Sep 16, 2024
2 parents 6921f83 + 1f3fe6f commit 3b09f43
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Tests/test_imagemorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
4 changes: 2 additions & 2 deletions src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
5 changes: 2 additions & 3 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/ImageMorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand All @@ -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
Expand All @@ -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"""
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/ImageTk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/PIL/_imagingcms.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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: ...
Expand Down

0 comments on commit 3b09f43

Please sign in to comment.