Copy an image to MacOS clipboard using python

旧巷老猫 提交于 2019-12-24 03:26:05

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/54008175/copy-an-image-to-macos-clipboard-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!