0
$\begingroup$

How can I run the code in a cell using the "button" function. I have a big code and instead of using "Shift + Enter", I want a button to do it. Here is the code:

data1 = ImageData[imgT];

{nRaw, nCol, nChan} = Dimensions[data1];

d = data1[[All, All, 2]];

d = d*(-1)^Table[i + j, {i, nRaw}, {j, nCol}];

fourieT = Fourier[d, FourierParameters -> {1, 1}];

abs = Log[1 + Abs [fourieT]];

Labeled[Image[abs/Max[abs], ImageSize -> 250], 

 Style["Amplitude Spectrum", 20]]

Moreover, I want to save this file and immediately exit the program:

Button["Save and quite", 
 img = Export[fileFull = SystemDialogInput["FileSave", ".jpg"], 
   Image[abs/Max[abs], ImageSize -> 1000]], Method -> "Queued"]
$\endgroup$
4
  • $\begingroup$ where does imgT come from? For the rest, when using Method->"Queued" you basically just can copy the code into the second argument of Button, or better make it a function and put the function call there instead... $\endgroup$ Commented Mar 12, 2019 at 19:54
  • $\begingroup$ I imported imgT earlier $\endgroup$ Commented Mar 12, 2019 at 19:58
  • $\begingroup$ so would you like to do the import and the processing all be initiated by the button? In that case you would have to do another SystemDialogInput["FileOpen"] to let the user choose the file to import, then import, process the data, ask user for the file to save and finally do the export. Is that what you plan to do? If so, you might edit the question to make it more clear what you are after... $\endgroup$ Commented Mar 12, 2019 at 20:35
  • $\begingroup$ Yes, this is what I wanted. The user selects a file to import(in this case: Button["Import File", {imgT = Import[fileFull = SystemDialogInput["FileOpen"]]). Further process the data. Then the user presses the button and saves the converted image. After that, I want the program to close, after the user saved the image. Something like the "Save and Exit" button $\endgroup$ Commented Mar 12, 2019 at 20:59

2 Answers 2

2
$\begingroup$

Here is the most simple version which would do what I think you try to achieve:

Button[
  "Convert Picture",
  imgT = Import[SystemDialogInput["FileOpen", ".jpg"]];
  data1 = ImageData[imgT];
  {nRaw, nCol, nChan} = Dimensions[data1];
  d = data1[[All, All, 2]];
  d = d*(-1)^Table[i + j, {i, nRaw}, {j, nCol}];
  fourieT = Fourier[d, FourierParameters -> {1, 1}];
  abs = Log[1 + Abs[fourieT]];
  Export[SystemDialogInput["FileSave", ".jpg"], 
  Image[abs/Max[abs], ImageSize -> 1000]],
  Method -> "Queued"
]

Of course there are several things you might want to improve once you start using it. First of all it makes sense to create a function from the code and only call that code in the button. Next it is good practice to localize the variables you are using and to check whatever input comes back from the user. Finally I would separate the functionality of getting the filename from the pure algorithm which will make it much more convenient to make changes of the code and test these changes. The final result of such changes could look like this:

createSpectrum[imgT_Image] := 
  Module[{data1, nRaw, nCol, nChan, d, fourieT, abs},
    data1 = ImageData[imgT];
    {nRaw, nCol, nChan} = Dimensions[data1];
    d = data1[[All, All, 2]];
    d = d*(-1)^Table[i + j, {i, nRaw}, {j, nCol}];
    fourieT = Fourier[d, FourierParameters -> {1, 1}];
    abs = Log[1 + Abs[fourieT]];
    Image[abs/Max[abs], ImageSize -> 1000]
 ];

 Button["Convert Picture",
  Module[{filein = SystemDialogInput["FileOpen", ".jpg"], fileout, spectrum},
    If[StringQ[filein] && FileExistsQ[filein],
      spectrum = createSpectrum[Import[filein]];
      fileout = SystemDialogInput["FileSave", ".jpg"];
      If[StringQ[fileout], Export[fileout, spectrum]]
    ]
  ],
  Method -> "Queued"
]

Of course there are still many things to improve, like better error checking, a progress indication while the spectrum is calculated, some plan for how to deploy etc. Whether or not it is worth to invest such efforts depends of course on how the code is going to be used. You will find many answers on this site to help you with any of these details -- or just ask another question...

$\endgroup$
1
$\begingroup$

You can do this :

codeToBeExecuted:= (  

data1 = ImageData[imgT];

{nRaw, nCol, nChan} = Dimensions[data1];

d = data1[[All, All, 2]];

d = d*(-1)^Table[i + j, {i, nRaw}, {j, nCol}];

fourieT = Fourier[d, FourierParameters -> {1, 1}];

abs = Log[1 + Abs [fourieT]];

Labeled[Image[abs/Max[abs], ImageSize -> 250], 

 Style["Amplitude Spectrum", 20]]  
)  

Button["execute the code",codeToBeExecuted,Method-> "Queued"]
$\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.