Fixed ImageTk getimage

This commit is contained in:
Andrew Murray 2019-04-29 23:43:13 +10:00
parent 6d5b8926be
commit d9a3878937
3 changed files with 29 additions and 21 deletions

View File

@ -61,9 +61,8 @@ class TestImageTk(PillowTestCase):
self.assertEqual(im_tk.width(), im.width)
self.assertEqual(im_tk.height(), im.height)
# _tkinter.TclError: this function is not yet supported
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
reloaded = ImageTk.getimage(im_tk)
self.assert_image_equal(reloaded, im.convert("RGBA"))
def test_photoimage_blank(self):
# test a image using mode/size:

View File

@ -275,10 +275,13 @@ class BitmapImage(object):
def getimage(photo):
""" This function is unimplemented """
"""Copies the contents of a PhotoImage to a PIL image memory."""
photo.tk.call("PyImagingPhotoGet", photo)
im = Image.new("RGBA", (photo.width(), photo.height()))
block = im.im
photo.tk.call("PyImagingPhotoGet", photo, block.id)
return im
def _show(image, title):

View File

@ -149,17 +149,18 @@ PyImagingPhotoPut(ClientData clientdata, Tcl_Interp* interp,
return TCL_OK;
}
/* Warning -- this does not work at all */
static int
PyImagingPhotoGet(ClientData clientdata, Tcl_Interp* interp,
int argc, const char **argv)
{
Imaging im;
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
int x, y, z;
if (argc != 2) {
if (argc != 3) {
TCL_APPEND_RESULT(interp, "usage: ", argv[0],
" srcPhoto", (char *) NULL);
" srcPhoto destImage", (char *) NULL);
return TCL_ERROR;
}
@ -172,21 +173,26 @@ PyImagingPhotoGet(ClientData clientdata, Tcl_Interp* interp,
return TCL_ERROR;
}
/* get PIL Image handle */
im = ImagingFind(argv[2]);
if (!im) {
TCL_APPEND_RESULT(interp, "bad name", (char*) NULL);
return TCL_ERROR;
}
TK_PHOTO_GET_IMAGE(photo, &block);
printf("pixelPtr = %p\n", block.pixelPtr);
printf("width = %d\n", block.width);
printf("height = %d\n", block.height);
printf("pitch = %d\n", block.pitch);
printf("pixelSize = %d\n", block.pixelSize);
printf("offset = %d %d %d %d\n", block.offset[0], block.offset[1],
block.offset[2], block.offset[3]);
for (y = 0; y < block.height; y++) {
UINT8* out = (UINT8*)im->image32[y];
for (x = 0; x < block.pitch; x += block.pixelSize) {
for (z=0; z < block.pixelSize; z++) {
int offset = block.offset[z];
out[x + offset] = block.pixelPtr[y * block.pitch + x + offset];
}
}
}
TCL_APPEND_RESULT(
interp, "this function is not yet supported", (char *) NULL
);
return TCL_ERROR;
return TCL_OK;
}