8

I want to set the clipboard to file abc.jpg, in image format, so I can paste it to gpaint, etc. X11 applications, can I?

I have enabled Screen snapshot feature in Compiz windows manager, and I need to give a command line to copy the snapshot image file to the clipboard.

12

scrot + xclip

You can use scrot with xclip to take a screenshot and copy it to clipboard.

scrot '/tmp/%F_%T_$wx$h.png' -e 'xclip -selection clipboard -target image/png -i $f'

It will capture whole of your screen and copy the image to clipboard. If you want to capture current window then use -u flag. For selection of particular area, you can add -s flag. See $ man scrot for more options.

It will store your screenshot in /tmp directory. You can change that directory wherever you want it to get stored. Files from /tmp directory usually gets deleted after each reboot. If you want to immediately remove the stored file, then do something like:

scrot -w '/tmp/%F_%T_$wx$h.png' -e 'xclip -selection clipboard -target image/png -i $f && rm $f'

As I read in other comments, you need it for copying a screenshot to the clipboard. I hope this answers your question.

If you just need to copy an already existing image file to clipboard:

cat 2018-06-16-224938_670x730_scrot.png | xclip -selection clipboard -target image/png -i

You can set keyboard shortcuts/keybindings according to your current Desktop Environment/window manager.


Bonus

Explanation of /tmp/%F_%T_$wx$h.png:

It's being used as the file name. These are called format specifiers. They are of two type: starting with % or $.

%F     Equivalent to %Y-%m-%d (the ISO 8601 date format).

%T     The time in 24-hour notation (%H:%M:%S).

%F_%T_ will print something like: 2018-06-17_02:52:19_ i.e. your current timestamp. You can customize the format as per your requirements. See $ man strftime for more help.

$wx$h are part of the scrot's internal specifiers.

$w   image width
$h   image height

So the final file name will look something like 2018-06-17_02:52:19_1365x384.png.

1
  • 1
    What a great answer! Thank you kindly.
    – Eduardo I.
    Apr 17 at 16:00
10

First, install python, and pygtk

sudo apt-get install python pygtk

Now save the following script somewhere as imgclip.py (see https://stackoverflow.com/questions/3571855/pasting-image-to-clipboard-in-python-in-linux)

#! /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]);

To use it:

python /path/to/imgclip.py filename.png

Note: tested pasting in gimp and xournal. Note: this is for gnome desktop (hence gtk). I bet there's something similar for kde

7

Install xclip (be sure the version is 0.12+svn84, because 0.12 is not enough); then, add this line to the box in the compiz settings manager:

xclip -selection clipboard -target image/png -i

Example in compiz

You should then be able to paste it to any other x application

4

Check out xclip. It allows you to move text, or files to the clipboard from the command line.

EDIT:

There are command-line screenshot apps discussed in this post: http://www.linux.com/archive/feed/57772 including scrot. This is from the scrot man page:

scrot  is  a  screen capture utility using the imlib2 library to aquire
       and save images.  scrot has a  few  options,  detailed  below.  Specify
       [file]  as  the  filename  to save the screenshot to.

One last option is to find an X app which just takes a screenshot when clicked (no prompts, dialogue boxes etc), assign it to a hotkey combination and use xdotool to simulate that keypress from the command line??

5
  • @OP, did you actually need a command line tool, or are you just trying to get a screenshot into the clipboard? There's much easier way to go that
    – Jeremy
    Oct 14 '10 at 4:17
  • Yes, I need a command line tool. Oct 15 '10 at 1:19
  • Unfortunately, both xclip and xsel don't work with images, they work with text contents only. Oct 15 '10 at 1:20
  • Right, my bad, I didn't try pasting into a graphics program, I only tried xclip < image.jpg and xclip -o > image2.jpg which works, but is useless ;(
    – Jeremy
    Oct 16 '10 at 11:56
  • I tested scrot, it works nicely to get a screenshot straight out to a file, but it can't get it into the clipboard :/
    – Jeremy
    Oct 16 '10 at 12:10
3

I'd disable Compiz's screenshot feature, just Head over to System -> Preferences -> Keyboard Shortcuts (under Desktop category) and bind Print Screen to Take a Screenshot

enter image description here


Now whenever you hit PrintScreen a sreencapture will be taken with an option to copy to clipboard, or just to save the file.

enter image description here

3
  • No, I like the screen shot feature comes from Compiz. Oct 15 '10 at 1:07
  • A small question, how do you make the "Print Screen" button in SU? Oct 15 '10 at 1:22
  • 2
    @谢继雷 oh just put the the desired button between <kbd> & </kbd>
    – Sathyajith Bhat
    Oct 15 '10 at 2:31
0

Command line solution

  1. To just copy an image IMG.png stored at $DESTINATION as $DESTINATION/IMG.png, use
xclip -in -selection clipboard -target image/png $DESTINATION/IMG.png

Similarly for IMG.jpg change argument after -target i.e.,

xclip -in -selection clipboard -target image/jpg $DESTINATION/IMG.jpg

If you are taking a screenshot with scrot and want to save as well as copy it, use

# Enter path such as $HOME/Pictures/screenshots/ in $DESTINATION.
$DESTINATION = "/path/to/screenshot/folder/"
scrot "$DESTINATION/%Y-%m-%d-%T.png" &&
{
        SSNAME="$(ls -t $DESTINATION |head -n1)";
        xclip -in -selection clipboard -target image/png "$DESTINATION/$SSNAME" && echo "Copied to clipboard.";
}

Note there is && after scrot command.

ls -t will list and sort according to time. head -n1 will filter only first result i.e., latest one. xclip -in will tell xclip to take input from a file.

You can also give xclip command in scrot with an -e option.

Check this script where I have used above mentioned snippet with notify-send (which is not a big deal :P).

0

Combining this answer with https://stackoverflow.com/questions/35500163/bash-script-with-scrot-area-not-working I use the following script now:

#!/bin/sh

set -e

tmpdir=$(mktemp --tmpdir -d scrot-xclip-XXXXXXX)
trap 'rm -rf "${tmpdir}"' EXIT

setxkbmap -option grab:break_actions
xdotool key XF86Ungrab

scrot "$@" "${tmpdir}"/'screenshot-%Y-%m-%d_%H:%M:%S.png' -e \
    'exec xclip -quiet -t image/png -selection clipboard -i $f'

echo "Clipboard got overwritten"

The idea is to remove the created file after the clipboard gets reused (xclip -quiet blocks until this happens). mktemp also makes sure no other user can read the screenshot file.

I hooked this as /home/user/bin/scrot-xclip into my i3 config:

bindsym Print exec --no-startup-id /home/user/bin/scrot-xclip --select --freeze
bindsym $mod+Print exec --no-startup-id /home/user/bin/scrot-xclip --focused

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.