I am trying to copy a image created in PIL to the macOS clipboard.
I found 2 ways:
Using Pasteboard (pypi.org/project/pasteboard) but that one does not seem to work with Python 3.10 (I get a wheels error when trying to install it).
The other way is from here ( Copy an image to MacOS clipboard using python ) and uses the following code:
import subprocess subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file 'image.jpg') as JPEG picture)'])
I tried playing around with this second approach but I cannot get it to work with an image created in Pillow. For example:
import subprocess
from PIL import Image
image = Image.open('img.png')
subprocess.run(["osascript", "-e", 'set the clipboard to (read (POSIX file '???') as JPEG picture)'])
In the original post the '???' was an image.jpg, so how could I insert the PIL data in there? I tried some variations but kept on getting the same error:
CFURLGetFSRef was passed an URL which has no scheme (the URL will not work with other CFURL routines)
22:67: 2023-01-12 13:53:42.227 osascript[1361:27923] CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme
execution error: Can’t make file ":image" into type file. (-1700)
This will copy the image as a data URL :
import base64
from io import BytesIO
from PIL import Image
image = Image.open('img.png')
buffered = BytesIO()
image.save(buffered, format="PNG")
base64_enc = base64.b64encode(buffered.getvalue())
print("data:image/png;base64,"+"".join(map(chr,base64_enc)))
which you then will be able to paste in your browser, and probably not in file manager though
if this didn't answer your question then you probably have to find a library that supports mac clipboard.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.