Rasterize [] Width Control for Notebook Related Expressions

Refresh . The Wizard answer gives a different result than a pixel, but it is only for Windows and destroys the contents of the clipboard. My answer should work on any platform, but it is less accurate: for example. It skips I / O labels. This allows you to set the width of the rasterization.




This problem arose when I was trying to make a preview window for the image downloader (see the end of this answer).

I would like to create a palette button that will load the current selection of the laptop as an image. Before downloading, I would like to show a preview image to reduce the likelihood that something will go wrong before accessing the server.

This is what I have so far (includes only the preview code, not the loader):

button = Button[ "Preview", Module[ {expr = NotebookRead@InputNotebook[]}, If[expr =!= {}, With[{img = Rasterize[expr]}, MessageDialog[ Column[{"Would you like to perform the action?", img}], {"Do it!" :> doIt[img], "Cancel" :> Null} ] ] ] ] ] 

In case you are wondering why I used the nested With inside the Module instead of making img modular variable too: this is because by the time doIt[img] local module is calculated, the variables will be cleared, so I need to substitute the rasterized expression directly into the function doIt

This button works (more or less). You can try it by creating graphics in one laptop (for example, Graphics[Circle[]] ), selecting it with one click, then clicking the "Preview" button.

However, if I placed it in the palette using CreatePalette[button] , then rasterization will occur for the width of the palette window, and I will get something like this:

Screenshots of the problem

How can I control the width of the rasterization or, more generally, how can I create a preview dialog for a user who avoids this problem?

For further improvement, it would be nice to be able to resize the message box to fit the preview image (and still show the button: the button disappears with WindowSize -> All ).




The answers

Mr. The offer of the master:

 button = Button[ "Preview", (FrontEndExecute[ FrontEndToken[FrontEnd`SelectedNotebook[], "CopySpecial", "MGF"]]; MessageDialog[ First@Cases[NotebookGet@ClipboardNotebook[], RasterBox[data_, ___] :> Image[data, "Byte", ColorSpace -> "RGB", Magnification -> 1], Infinity]])] CreatePalette[button] 

Problems: It (possibly) only works on Windows, and it destroys the contents of the clipboard.

+4
wolfram-mathematica mathematica-frontend
Dec 02 2018-11-12T00:
source share
4 answers

I managed to do this by copying the selection to a new notebook, rasterizing the full notebook and closing it.

 CreatePalette@Button["Preview", Module[{target}, target = CreateDocument[{}, WindowSelected -> False, Visible -> False]; NotebookWrite[target, NotebookRead[SelectedNotebook[]]]; CreateDialog[{Rasterize[target], DefaultButton[]}]; NotebookClose[target] ] ] 

The WindowSize -> 500 can be added to CreateDocument to set the width of the rasterization to 500 pixels.




Pay attention to some disadvantages (advantages in some cases) of this method compared to copying as a bitmap:

  • Custom styles are lost.
  • I / O characters are lost
  • Notebook increase value lost

If necessary, some of them can be eliminated by explicitly transferring some laptop settings from SelectedNotebook to the newly created one.

+1
Dec 30 '11 at 10:15
source share

If it is convenient to use the clipboard in this operation, you can use: FrontEnd`CopySpecial["MGF"] (copy as bitmap).

+2
Dec 02 2018-11-11T00:
source share

Have you tried using ExportString [] to create graphics in memory? (technically into a temporary file, but what do you care:])

 ExportString[your_mathematica_stuff_here,"PNG",Background->None] 

See the output on a colored background to check the transparent background graphics:

 Framed[ImportString[ExportString[x^2,"PNG",Background->None] ,"PNG"] ,Background->Yellow] 

For images with many color variations (such as 3D graphics), I recommend the JPEG2000 format, and for solid images where transparency is not required, use GIF to preserve color details.

Yes, you can control ImageSize when exporting an image string.


ExportString output and image type / size comparison

0
Dec 05 2018-11-12T00:
source share

I think this should work without having to create a new laptop:

 button = Button["Preview", Module[{expr = NotebookRead@InputNotebook[]}, If[expr =!= {}, With[{img = Rasterize[expr, ImageFormattingWidth -> First@(WindowSize /. AbsoluteOptions[InputNotebook[], WindowSize])]}, MessageDialog[ Column[{"Would you like to perform the action?", img}], {"Do it!" :> doIt[img], "Cancel" :> Null}, WindowSize -> {First@ImageDimensions@img, All}]]]]]; CreateDialog[button, WindowFloating -> True, WindowClickSelect -> False, Selectable -> False ] 

I used a search engine with few options to find ImageFormattingWidth , and by going through the width of the image as the width of the window, you can make the dialog suitable for the image and still display the button.

Here is a demonstration of its results:

Rasterize cells from the palette

0
26 Oct '16 at 21:29
source share



All Articles