使用PIL和win32clipboard将图像写入python中的Windows剪贴板吗? [英] Write image to Windows clipboard in python with PIL and win32clipboard?

查看:255
本文介绍了使用PIL和win32clipboard将图像写入python中的Windows剪贴板吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打开图像文件并将图像复制到Windows剪贴板.有没有办法解决这个问题:

I'm trying to open an image file and copy the image to the Windows clipboard. Is there a way to fix this:

import win32clipboard
from PIL import Image

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

clip_type = win32clipboard.CF_BITMAP
filepath = 'c:\\temp\\image.jpg'

im = Image.open(filepath) 
data = im.tobitmap() # fails with valueerror: not a bitmap
# data = im.tostring() runs, but receiving programs can't read the results
send_to_clipboard(clip_type, data)

我可以安装PythonMagick等,但是不希望为一次性程序安装另一个库

I could install PythonMagick, etc., but would prefer not installing yet another library for a one-off program

推荐答案

from cStringIO 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)

这篇关于使用PIL和win32clipboard将图像写入python中的Windows剪贴板吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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