Copying a non PNG image to the X clipboard

1k Views Asked by At

Several questions have already been answered regarding copying images to the X clipboard, and the answer is always to use xclip like so:

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

This works, but only for PNG files. You might imagine that changing the -t image/png to the correct MIME type, such as image/jpg or image/jpeg for JPEG files, would easily solve the problem, but this simply does not work:

xclip -selection clipboard -t image/jpeg -i other_image.jpg

The image is copied to the clipboard, but is not recognized by other applications as an image. In my case, copying a JPEG image, Discord refuses to paste any content, while Firefox pastes it as if it was text, resulting in some high quality Unicode soup of random CJK characters.

What is the correct way to copy a non PNG image to the X clipboard using xclip?

3

There are 3 best solutions below

1
On

I think image/jpeg format is not supported by xclip based on my researches, anyway you always can hack in somewhat, below the solution I found, it will use convert to convert the image from jpeg to png and paste the result to the out stream, then pass this stream to xclip in png format so it can copy the image to the clipboard.

convert image.jpg png:- | xclip -selection clipboard -t image/png
0
On

It seems that copying the image to the clipboard is one thing but pasting it into an application, be it Firefox or Chromium, is another thing. I found that it is more efficient to copy the file URI to the clipboard

i=/foo/far/img.jpeg
echo "file://$i" | xclip -selection clipboard -t text/uri-list

To date I am using the code with chromium and firefox for both .jpg images and small (<25M) .mp4 videos

0
On
xclip -selection clipboard -t image/png < file_path.png

worked for me. But when i used jpeg files it did not work