将图像复制到剪贴板?

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

首先,关于SO 在python中将图像复制到剪贴板的问题导致回答使用PIL和win32clipboard在python中将图像写入Windows剪贴板?,这仅适用于Python 2.x。 ——我尝试过,但没有成功。我克服了一个问题:StringIO 和 cStringIO 模块在 Python 3.0 中消失了:,但遇到了另一个问题:

TypeError: string argument expected, got 'bytes'

因此,对于Python 3再次提出同样的问题——如何在Python 3中将图像复制到剪贴板?这是我到目前为止得到的代码:

from io import StringIO
import win32clipboard
from PIL import Image

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

filepath = 'image.jpg'
image = Image.open(filepath)

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

send_to_clipboard(win32clipboard.CF_DIB, data)

谢谢

python windows python-3.x clipboard
7个回答
12
投票

你不想在这里

StringIO
。图像是原始二进制数据,在 Py3 中,
str
纯粹用于文本;
bytes
bytes
类对象(
bytearray
、连续
memoryview
mmap
)适用于二进制数据。要替换 Py2 的
StringIO.StringIO
来表示二进制数据,您需要在 Python 3 中使用
io.BytesIO
,而不是
io.StringIO


11
投票

我复制了代码并将 StringIO 替换为 BytesIO,它成功了! (带有 *.jpg 和 *.png 文件)非常感谢!

from io import BytesIO
import win32clipboard
from PIL import Image

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

filepath = 'Ico2.png'
image = Image.open(filepath)

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

send_to_clipboard(win32clipboard.CF_DIB, data)

5
投票

对于那些想要复制粘贴的人

# parameter must be a PIL image 
def send_to_clipboard(image):
    output = BytesIO()
    image.convert('RGB').save(output, 'BMP')
    data = output.getvalue()[14:]
    output.close()

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

3
投票

有了上面的这些进口,一个杰作

def send_to_clipboard(clip_type, filepath):
    
    image = Image.open(filepath)

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

    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardData(clip_type, data)
    win32clipboard.CloseClipboard()

send_to_clipboard(win32clipboard.CF_DIB, 'imagem.png')

2
投票

这是一个工作应用程序,可以将屏幕的一部分作为图像抓取,一旦您单击黄色按钮,它就会出现在用 tkinter 制作的窗口中,您必须单击左上角,然后单击一个假想矩形的底部,该矩形将成为捕获的屏幕的一部分。一旦您单击该假想矩形的底部向下部分,屏幕的部分将被捕获。现在打开一些应用程序(例如work)并按control + v即可查看已复制粘贴到应用程序上的图像。

# grabscreen.py

import pyscreenshot as ImageGrab
import os
from pynput.mouse import Listener
import sys
import tkinter as tk
from PIL import Image

from io import BytesIO
import win32clipboard


''' Derives from my script grab (use this to show text in a pic and
transform in audio)


'''

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

def grab(x, y, w, h):
    im = ImageGrab.grab(bbox=(x, y, w, h))
    im.save('im.png')
    image = Image.open("im.png")
    output = BytesIO()
    image.convert("RGB").save(output, "BMP")
    data = output.getvalue()[14:]
    output.close()
    send_to_clipboard(win32clipboard.CF_DIB, data)


click1 = 0
x1 = 0
y1 = 0
def on_click(x, y, button, pressed):
    global click1, x1, y1, listener
    
    if pressed:
        if click1 == 0:
            x1 = x
            y1 = y
            click1 = 1
        else:
            grab(x1, y1, x, y)
            listener.stop()
            sys.exit()
def start():
    global listener

    # root.destroy()
    print("Click once on top left and once on bottom right")
    # with Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll) as listener:
    with Listener(on_click=on_click) as listener:
        listener.join()
        # listener.stop()
        # sys.exit()

root = tk.Tk()
root.geometry("400x200")
'''
when you click on this button goes to start
in start there i a listener and when you click it sends you to on_click
in on_click it makes you click twice and then goes to grab
in grab it uses pyscreenshot functions to grab the image and save it
we do not use ocr here, to do it use the grab.py file
'''
but = tk.Button(root, text="GRAB GET IMAGE", command=start, width=20,height=10, bg="gold")
but.pack()

root.mainloop()

再见


1
投票

您可以使用 winclip32 将位图图像复制到剪贴板 安装:

pip install winclip32

复制:

import winclip32
winclip32.set_clipboard_data(winclip32.BITMAPINFO_STD_STRUCTURE, your_binary_here)

0
投票

winclip32 建议本身不起作用,但与其他答案结合起来效果很好:

from io import BytesIO

import winclip32

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

winclip32.set_clipboard_data(winclip32.BITMAPINFO_STD_STRUCTURE, data)
© www.soinside.com 2019 - 2024. All rights reserved.