diff --git a/Tests/test_imagetk.py b/Tests/test_imagetk.py index a6a4dd4ea..162c0a935 100644 --- a/Tests/test_imagetk.py +++ b/Tests/test_imagetk.py @@ -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: diff --git a/src/PIL/ImageTk.py b/src/PIL/ImageTk.py index cc0c5294c..bf43cc672 100644 --- a/src/PIL/ImageTk.py +++ b/src/PIL/ImageTk.py @@ -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): diff --git a/src/Tk/tkImaging.c b/src/Tk/tkImaging.c index 991cf1c95..bb0fd33a3 100644 --- a/src/Tk/tkImaging.c +++ b/src/Tk/tkImaging.c @@ -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; }