Linux Image from Clipboard

linux image from clipboard

I couldn't find any tool to do it, so I wrote this small Python script. It requires pygtk.

#!/usr/bin/python
"""
Save image from clipboard to file
"""

import sys
import glob
from optparse import OptionParser

def def_file():
"""
Return default file name
"""
files = glob.glob("img???.png")
if len(files) < 1:
return 'img001.png'
maxf = 0
for f in files:
try:
n = int(f[3:6])
maxf = max(n, maxf)
except ValueError:
pass
return 'img{:03d}.png'.format(maxf+1)

usage = """%prog [option] [filename]
Save image from clipboard to file in PNG format."""

op = OptionParser(usage=usage)
op.add_option("-o", "--open", action="store_true", dest="open", default=False,
help="Open saved file with default program (using xdg-open)")
(options, args) = op.parse_args()

if len(args) > 1:
parser.error("Only one argument expected")
sys.exit(1)
elif len(args) == 1:
fname = args[0]
else:
fname = def_file()

import gtk
clipboard = gtk.clipboard_get()
image = clipboard.wait_for_image()
if image is not None:
image.save(fname, "png")
print "PNG image saved to file", fname
if options.open:
import subprocess
subprocess.call(["xdg-open", fname])
else:
print "No image in clipboard found"

How to copy a file/image to the clipboard in Linux using Python

I found a way to do this using a shell command:

os.system(f"xclip -selection clipboard -t image/png -i {path + '/image.png'}")

It's less than ideal but it does the job.

What is involved in open sourcing proprietary software?

The main answer to your question has been given by others - getting the legal permission to do so (see the SO blog entry on Reverse Engineering the WMD Editor for an SO-related problem child) is often extremely difficult, even impossible.

So, my question is: why does it seem to take so long to make software open source? To me, it seems pretty simple: put your code on Sourceforge and Google Code and be done with it. But there's obviously something that I'm missing in the whole process.

What you describe - dumping the source - is not really Open Source. It is more akin to either AbandonWare or perhaps 'Available Source'. An Open Source project needs to accept inputs from outside, and build a community. One of the criteria that the Apache Software Foundation uses for its incubator projects is "has it acquired a critical mass of contributors from outside the original authors?". This is a valid concern.

Note that neither AbandonWare nor 'Available Source' is necessarily bad; both make code available that would otherwise not be available (and provide some of the benefits of Open Source). But there is more to Open Source than that.

Also, there is management overhead in handling a truly Open Source project. That is not negligible.

And, finally, it is not unheard of for the code quality to be such that the authors would rather not make the source available for fear of ridicule. I doubt if that applies in this case, but it can in other areas of the software world.

Automatically Copy a Screenshot to the Clipboard in Fedora (Linux)

Try press Ctrl+PrtScr for take screenshot to clipboard

Take screenshots on fedora

Image copied to clipboard doesn't persist on Linux

Unfortunately for you, this is "normal" behaviour on Linux. By default, clipboard data is not persisted when an application closes. The usual work-around for this problem is to install a clipboard manager. For Ubuntu, see this wiki article for more details:

  • Ubuntu Wiki: Clipboard Persistence

(NB: I have not actually tested any of the suggested solutions myself, so I don't know whether any of them will work with PyQt).

The basic problem is that on Linux, the clipboard only stores a reference to the underlying data. This is very efficient in terms of storage, because the data is only copied when the client program actually requests it. But of course if the source application closes, the reference will be invalidated, and the clipboard will become empty.

Pasting image to clipboard in python in linux

One way of getting text from/to the clipboard is using XSel. It's not pretty and requires you to communicate with an external program. But it works and is quite fast.

Not sure if it's the best solution but I know it works :)

[edit]You're right, it seems that xsel does not support images.

In that case, how about a slightly modified GTK version.

def copy_image(f):
assert os.path.exists(f), "file does not exist"
image = gtk.gdk.pixbuf_new_from_file(f)

clipboard = gtk.clipboard_get()
clipboard.set_image(image)
clipboard.store()

Do note that you might have to change the owner if your program exits right away because of how X keeps track of the clipboard.



Related Topics



Leave a reply



Submit