MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Writing images from manipulate

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117691] Re: Writing images from manipulate
  • From: "W. Craig Carter" <ccarter at mit.edu>
  • Date: Wed, 30 Mar 2011 04:08:01 -0500 (EST)

Hello John,
Thank you very much. Sjoerd De Vries also offered the Button solution---which is working exactly as I had wished.  He also noted my currentgraphic==currentgraphic typo.

He had wondered why I was using CreatePallete part---which you include.  I arrived at using CreatePallette, but I am not sure why but it appeared to be better behaved.  It appears that you use it to make sure the is something to NotebookClose---is that right?

The SynchonusUpdating->True was there because---in the actual application---the simulation is linked to "time". Thus,  I thought that SynchonusUpdating was more likely to give me a constant frame-rate.

As always, your help is appreciated--as is Sjoerd's, Craig



On 25 Mar,   2011, at 4:04 PM, John Fultz wrote:

> Because you're adding the SynchronousUpdating->True option, Mathematica is
> bracketing evaluations with a TimeConstrained[] to make sure that no part of the
> Dynamic ends up hanging the entire system.  That includes the evaluation of
> SystemDialogInput[].  SystemDialogInput[] may take a very long time to evaluate
> because it does *not* return until the user has chosen a file.
>
> In any case, this is a somewhat tortured way of doing things, so let me suggest
> an entirely different approach to doing this.  Instead of the checkbox which
> basically causes a strobe sort of response, it's much better to simply use a
> Button[].  The Button should have the Method->"Queued" option set so that it's
> not going to run into any problems with calling SystemDialogInput[].
>
> So, in your code...
>
> * Remove the If[WriteImage, ...] block
> * Remove the checkbox for WriteImage
> * Add a new control, our Button expression, which will look something like:
>
> Button["Write Image File",
> Module[{nb, nm = SystemDialogInput["FileSave"]},
>  If[nm =!= $Canceled, nb = CreatePalette[nm];
>   Export[nm, currentgraphic];
>   NotebookClose[nb]]], Method -> "Queued"]
>
> Oh, and I'd do the same thing with your "reset to zero" checkbox, too.  I made
> those two things buttons, popped them in a Grid[], did a couple more cleanups,
> and came up with the following:
>
>
> Manipulate[
> If[Evolution === "Go", radii += RandomReal[{0, .0001}, {20}];
>  currentgraphic =
>   Graphics[
>    GraphicsComplex[seeds,
>     Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]];];
> currentgraphic,
> {Evolution, {"Stop", "Go"}, ControlType -> RadioButton,
>  Labeled -> False},
> Grid[{{
>    Button["Write Image File",
>     Module[{nb, nm = SystemDialogInput["FileSave"]},
>      If[nm =!= $Canceled, nb = CreatePalette[nm];
>       Export[nm, currentgraphic];
>       NotebookClose[nb]]], Method -> "Queued"],
>    Button["Reset to Zero", radii = ConstantArray[0, {20}];
>     currentgraphic =
>      Graphics[
>       GraphicsComplex[seeds,
>        Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]]]
>    }}],
> TextCell[
>  "Use .tiff, .gif, or .jpeg, etc, file extensions explicitly for \
> image file formats"], SynchronousUpdating -> True,
> Initialization -> (currentgraphic =
>    currentgraphic =
>     Graphics[
>      GraphicsComplex[seeds,
>       Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]])]
>
>
> Sincerely,
>
> John Fultz
> jfultz at wolfram.com
> User Interface Group
> Wolfram Research, Inc.
>
>
>
> On Thu, 24 Mar 2011 06:32:20 -0500 (EST), W. Craig Carter wrote:
>> Dear MathGroup,
>> I am teaching a laboratory course. As part of one laboratory, students
>> run a simulation on their laptops and then use a generated image in
>> their lab reports.
>>
>> I have a method of writing images from Manipulate, ***but it fails on
>> some but not all*** windows laptops that the students use.
>>
>> I've made as simplified representation of the code and am pasting it
>> here. I hope that it is not too long to receive some advice.
>>
>> The relevant bit is at the If[WriteImage,..] part.  Is there a more
>> reliable way to save the image?
>>
>> Also, I am wondering if the constant reevaluation of the
>> If[Evolution=="go",...] part can be avoided, or should I not worry
>> about it.
>>
>> Just running the Manipulate will give a self-explanatory idea of what
>> the code is doing., Thanks Craig
>>
>>
>> seeds = RandomReal[{0, 1}, {20, 2}]
>> radii = ConstantArray[0, {20}]
>> colors =  Hue /@ RandomReal[{0, 1}, {20}]
>>
>> Manipulate[
>>
>> If[reset,
>> radii = ConstantArray[0, {20}]; reset = False;
>> currentgraphic =
>> currentgraphic =
>> Graphics[
>> GraphicsComplex[seeds,
>> Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]]];
>>
>> If[WriteImage,(*works on a mac, works on *some* windows*)
>>
>> nb = CreatePalette[nm = SystemDialogInput["FileSave"]];
>> WriteImage = False;
>> Export[nm, currentgraphic];
>> NotebookClose[nb]
>> ];
>>
>> If[Evolution == "Go", (*constant evaluation of the if?*)
>> radii += RandomReal[{0, .0001}, {20}];
>> currentgraphic =
>> Graphics[
>> GraphicsComplex[seeds,
>> Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]];
>> ];
>> currentgraphic,
>>
>> {Evolution, {"Stop", "Go"}, ControlType -> RadioButton, Labeled -> False},
>> {{WriteImage, False, "Write Image File"}, {True, False},
>> ControlType -> Checkbox, Labeled -> False},
>> {{reset, False, "reset to zero"}, {True, False}, ControlType -> Checkbox,
>> Labeled -> False},
>> TextCell[
>> "Use .tiff, .gif, or .jpeg, etc, file extensions explicitly for image
>> file \
>> formats"],
>> SynchronousUpdating -> True,
>> Initialization -> (
>> currentgraphic =
>> currentgraphic =
>> Graphics[
>> GraphicsComplex[seeds,
>> Map[{colors[[#]], Disk[#, radii[[#]]]} &, Range[20]]]])
>> ]
>
>


  • Prev by Date: Re: About C[i]
  • Next by Date: Re: Why Mathematica does not issue a warning when the calculations
  • Previous by thread: Re: Writing images from manipulate
  • Next by thread: Importing into Mathematica from URL (PubMed)