4

I want to copy an image in mathematica to use in another program.

E.g:

im = ExampleData[{"TestImage", "House"}]

When I evaluate im // CopyToClipboard and paste in another program, the image is destroyed.

Irfanview:

Paint is similar just slightly brighter:

I know I can export as the image .png and get the right thing. It also works using print screen followed by cropping in the target program. Is there no way to make it work correctly by simple using the clipboard?

I'm using V11.3 on Windows.

MeMyselfI
  • 1,116
  • 5
  • 12
  • 1
    Can you describe 'destroyed' a little more, you don't describe the images you're showing so I don't know what I'm supposed to take from them. Is only the red square copied? Do you not like the artifacts? On my Mac I can open the copied image up in Preview and it looks fine - might be an OS issue. – N.J.Evans Nov 06 '18 at 16:36
  • 1
    Are you aware that you can simply select the image (just click on it], then copy-paste it in another program (control-C control-V on windows) – andre314 Nov 06 '18 at 16:37
  • @N.J.Evans ImageDimensions[im] is {256, 256}, but when I evaluate im // CopyToClipboard and paste in paint, the dimension change to {256, 255}, a margin of white pixels appears, and artifacts are introduced all over the image pixels. In free clipboard viewer 3.0 for windows, the artifacts appears too. – MeMyselfI Nov 06 '18 at 16:46
  • 1
    @andre Yes, but when using those short cuts the behavior is the exact same as that of im // CopyToClipboard. – MeMyselfI Nov 06 '18 at 16:48
  • @MeMyselfI , ok, I don't see those artifacts, or a margin. Dimensions look good too, I'm guessing it's a windows problem. You might submit a bug report to Wolfram. Someone here might have a workaround. – N.J.Evans Nov 06 '18 at 16:52
  • 2
    Strongly related: "Color degrades when using Copy As Bitmap." Check the "Workaround" section in the accepted answer. – Alexey Popkov Nov 07 '18 at 01:34
  • @AlexeyPopkov Thank you for that link, but unfortunately when using that workaround function to copy an image, I can only paste in Mathematica. However, I have found solution now. – MeMyselfI Nov 07 '18 at 11:26
  • @MeMyselfI Actually you can also paste in MS Word, Excel, PowerPoint and other applications which support importing the PNG file format from clipboard. – Alexey Popkov Nov 07 '18 at 11:59

1 Answers1

2

Downloading and extracting http://www.nirsoft.net/utils/nircmd-x64.zip makes the function

copy = (Export["C:\\nircmd\\asd.png", #];
    RunProcess[{"C:\\nircmd\\nircmdc.exe",
                "clipboard", "copyimage", "\"C:\\nircmd\\asd.png\""}];) &;

copy images from mathematica (as well as everything that exports properly as PNG), and I get the same correct result when pasting in all my programs.

When using RunProcess no console window pops up unlike when Run is used.

Update: It is better to combine the code above with that of this answer.
I.e let the file $UserBaseDirectory/Autoload/FrontEnd/init.m contain

FrontEndExecute[FrontEnd`AddMenuCommands["Copy", {MenuItem["Copy Image",
 FrontEndExecute[Module[{}, FrontEndExecute[FrontEndToken["Copy"]];
  RunProcess[{"C:\\nircmd\\nircmdc.exe", "clipboard", "copyimage",
   "\"" <> Export["C:\\nircmd\\asd.png", NotebookGet[ClipboardNotebook[]]] <> "\""}];]],
 MenuKey["c", Modifiers -> {"Control", "Command"}],
   System`MenuEvaluator -> Automatic, Method -> "Queued"]}]]

which makes Alt Gr+c copy images correctly.

MeMyselfI
  • 1,116
  • 5
  • 12