如何将 python 中的图像放入 klipper 中?
使用Python,可以读取图像:
from PIL import Image, ImageTk
image = Image.open("test.jpg")
然后可以通过以下方式写入klipper:
import dbus
bus = dbus.SessionBus()
proxy = bus.get_object("org.kde.klipper","/klipper")
iface = dbus.Interface(proxy,"org.kde.klipper.klipper")
iface.setClipboardContents("text")
但是当我尝试写入我刚刚打开的图像时 - 它说它无法接受它:
iface.setClipboardContents(image)
TypeError: Expected a string or unicode object
所以我的问题是:“如何用 python 将图像放入 klipper 中?”
Using python, one can read in an image:
from PIL import Image, ImageTk
image = Image.open("test.jpg")
Then one can write to klipper in the following way:
import dbus
bus = dbus.SessionBus()
proxy = bus.get_object("org.kde.klipper","/klipper")
iface = dbus.Interface(proxy,"org.kde.klipper.klipper")
iface.setClipboardContents("text")
But when I try to write an image I just opened - it says it can't accept it:
iface.setClipboardContents(image)
TypeError: Expected a string or unicode object
So my question is: "how to put an image to klipper with python?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Image 是一个原始的 Python 图像库对象。您需要弄清楚
1) Klipper 除了图像(不是文本)还有什么输入格式?
2) 如何将图像序列化为这种格式。您不传递Python图像对象,但可能将保存的数据(PNG文件数据)传递给Klipper。
Image is a raw Python imaging library object. You need to find out
1) What input format Klipper excepts for images (not text?)
2) How to serialize the image to this format. You don't pass Python image object, but probably saved data (PNG file data) to Klipper.
找到了一些解决方案 - 不是使用 python,而是使用 bash,并且它不适用于远程图片 - 仅适用于本地图片(因为 gwenview)。这是一个 bash 脚本:
这也可以使用 python 来完成,但是由于我进行了系统调用,而不是在图片中读取 - 不需要使用 python。
解释一下 - 假设您将图像下载到 ~/Downloads。然后,如果您使用此脚本,它将在其中找到最新的文件,使用 gwenview 打开它,将其复制到剪贴板(!),然后关闭 gwenview。
我认为工作正常 - 没有其他格温可以运行。如果您想使用它 - 将脚本绑定到热键是明智的。
我正在寻找这样的脚本的原因 - 是因为我想切换到键盘驱动的网络浏览器(例如 Conkeror 或 Pentadactyl) - 它允许快速保存图像 - 而我主要需要将其粘贴到某个 .odt 文件中)。
该脚本可以进一步成为守护进程 - 它将监视下载目录。但现在我要编写一个脚本来下载图像并自动放入剪贴板 - 请参阅我的下一个问题< /a>.
找到您可能需要的 qdbus 命令的方法是打开
并找到您需要的应用程序,然后找到您需要的命令,然后进行类似的调用,例如锁定屏幕:
Found some solution - not with python but with bash, and it doesn't work for remote pictures - works only for local ones (because of gwenview). Here is a bash script:
This could also be done with python of cource, but since I make a system call, not read in a picture - there's no need to use python.
To explain it - suppose You downloaded an image to ~/Downloads. Then if you the this script it will finds the freshest file in there, opens it with gwenview, copies it to clipboard (!), then closes gwenview.
I think to work properly - no other gwen's could be running. And if You want to use it - it's wise to bind the script to a hotkey.
The reason I was looking for such a script - is because I want to switch to keyboard-driven webbrowser (such as Conkeror or Pentadactyl) - and it allows to save image quickly - while I mostly need to paste it to some .odt file).
This script can further be made a daemon - which will watch the Downloads dir. But now I to write a script which will download image, and put to clipboard automatically - please see my next question.
The way to find qdbus command you might need - is to open
and find the app you need, then the command you need, and then make call similarly it's done to, e.g. lock the screen: