将 PIL/PILLOW 图像复制到 Windows 剪贴板

问题描述 投票:0回答:2

我已经看到了这个问题,我遵循了每一步,更改代码以满足我的要求,即Python3、Pillow和ctypes。库越少越好。

import ctypes
from PIL import ImageGrab, Image
from io import BytesIO

user32 = ctypes.windll.user32

img = ImageGrab.grab()
output = BytesIO()
img.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()

user32.OpenClipboard()
user32.EmptyClipboard()
user32.SetClipboardData(user32.CF_DIB, data)
user32.CloseClipboard()

这是从我的脚本中删除的代码,我认为它与移植到我的要求的问题中的代码相同。执行时,它应该将当前桌面复制到剪贴板。我得到这个:

File "C:\Users\Gcq\Documents\python\Screen\Screen.py", line 132, in shot
    user32.OpenClipboard()
ValueError: Procedure probably called with not enough arguments (4 bytes missing)

很抱歉,我在这里问了这样一个(可能)简单的问题,但我真的不知道失败的原因是什么,而且 ctypes 不是我的菜。

python winapi ctypes python-imaging-library
2个回答
7
投票

该示例使用

pywin32
,它是 Win32 API 的 Python 包装器,它隐藏了一些低级细节,如果您想使用
ctypes
,您需要自己处理。

这是使用

ctypes
执行此操作的方法,它添加了创建全局分配的缓冲区并将
data
复制到该缓冲区的功能:

#!python
"""https://stackoverflow.com/a/21320589/819417"""

# from cStringIO import StringIO
from io import BytesIO
from ctypes import c_size_t, memmove, windll
from ctypes.wintypes import BOOL, HANDLE, HWND, LPVOID, UINT

from PIL import Image


HGLOBAL = HANDLE
SIZE_T = c_size_t
GHND = 0x0042
GMEM_SHARE = 0x2000

GlobalAlloc = windll.kernel32.GlobalAlloc
GlobalAlloc.restype = HGLOBAL
GlobalAlloc.argtypes = [UINT, SIZE_T]

GlobalLock = windll.kernel32.GlobalLock
GlobalLock.restype = LPVOID
GlobalLock.argtypes = [HGLOBAL]

GlobalUnlock = windll.kernel32.GlobalUnlock
GlobalUnlock.restype = BOOL
GlobalUnlock.argtypes = [HGLOBAL]

CF_DIB = 8

OpenClipboard = windll.user32.OpenClipboard
OpenClipboard.restype = BOOL
OpenClipboard.argtypes = [HWND]

EmptyClipboard = windll.user32.EmptyClipboard
EmptyClipboard.restype = BOOL
EmptyClipboard.argtypes = None

SetClipboardData = windll.user32.SetClipboardData
SetClipboardData.restype = HANDLE
SetClipboardData.argtypes = [UINT, HANDLE]

CloseClipboard = windll.user32.CloseClipboard
CloseClipboard.restype = BOOL
CloseClipboard.argtypes = None

#################################################

image = Image.new("RGB", (200, 200), (255, 0, 0))

# output = StringIO()
output = BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:]
output.close()

hData = GlobalAlloc(GHND | GMEM_SHARE, len(data))
pData = GlobalLock(hData)
memmove(pData, data, len(data))
GlobalUnlock(hData)

OpenClipboard(None)
EmptyClipboard()
SetClipboardData(CF_DIB, pData)
CloseClipboard()

4
投票

哇。显然,与

win32clipboard
相比,
ctypes
库确实简化了一些事情。您尝试简单地用另一种替代一种的尝试是远远不正确的。

所以我启动了我的Windows虚拟机,安装了Pillow并重写了你的程序,从两个otheranswers中学习:

import io

import ctypes
msvcrt = ctypes.cdll.msvcrt
kernel32 = ctypes.windll.kernel32
user32 = ctypes.windll.user32

from PIL import ImageGrab

img = ImageGrab.grab()
output = io.BytesIO()
img.convert('RGB').save(output, 'BMP')
data = output.getvalue()[14:]
output.close()

CF_DIB = 8
GMEM_MOVEABLE = 0x0002

global_mem = kernel32.GlobalAlloc(GMEM_MOVEABLE, len(data))
global_data = kernel32.GlobalLock(global_mem)
msvcrt.memcpy(ctypes.c_char_p(global_data), data, len(data))
kernel32.GlobalUnlock(global_mem)
user32.OpenClipboard(None)
user32.EmptyClipboard()
user32.SetClipboardData(CF_DIB, global_mem)
user32.CloseClipboard()
© www.soinside.com 2019 - 2024. All rights reserved.