diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 9b5084acf..23eee2445 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -1,3 +1,4 @@ +import os import subprocess import sys @@ -79,7 +80,8 @@ $bmp = New-Object Drawing.Bitmap 200, 200 p.communicate() im = ImageGrab.grabclipboard() - assert_image_equal_tofile(im, "Tests/images/hopper.gif") + assert len(im) == 1 + assert os.path.samefile(im[0], "Tests/images/hopper.gif") @pytest.mark.skipif(sys.platform != "win32", reason="Windows only") def test_grabclipboard_png(self): diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 9fd8b1610..cb685b1ed 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -93,15 +93,20 @@ def grabclipboard(): os.unlink(filepath) return im elif sys.platform == "win32": - import io - fmt, data = Image.core.grabclipboard_win32() - if isinstance(data, str): - if fmt == "file": - with open(data, "rb") as f: - im = Image.open(io.BytesIO(f.read())) - return im + if fmt == "file": # CF_HDROP + import struct + + o = struct.unpack_from("I", data)[0] + if data[16] != 0: + files = data[o:].decode("utf-16le").split("\0") + return files[: files.index("")] + else: + files = data[o:].decode("mbcs").split("\0") + return files[: files.index("")] if isinstance(data, bytes): + import io + data = io.BytesIO(data) if fmt == "png": from . import PngImagePlugin diff --git a/src/display.c b/src/display.c index 0d52ca2cf..c8d3086e3 100644 --- a/src/display.c +++ b/src/display.c @@ -32,7 +32,6 @@ #ifdef _WIN32 -#include #include "ImDib.h" #if SIZEOF_VOID_P == 8 @@ -504,27 +503,6 @@ PyImaging_GrabClipboardWin32(PyObject* self, PyObject* args) return Py_BuildValue("zO", NULL, Py_None); } - if (formats[format] == CF_HDROP) { - LPDROPFILES files = (LPDROPFILES)GlobalLock(handle); - size = GlobalSize(handle); - - if (files->fWide) { - LPCWSTR filename = (LPCWSTR)(((char*)files) + files->pFiles); - size = wcsnlen(filename, (size - files->pFiles) / 2); - result = Py_BuildValue("zu#", "file", filename, size); - } - else { - LPCSTR filename = (LPCSTR)(((char*)files) + files->pFiles); - size = strnlen(filename, size - files->pFiles); - result = Py_BuildValue("zs#", "file", filename, size); - } - - GlobalUnlock(handle); - CloseClipboard(); - - return result; - } - data = GlobalLock(handle); size = GlobalSize(handle);