4
$\begingroup$

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.

$\endgroup$
8
  • 1
    $\begingroup$ 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. $\endgroup$
    – N.J.Evans
    Nov 6, 2018 at 16:36
  • 1
    $\begingroup$ 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) $\endgroup$
    – andre314
    Nov 6, 2018 at 16:37
  • $\begingroup$ @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. $\endgroup$
    – MeMyselfI
    Nov 6, 2018 at 16:46
  • 1
    $\begingroup$ @andre Yes, but when using those short cuts the behavior is the exact same as that of im // CopyToClipboard. $\endgroup$
    – MeMyselfI
    Nov 6, 2018 at 16:48
  • 2
    $\begingroup$ Strongly related: "Color degrades when using Copy As Bitmap." Check the "Workaround" section in the accepted answer. $\endgroup$ Nov 7, 2018 at 1:34

1 Answer 1

2
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.