22

Is there a way in a bash to copy a picture to the clipboard?

For example if there is a picture mypic.png I want to have a command like clipcopy mypic.png such that I can go for example to inkscape and paste it afterwards.

9

This python script by cheshirekow claims to do what you want.

#! /usr/bin/python
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys

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()

copy_image(sys.argv[1]);
  • Thanks, it works to past into libreoffice, however pasting into inkscape doesn't. – student Jan 27 '12 at 14:58
  • Nice one, it works well with pasting into xournal. – Keivan Jul 19 '17 at 19:53
9

You can use CopyQ for this. From the website:

Clipboard manager with advanced features

  • CopyQ is clipboard manager with searchable and editable history.
  • Supports Linux and Windows.
  • Experimental support for OS X 10.9+.
  • Store text, HTML, images and any other custom format.
  • Advanced command-line interface and scripting.

To copy an image (you need to indicate the MIME type):

copyq write image/png - < file.png && copyq select 0

To copy a data file (you need to indicate the MIME type):

copyq write application/pdf - < file.pdf && copyq select 0

Check their wiki for more documentation and usage examples.

See also:

  • Your first option using png works great! Thank you! – Edenshaw May 9 '17 at 14:01
5

I've edited @don_crissti's answer to allow pipe input:

#!/usr/bin/env python

import sys
from gi.repository import Gtk, Gdk, GdkPixbuf

def store(pixbuf):
        clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
        clipboard.set_image(pixbuf)
        clipboard.store()

def copy_image(f):
    image = Gtk.Image.new_from_file(f)
    if image.get_storage_type() == Gtk.ImageType.PIXBUF:
        pixbuf = image.get_pixbuf()
        store(pixbuf)
    else:
        print("Copying failed")

def copy_pixbuf(data):
    loader = GdkPixbuf.PixbufLoader()
    loader.write(data)
    loader.close()
    pixbuf = loader.get_pixbuf()
    store(pixbuf)

if sys.stdin.isatty():
    if len(sys.argv) != 2:
        print("Usage: image-to-clipboard.py image")
    else:
        f = sys.argv[1]
        copy_image(f);
else:
    data = sys.stdin.read()
    copy_pixbuf(data)
5

Here's a short bash script that uses xclip to copy a file. Should be able to paste with ctrl-v.

#!/bin/bash
command -v xclip >/dev/null 2>&1 || { echo "Need command xclip. Aborting." >&2; exit 1; }
[[ -f "$1" ]] || { echo "Error: Not a file." >&2; exit 1; }
TYPE=$(file -b --mime-type "$1")
xclip -selection clipboard -t "$TYPE" < "$1"

Thanks to...

  • This copies into office fine, but not into xournal! – Keivan Jul 19 '17 at 19:53
2

See the answer here: https://askubuntu.com/a/759660/187689

xclip -selection clipboard -t image/png -i example.png

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.