使用python将图像复制到MacOS剪贴板 [英] Copy an image to MacOS clipboard using python

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

问题描述

我想将图像(PIL图像)复制到Mac OS
上的剪贴板中,我尝试了数十种不同的方法来做到这一点。 pyclip或剪贴板等大多数python剪贴板模块不支持图片。

I want to copy an image (PIL image) to clipboard on Mac OS I tried dozens of different ways to do that. Most of python clipboard modules like pyclip or clipboard doesn't support images.

我找到了在Windows上执行此操作的代码,但对于Mac却没有。我尝试使用QT,通过brew安装它,由于某种原因brew安装了一个新的python 3.7.2(我有3.7.1),并弄乱了诸如python和python3之类的终端命令。我设法修复了它,但是非常令人沮丧。而且我还是无法使它正常工作。

I found a code that does that on windows, but nothing for mac. I tried to use QT, installed it through brew, for some reason brew installed a new python 3.7.2 (I had 3.7.1) and messed up with terminal commands like python and python3. I managed to fix it, but it's very frustrating. And I couldn't get it to work anyway.

我见过一种带有win32剪贴板的方法,但这仅适用于Windows。我尝试了各种不同的模块,但是没有一个接受图像。我无法提出解决方案,所以我在这里寻求帮助。

I've seen a method with win32 clipboard, but it's for windows only. I tried various different modules, but none of them accept images. I couldn't come up with a solution, so I'm here asking for help.

有一点背景知识。我正在编写一个应用程序,它将保存剪贴板中的图像并将其保存到文件中。我对此没有问题,但我也想存储保存的内容,以后再将其拉回到剪贴板。像打开上次保存的一样。

A little background. I'm writing an app that will save the image from the clipboard and saves it to a file. I have no problem with this, but I also want to store what I saved and later to be able to pull it back to the clipboard. Like Open Last Saved.

推荐答案

这小小的丑陋作品可以加载 image.jpg 到剪贴板上...

This little piece of ugliness works and loads "image.jpg" onto the clipboard...

#!/usr/bin/env python3

import subprocess

subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file "image.jpg") as JPEG picture)'])

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

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