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.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou 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:
See the answer here: https://askubuntu.com/a/759660/187689
xclip -selection clipboard -t image/png -i example.png
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]);
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...
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)
Note that xclip
only works for X11.
A solution for Wayland:
wl-copy -t image/png < myimage.png
xsel -b
? 2. How to get that data on the clipboard.convert ... clipboard:
(... are the other arguments including your filename).