python图片保存剪贴板_使用python将图像复制到MacOS剪贴板

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.

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.

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.

解决方案

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)'])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值