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.

landroni
  • 10,906
student
  • 18,305
  • This should be broken in two steps: 1. what data does Inkscape expect to be on the clipboard? E.g. file content or file path. I imagine content since path is trivial with xsel -b? 2. How to get that data on the clipboard. – Ciro Santilli OurBigBook.com Aug 13 '14 at 09:33
  • 1
    possible cross site duplicate of: http://superuser.com/questions/301851/how-to-copy-a-picture-to-clipboard-from-command-line-in-linux – Ciro Santilli OurBigBook.com Aug 13 '14 at 09:34
  • More general question for any file type: http://superuser.com/questions/796376/is-is-possible-to-copy-binary-data-containing-a-nul-character-to-the-x-clipboard – Ciro Santilli OurBigBook.com Aug 13 '14 at 11:50
  • If you have ImageMagick installed, I believe the command is convert ... clipboard: (... are the other arguments including your filename). – Deathgrip Jun 12 '17 at 22:00

6 Answers6

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:

landroni
  • 10,906
12

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

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

iman
  • 251
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]);
Mika Fischer
  • 2,262
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...

Greenonline
  • 1,851
  • 7
  • 17
  • 23
jozxyqk
  • 702
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)
wieczorek1990
  • 151
  • 1
  • 3
0

Note that xclip only works for X11.

A solution for Wayland:

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