开发者

How to put an image from python to klipper?

开发者 https://www.devze.com 2023-03-19 15:47 出处:网络
Using python, one can read in an image: from PIL import Image, ImageTk image = Image.open(\"test.jpg\")

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.setClipboar开发者_StackOverflow中文版dContents("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?"


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.


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:

#!/bin/bash
cd /home/boris/Downloads/
name=`ls -t1 | head -1`
gwenview $name &
sleep 3
id=`ps -e | grep gwenview | awk '{print $1}'`
qdbus org.kde.gwenview-$id /gwenview/MainWindow_1/actions/edit_copy trigger
sleep 3
kill $id

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

qdbusviewer

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:

qdbus org.kde.krunner /ScreenSaver Lock
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号