30

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.

4

6 Answers 6

12

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:

1
  • Your first option using png works great! Thank you!
    – Edenshaw
    May 9, 2017 at 14:01
12

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

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

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]);
2
  • Thanks, it works to past into libreoffice, however pasting into inkscape doesn't.
    – student
    Jan 27, 2012 at 14:58
  • Nice one, it works well with pasting into xournal.
    – K1.
    Jul 19, 2017 at 19:53
7

Here's a short bash script that uses xclip to copy a file.

You 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...

1
  • This copies into office fine, but not into xournal!
    – K1.
    Jul 19, 2017 at 19:53
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)
0

Note that xclip only works for X11.

A solution for Wayland:

wl-copy -t image/png < myimage.png

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .