3
$\begingroup$

How to Export LocatorPane as image by button or at least by something else?

Kuba, thank you! Your method works in a simple case:

DynamicModule[{pt = {{1, 1}/2, {-1, 1}/2, {1, -1}/2}}, {Dynamic@LocatorPane[Dynamic[pt],
Graphics[{Gray, Disk[]}],
Appearance -> Graphics[{Red , Disk[Scaled[{.5, .5}], .2], Inset["a", Scaled[{.1, .1} + 0.5]]}, PlotRange -> {{0, 10}, {0, 10}}]]}]

Button["Export", Export[FileNameJoin[{$HomeDirectory, "Desktop", "expimg.png"}], Rasterize@ToExpression@First@NotebookRead@Experimental`PreviousCell[]]]

But in my case need to fix something. This code results an image with initial positions of locators:

hkl = {-1, 2, 3};

Manipulate[Pane[
  DynamicModule[{pt = {{1000, 1059}/2, {1000, 1059}/2.2}},
   Column[
    {Dynamic@
      LocatorPane[Dynamic[pt], 
       Image[Import[
         "http://download.hdd.tomsk.ru/preview/warpaheh.jpg"], 
        ImageSize -> Zoom],


       Appearance -> {
         Graphics[{Red, 
           Disk[Scaled[{1000, 1059}/(2*{1000, 1059})], 
            Dynamic[PointRadius]]}, 
          PlotRange -> {{0, 1000}, {0, 1059}}, 
          ImageSize -> Dynamic[Zoom]],
         Dynamic[
          Style[Row[hkl], RGBColor[TextColor, TextColor, TextColor], 
           Zoom*PointRadius/100]]
         }]

     }]]
  , ImageSize -> {1000, 1000}, Scrollbars -> True, 
  ScrollPosition -> {150, 200}],
 Row[{
   Column[{
     Row[{Control[{{TextColor, 0.5}, 0, 
          1}] Control[{{TextColor, 0.5, ""}, {0, 0}, {1, 1}, 
          InputField[#, FieldSize -> {4, 1}] &}]}], 
     Row[{Control[{{Zoom, 1000}, 100, 3000, 
          100}] Control[{{Zoom, 1000, ""}, {100, 100}, {3000, 3000}, 
          InputField[#, FieldSize -> {4, 1}] &}]}], 
     Row[{Control[{{PointRadius, 3}, 1, 
          20}] Control[{{PointRadius, 3, ""}, {1, 1}, {20, 20}, 
          InputField[#, FieldSize -> {4, 1}] &}]}],
     }, Right]
   }, Spacer[50]]
 ]
Button["Export", 
 Export[FileNameJoin[{$HomeDirectory, "Desktop", "expimg.png"}], 
  Rasterize@
   ToExpression@First@NotebookRead@Experimental`PreviousCell[]]]

How to implement the export of this picture?

How to export an image with modified locators?

Please, look at my code. Why it still doesn't work? https://drive.google.com/file/d/0B_R6abD1r7RXU0NrZS1GdGF3U1E/edit?usp=sharing

$\endgroup$
4
  • $\begingroup$ Hi, do you want the whole Manipulate to be exported or only the graphics part? Sorry for the delay. $\endgroup$
    – Kuba
    May 25, 2014 at 9:51
  • $\begingroup$ Kuba, you're a genius!!! This is exactly what I need! Thank you so much! $\endgroup$
    – user10431
    May 25, 2014 at 11:04
  • $\begingroup$ All credits to Mathematica.SE community since most things I've learned here :) $\endgroup$
    – Kuba
    May 25, 2014 at 11:38
  • $\begingroup$ Please, take a tour $\endgroup$
    – Kuba
    May 27, 2014 at 11:16

1 Answer 1

3
$\begingroup$

I have reformulated your code a little bit. If you want to make complicated GUIs I think it is better to leave Manipulate and do all that with DynamicModule, Sliders etc.

The point about exporting. Thanks to the chat with Rojo, it seems that the most reliable way to export the state of something that Dynamic[vars] are involved is to put the Button in the same scope as vars are. So here I'm putting it inside DynamicModule but you can easily make pt Global` variable.

I was not sure what do you want to export at the end so this is only a graphical part of the code, easy to change.

Old solutions are in edit history. They work but only for simple examples, and only because of side effects.

DynamicModule[{controls, pt, TextColor, Zoom, PointRadius, locatorpane, loc, hkl},
 Dynamic[Refresh[
   Column[{
     controls,         
     Pane[Dynamic[loc = locatorpane ], ImageSize -> {500, 500}, Scrollbars -> True,
                                        ScrollPosition -> {150, 200}]         
     }, Left],
   None]],

 Initialization :> (
   pt = {{1000, 1059}/2, {1000, 1059}/2.2};
   TextColor = .5;
   Zoom = 500;
   PointRadius = 3;
   hkl = {-1, 2, 3};

   controls = Grid[{
      {"TextColor", Slider[Dynamic@TextColor, {0, 1}], InputField[Dynamic@TextColor, FieldSize -> {4, 1}]},
      {"Zoom", Slider[Dynamic@Zoom, {100, 3000, 100}], InputField[Dynamic@Zoom, FieldSize -> {4, 1}]},
      {"PointRadius", Slider[Dynamic@PointRadius, {1, 20}], InputField[Dynamic@PointRadius, FieldSize -> {4, 1}]},
      {Button["Export", Print[Rasterize@loc]], ""}
      }];

   locatorpane = LocatorPane[Dynamic[pt],         
     Image[Import["http://download.hdd.tomsk.ru/preview/warpaheh.jpg"], 
           ImageSize -> Dynamic@Zoom], 
     Appearance -> {Graphics[{Red, Dynamic@AbsolutePointSize@PointRadius,
                              Point[{0, 0}]}],           
                    Dynamic[Style[Row[hkl], RGBColor[TextColor, TextColor, TextColor], 
                                 Zoom*PointRadius/100]]}];           
   )]

enter image description here

$\endgroup$

Your Answer

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

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