我是靠谱客的博主 迅速热狗,最近开发中收集的这篇文章主要介绍python实现图片粘贴_将图像复制到Python3中的剪贴板,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

First of all, the question on SO copy image to clipboard in python leads to answer Write image to Windows clipboard in python with PIL and win32clipboard?, which was only good for Python 2.x. -- I tried it and it didn't work. I overcame one problem: StringIO and cStringIO modules are gone in Python 3.0:, but bumped into another one:

TypeError: string argument expected, got 'bytes'

Hence, re-asking the same question again for Python 3 -- How to copy image to clipboard in Python 3? Here is the code I've got so far:

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)

Thanks

解决方案

I did copy the code and replace the StringIO with BytesIO and it worked! (with *.jpg and *.png files) Thank you so much!

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)

最后

以上就是迅速热狗为你收集整理的python实现图片粘贴_将图像复制到Python3中的剪贴板的全部内容,希望文章能够帮你解决python实现图片粘贴_将图像复制到Python3中的剪贴板所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部