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
.
@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 – 2010-10-14T04:17:11.320
Yes, I need a command line tool. – Xiè Jìléi – 2010-10-15T01:19:01.223
Unfortunately, both
xclip
andxsel
don't work with images, they work with text contents only. – Xiè Jìléi – 2010-10-15T01:20:58.767Right, my bad, I didn't try pasting into a graphics program, I only tried
xclip < image.jpg
andxclip -o > image2.jpg
which works, but is useless ;( – Jeremy – 2010-10-16T11:56:59.923I tested scrot, it works nicely to get a screenshot straight out to a file, but it can't get it into the clipboard :/ – Jeremy – 2010-10-16T12:10:00.073