Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def grabclipboard() -> Image.Image | list[str] | None:
data = io.BytesIO(binascii.unhexlify(p.stdout[11:-3]))
return Image.open(data)
elif sys.platform == "win32":
fmt, data = Image.core.grabclipboard_win32()
if fmt == "file": # CF_HDROP
format_name, data = Image.core.grabclipboard_win32()
if format_name == "file": # CF_HDROP
import struct

o = struct.unpack_from("I", data)[0]
Expand All @@ -180,16 +180,8 @@ def grabclipboard() -> Image.Image | list[str] | None:
else:
files = data[o:].decode("utf-16le").split("\0")
return files[: files.index("")]
if isinstance(data, bytes):
data = io.BytesIO(data)
if fmt == "png":
from . import PngImagePlugin

return PngImagePlugin.PngImageFile(data)
elif fmt == "DIB":
from . import BmpImagePlugin

return BmpImagePlugin.DibImageFile(data)
if format_name in {"DIB", "PNG"}:
return Image.open(io.BytesIO(data), formats=[format_name])
return None
else:
if os.getenv("WAYLAND_DISPLAY"):
Expand Down
4 changes: 3 additions & 1 deletion src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,10 @@ PyImaging_GrabClipboardWin32(PyObject *self, PyObject *args) {
void *data;
PyObject *result;
UINT format;
// Windows clipboard format identifiers
UINT formats[] = {CF_DIB, CF_DIBV5, CF_HDROP, RegisterClipboardFormatA("PNG"), 0};
Comment thread
radarhere marked this conversation as resolved.
LPCSTR format_names[] = {"DIB", "DIB", "file", "png", NULL};
// For format_name in ImageGrab.py, in the same order as the formats above
LPCSTR format_names[] = {"DIB", "DIB", "file", "PNG", NULL};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LPCSTR format_names[] = {"DIB", "DIB", "file", "PNG", NULL};
// Pillow format names in the same order as the formats above
LPCSTR format_names[] = {"DIB", "DIB", "file", "PNG", NULL};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...they're not exactly 'Pillow format names'. 'DIB' and 'PNG are, but 'file' isn't.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair:

Suggested change
LPCSTR format_names[] = {"DIB", "DIB", "file", "PNG", NULL};
// Names known by ImageGrab.py in the same order as the formats above
LPCSTR format_names[] = {"DIB", "DIB", "file", "PNG", NULL};

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've pushed a similar comment, that connects it clearly to the variable in ImageGrab.py.


if (!OpenClipboard(NULL)) {
// Maybe the clipboard is temporarily in use by another process.
Expand Down
Loading