首页 > 解决方案 > 将 PIL 图像复制到剪贴板(Python)

问题描述

我已经用 Tkinter 在 Python 中制作了一个小画图程序,现在我想添加一个“复制”功能,这样你就可以在 Discord 等其他应用程序中更快地使用 CTRL + V 绘图。这是一个测试:

def Copy():
def send_to_clipboard(clip_type, data):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(clip_type, data)
    win32clipboard.CloseClipboard()

x2 = fen.winfo_rootx()+Can.winfo_x()
y2 = fen.winfo_rooty()+Can.winfo_y()
x1 = x2+Can.winfo_width()
y1 = y2+Can.winfo_height()
print("save")
ImageGrab.grab().crop((x2,y2,x1,y1)).save("Image.bmp")

filepath = "Image.bmp"
image = Image.open(filepath)

output = io.BytesIO()
image.convert("RGB")
image.save(output, "BMP")
data = output.getvalue()[8:]
output.close()

send_to_clipboard(win32clipboard.CF_DIB, data)

当我尝试这个时,它可以工作,除了我无法将图像粘贴到 Paint 或 Discord 中......或者它无法读取它,或者它崩溃。有人能帮助我吗?;-;

标签: pythonpython-3.xtkinter

解决方案


推荐阅读