Repository: python-pillow/Pillow

Issue:
ImageTk.getimage() is broken (again)

### What did you do?
Tried to convert a PhotoImage to a Image using the ImageTk library function 

### What did you expect to happen?
Get an Image from the PhotoImage

### What actually happened?
got a TclError:
dir..\lib\site-packages\PIL\ImageTk.py", line 279, in getimage
    photo.tk.call("PyImagingPhotoGet", photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhotoGet"

### What are your OS, Python and Pillow versions?
* OS: Windows 10
* Python: 3.10.2
* Pillow: 8.0.1


```python
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
p_img = PhotoImage(file='dir/img_name')
image = ImageTk.getimage(p_img)
```



Discussion:
When you say "again", could you elaborate on this? Are you saying that it has been broken with some Pillow versions for you and not others?

Bear with me if you know this and think it is obvious, but
```python
p_img = PhotoImage(file='dir/img_name')
image = ImageTk.getimage(p_img)
```
could be simplified to 
```python
image = Image.open('dir/img_name')
```
You may have just used that specific code to demonstrate the general problem however.

Also, if you look at https://pillow.readthedocs.io/en/stable/installation.html#python-support, you will see that Pillow 8.0.1 is not intended to support Python 3.10. I doubt it would solve your problem, but I would recommend upgrading to a newer version of Pillow.
Greetings, 
I was referring to a [previous issue (from 2019)](https://github.com/python-pillow/Pillow/issues/2372) that has been long solved. In hindsight, maybe referencing such an old issue was not useful. 

Yes, I am aware that I can use the code provided, and yes, you are correct that I used the code to demonstrate the issue. 
The actual code is to resize a PhotoImage and I don't know any other way of doing so without converting to an Image. I can make it work without, but still thought useful to open the issue. Suggestions to resize without conversion would be very helpful! 

Thank you for the update suggestion. I am now running Pillow 9.0.0, however the issue still persists. 

Hi. Another question just to provide context - what version of Tkinter are you using?
```pycon
>>> from PIL import _tkinter_finder
>>> _tkinter_finder.tk_version
'8.6'
```
It is 8.6

Resolving patch:
diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py
--- a/src/PIL/ImageTk.py
+++ b/src/PIL/ImageTk.py
@@ -58,6 +58,33 @@ def _get_image_from_kw(kw):
         return Image.open(source)
 
 
+def _pyimagingtkcall(command, photo, id):
+    tk = photo.tk
+    try:
+        tk.call(command, photo, id)
+    except tkinter.TclError:
+        # activate Tkinter hook
+        # may raise an error if it cannot attach to Tkinter
+        from . import _imagingtk
+
+        try:
+            if hasattr(tk, "interp"):
+                # Required for PyPy, which always has CFFI installed
+                from cffi import FFI
+
+                ffi = FFI()
+
+                # PyPy is using an FFI CDATA element
+                # (Pdb) self.tk.interp
+                #  <cdata 'Tcl_Interp *' 0x3061b50>
+                _imagingtk.tkinit(int(ffi.cast("uintptr_t", tk.interp)), 1)
+            else:
+                _imagingtk.tkinit(tk.interpaddr(), 1)
+        except AttributeError:
+            _imagingtk.tkinit(id(tk), 0)
+        tk.call(command, photo, id)
+
+
 # --------------------------------------------------------------------
 # PhotoImage
 
@@ -170,33 +197,7 @@ def paste(self, im, box=None):
             block = image.new_block(self.__mode, im.size)
             image.convert2(block, image)  # convert directly between buffers
 
-        tk = self.__photo.tk
-
-        try:
-            tk.call("PyImagingPhoto", self.__photo, block.id)
-        except tkinter.TclError:
-            # activate Tkinter hook
-            try:
-                from . import _imagingtk
-
-                try:
-                    if hasattr(tk, "interp"):
-                        # Required for PyPy, which always has CFFI installed
-                        from cffi import FFI
-
-                        ffi = FFI()
-
-                        # PyPy is using an FFI CDATA element
-                        # (Pdb) self.tk.interp
-                        #  <cdata 'Tcl_Interp *' 0x3061b50>
-                        _imagingtk.tkinit(int(ffi.cast("uintptr_t", tk.interp)), 1)
-                    else:
-                        _imagingtk.tkinit(tk.interpaddr(), 1)
-                except AttributeError:
-                    _imagingtk.tkinit(id(tk), 0)
-                tk.call("PyImagingPhoto", self.__photo, block.id)
-            except (ImportError, AttributeError, tkinter.TclError):
-                raise  # configuration problem; cannot attach to Tkinter
+        _pyimagingtkcall("PyImagingPhoto", self.__photo, block.id)
 
 
 # --------------------------------------------------------------------
@@ -276,7 +277,7 @@ def getimage(photo):
     im = Image.new("RGBA", (photo.width(), photo.height()))
     block = im.im
 
-    photo.tk.call("PyImagingPhotoGet", photo, block.id)
+    _pyimagingtkcall("PyImagingPhotoGet", photo, block.id)
 
     return im
 


Test patch:
diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py
--- a/Tests/test_imagetk.py
+++ b/Tests/test_imagetk.py
@@ -75,8 +75,9 @@ def test_photoimage_blank():
         assert im_tk.width() == 100
         assert im_tk.height() == 100
 
-        # reloaded = ImageTk.getimage(im_tk)
-        # assert_image_equal(reloaded, im)
+        im = Image.new(mode, (100, 100))
+        reloaded = ImageTk.getimage(im_tk)
+        assert_image_equal(reloaded.convert(mode), im)
 
 
 def test_bitmapimage():