Re: Writing images from manipulate
- To: mathgroup at smc.vnet.net
- Subject: [mg117643] Re: Writing images from manipulate
- From: John Fultz <jfultz at wolfram.com>
- Date: Tue, 29 Mar 2011 06:52:24 -0500 (EST)
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]]]]) > ]