astrand/xclip

Image stored as text in the clipboard

mhalano opened this issue · 22 comments

I use i3 (so no GNOME, if it's relevant) and I configured some keystrokes to do screenshots. Some of them are to paste the screenshot directly to the clipboard. This is (one of) the command: maim --select | xclip -selection clipboard -t image/png. The idea about it is quite simple: Do a screenshot of some part of the screen and put in the clipboard. The problem is when I paste the image, more specifically in the message compose screen of Thunderbird (my use case, but not limited to that, Firefox acts on the same way), just the pure text of the file is pasted, not the image. If I copy an image directly (like on Firefox, right-click on image and copy) the paste occurs as expected. I found the content of this link about targets is true, as the same for me, and I think could be helpful for someone debugging the problem: https://unix.stackexchange.com/questions/375002/xclip-image-binary-contents-pasted-into-text-fields

I have this i3 keybinding for the same purpose and it's been working fine for me:

bindsym $mod+z --release exec --no-startup-id maim -s /dev/stdout | xclip -selection clipboard -t image/png

I think the last released version of xclip has some bugs dealing with large screenshots though. This has been fixed but there hasn't been a release (I think?). If you're on arch you might want to try xclip-git AUR package to get the latest version.

@mil-ad I tried with Ubuntu. I will try to compile later.

image

I compiled from source code and the problem remains.

The script xclip-copyfile doesn't work either.

The screenshot you show has JPEG data in it. I don't know about other sites, but gmail requires the data to be in PNG format.

Maybe try piping through 'convert - png:-'

@hackerb9 That is Thunderbird, not the Gmail web interface. After use convert, I got the same problem, but with PNG:
image

I have this i3 keybinding for the same purpose and it's been working fine for me:

bindsym $mod+z --release exec --no-startup-id maim -s /dev/stdout | xclip -selection clipboard -t image/png

I think the last released version of xclip has some bugs dealing with large screenshots though. This has been fixed but there hasn't been a release.

That could be the problem. I submitted the fix for the large files problem a year ago, but then things came up and I lost track of xclip. I don't know if a new release has been made since then. If I recall correctly, we can find out pretty easily. Try this:

cat somelargeimage.png | xclip -i -selection clipboard -t image/png
xclip -o -selection clipboard -t image/png > foo.png
cmp somelargeimage.png foo.png

Also, could you please see if pasting a PNG image into the comments here works? (Not too large, please). I just tried it and it worked for me with xclip version 0.13.

UNIX: Live Free or Die

If that works for you, then it must be that Thunderbird is rejecting the MIME type we offered it. Try copying an image using right-click and then running

xclip -o -selection cilpboard -t TARGETS

That will list what MIME types are being offered for Thunderbird to select from.

@hackerb9 I'm able to paste an image here in GitHub. The problem is when I try to paste in Thunderbird compose screen. Sometimes I have to send selection of captures to other people over email.
image
Here is the capture I made after pasting the content of the capture above:
image
(I did this using Flameshot, not my cup of tea, though).
So you could agree with me the problem appears even in tiny captures.
Here is the result of the second command:
image

Great! Then I think we've narrowed down the problem. Thunderbird is picky about what filetypes it will use. If it doesn't see a particular image format, it falls back to asking for the selection as text (which, of course, fails).

Let's figure out which MIME type Thunderbird is looking for so you can convert your data to something that makes it happy. Use the right-click copy on an image and use that -t TARGETS command again to see what all formats are supported by the application doing the right-click copy. There will be at most a dozen. Try them one by one to see if they make Thunderbird happy. For example,

xclip -i -selection clipboard -t image/bmp < <( convert  somelargeimage.png  bmp:- )

[P.S. I believe there used to be a "clipboard manager" which would could convert any image on the fly based on whatever type was requested. I can't find it now. Maybe someone can refresh my memory. ]

Oh, yeah, I think it was copyq that could do on the fly conversion of images. I believe you had to double-click on the image in the clipboard manager to tell it to paste into the current window.

I checked the mimetypes of an image on the clipboard and got this:

➜ xclip -o -selection cilpboard -t TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlinfo
text/_moz_htmlcontext
image/png
image/bmp
image/x-bmp
image/x-MS-bmp
image/x-icon
image/x-ico
image/x-win-bitmap
image/vnd.microsoft.icon
application/ico
image/ico
image/icon
text/ico
image/jpeg
image/tiff

But I had an out-side-of-the-box thinking and pasted an image on Thunderbird and, after that, I copied again and checked one more time the mimetypes of the clipboard. I got this:

➜ xclip -o -selection cilpboard -t TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain
text/x-moz-url-priv

Convert to BMP using any mimetype didn't work, the same for JPEG or PNG. I think the secret is one of this text formats, but I don't know how to convert to it.

You copied the image from within Thunderbird? And it didn't show any images available?

If so, Thunderbird isn't seeing it as an image at all, it must be an HTML reference of some sort. What is the output of

xclip -o -sel clipboard -t text/html

If that's not useful, what about the moz targets? I believe "url-priv" can also specify a file on your local computer.

Convert to BMP using any mimetype didn't work, the same for JPEG or PNG. I think the secret is one of this text formats, but I don't know how to convert to it.

Oh, I had meant convert to each filetype and give the appropriate mimetype. It looks like the only other likely image format on your list would be tiff:

xclip -i -selection clipboard -t image/tiff < <( convert  somelargeimage.png  tiff:- )

TIFF also produced gibberish

Now that's a very fascinating thing: I copied the image you posted above (live free or die Unix...) and got the text/html mimetype:
image
But... I did a selection using Flameshot, saved in the clipboard and checked the targets. I got this:

➜ xclip -o -selection cilpboard -t TARGETS   
application/x-qt-image
image/png
image/bmp
image/cur
image/icns
image/ico
image/jpeg
image/jpg
image/pbm
BITMAP
image/pgm
image/ppm
PIXMAP
image/tif
image/tiff
image/wbmp
image/webp
image/xbm
image/xpm
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS

Flameshot is just offering to convert the image type for any application that is looking for a specific format. Clipboard managers can do that. I wish xclip could do it without entailing large dependencies on image libraries.

I just zoomed in on your image and it looks like there was a misspelling "clipboard". Could you try again?

You are right. I did again a selection with Flameshot and executed (the correct) command to see the targets.

image

So, pasting from flameshot when it has those targets worked?

Also, when you copy from Thunderbird, without using flameshot, what targets are available?

Yes, I'm able to paste the content Flameshot put in the clipboard.

For this test, I inserted an image using the appropriate menu option in the Thunderbird compose screen, and after that, I copied the image. I got this targets than.

TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
text/html
text/_moz_htmlcontext
text/_moz_htmlinfo
text/x-moz-url-priv
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain

I'm thinking it would be converted to HTML. That is the image when I convert to text/html:
image

Oh, how odd! Thunderbird wants the image to be encoded as an "inline" (base64) image? That would be pretty gross, but doable.

I think the conversion would look something like this:

( echo -n '<meta http-equiv="content-type"';
  echo -n ' content="text/html; charset=utf-8"><p><img src="'; 
  convert myimage.png  inline:-; 
  echo '">')  |  xclip -i -sel clipboard -t text/html

Still, it seems highly unlikely to me that an inline image is what Thunderbird is insisting on.