将PIL/PILLOW图像复制到Windows剪贴板 [英] Copy PIL/PILLOW Image to Windows Clipboard

查看:220
本文介绍了将PIL/PILLOW图像复制到Windows剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过这个问题,我按照每个步骤进行操作,更改代码以满足我的要求,它们是Python3,Pillow和ctypes.库越少越好.

I've seen this question and i followed every step, changing the code to satisfy my requirements, that are Python3, Pillow, and ctypes. The less libraries, the better.

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()

那是我脚本中剥离的代码,我认为这是移植到我的需求中的相同代码.执行后,它应将当前桌面复制到剪贴板.我得到这个代替:

That is the stripped code from my script that, i think, is the same code in the question ported to my requirements. When executed, it should copy the current desktop to the clipboard. I get this instead:

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不是我的事.

I'm sorry i'm asking such a (probably) easy question here, but i really don't know what is failing, and ctypes is not my thing.

推荐答案

为什么.与ctypes相比,显然win32clipboard库确实简化了某些事情.您试图简单地用另一种替换另一种是不正确的.

Whew. Apparently the win32clipboard library does simplify some things when compared to ctypes. Your attempt to simply replace one with the other is far from correct.

因此,我启动了Windows虚拟机,安装了Pillow并重写了程序,从两个其他中学习了

So I booted up my Windows virtual machine, installed Pillow and rewrote your program, learning from two other answers:

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()

这篇关于将PIL/PILLOW图像复制到Windows剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆