-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Use ImagingCore.ptr instead of ImagingCore.id #8341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 36 commits
2303c06
a9798e7
2fcab26
f246be7
7435a06
c69ad03
920c4ac
147f75e
bf11639
8833548
8dcf229
6f9128b
fe002a7
56bc6a1
f916b5d
7f48567
5428e35
cb3a4e6
ee65b30
882ac78
934ae12
d29fa73
4318834
bd14915
a2988da
6921f83
1f3fe6f
3b09f43
d8ef314
bc97369
af521a1
aa22b24
5d430ea
9f409e8
87414b3
11bcd5a
b9d1768
8e332eb
a227f22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
from __future__ import annotations | ||
|
||
import pytest | ||
|
||
from .helper import hopper | ||
|
||
|
||
def test_sanity() -> None: | ||
im = hopper() | ||
type_repr = repr(type(im.getim())) | ||
|
||
type_repr = repr(type(im.getim())) | ||
assert "PyCapsule" in type_repr | ||
assert isinstance(im.im.id, int) | ||
|
||
with pytest.warns(DeprecationWarning): | ||
assert isinstance(im.im.id, int) | ||
|
||
with pytest.warns(DeprecationWarning): | ||
ptrs = dict(im.im.unsafe_ptrs) | ||
assert all(k in ptrs for k in ["image8", "image32", "image"]) | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,23 +32,12 @@ | |
|
||
from . import Image, ImageFile | ||
|
||
if TYPE_CHECKING: | ||
from ._typing import CapsuleType | ||
|
||
# -------------------------------------------------------------------- | ||
# Check for Tkinter interface hooks | ||
|
||
_pilbitmap_ok = None | ||
|
||
|
||
def _pilbitmap_check() -> int: | ||
global _pilbitmap_ok | ||
if _pilbitmap_ok is None: | ||
try: | ||
im = Image.new("1", (1, 1)) | ||
tkinter.BitmapImage(data=f"PIL:{im.im.id}") | ||
_pilbitmap_ok = 1 | ||
except tkinter.TclError: | ||
_pilbitmap_ok = 0 | ||
return _pilbitmap_ok | ||
|
||
|
||
def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None: | ||
source = None | ||
|
@@ -62,18 +51,18 @@ def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None: | |
|
||
|
||
def _pyimagingtkcall( | ||
command: str, photo: PhotoImage | tkinter.PhotoImage, id: int | ||
command: str, photo: PhotoImage | tkinter.PhotoImage, ptr: CapsuleType | ||
) -> None: | ||
tk = photo.tk | ||
try: | ||
tk.call(command, photo, id) | ||
tk.call(command, photo, repr(ptr)) | ||
except tkinter.TclError: | ||
# activate Tkinter hook | ||
# may raise an error if it cannot attach to Tkinter | ||
from . import _imagingtk | ||
|
||
_imagingtk.tkinit(tk.interpaddr()) | ||
tk.call(command, photo, id) | ||
tk.call(command, photo, repr(ptr)) | ||
|
||
|
||
# -------------------------------------------------------------------- | ||
|
@@ -142,7 +131,10 @@ def __init__( | |
self.paste(image) | ||
|
||
def __del__(self) -> None: | ||
name = self.__photo.name | ||
try: | ||
name = self.__photo.name | ||
except AttributeError: | ||
return | ||
homm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.__photo.name = None | ||
try: | ||
self.__photo.tk.call("image", "delete", name) | ||
|
@@ -185,15 +177,14 @@ def paste(self, im: Image.Image) -> None: | |
the bitmap image. | ||
""" | ||
# convert to blittable | ||
im.load() | ||
ptr = im.getim() | ||
image = im.im | ||
if image.isblock() and im.mode == self.__mode: | ||
block = image | ||
else: | ||
block = image.new_block(self.__mode, im.size) | ||
if not image.isblock() or im.mode != self.__mode: | ||
block = Image.core.new_block(self.__mode, im.size) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine this change was just because you didn't like Rather than moving There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’d like to clarify that the reason for this change is not a matter of personal preference. The There is no intent to modify or enhance the behavior; I was simply correcting the method's improper placement. |
||
image.convert2(block, image) # convert directly between buffers | ||
ptr = block.ptr | ||
|
||
_pyimagingtkcall("PyImagingPhoto", self.__photo, block.id) | ||
_pyimagingtkcall("PyImagingPhoto", self.__photo, ptr) | ||
|
||
|
||
# -------------------------------------------------------------------- | ||
|
@@ -225,17 +216,11 @@ def __init__(self, image: Image.Image | None = None, **kw: Any) -> None: | |
self.__mode = image.mode | ||
self.__size = image.size | ||
|
||
if _pilbitmap_check(): | ||
# fast way (requires the pilbitmap booster patch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the record, the last mention of this patch was removed in Pillow 4.1.0 by #2360 |
||
image.load() | ||
kw["data"] = f"PIL:{image.im.id}" | ||
self.__im = image # must keep a reference | ||
else: | ||
# slow but safe way | ||
kw["data"] = image.tobitmap() | ||
self.__photo = tkinter.BitmapImage(**kw) | ||
self.__photo = tkinter.BitmapImage(data=image.tobitmap(), **kw) | ||
|
||
def __del__(self) -> None: | ||
if not hasattr(self, "__photo"): | ||
return | ||
name = self.__photo.name | ||
homm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self.__photo.name = None | ||
try: | ||
|
@@ -273,9 +258,8 @@ def __str__(self) -> str: | |
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 | ||
|
||
_pyimagingtkcall("PyImagingPhotoGet", photo, block.id) | ||
_pyimagingtkcall("PyImagingPhotoGet", photo, im.getim()) | ||
|
||
return im | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.