Is there a way to run a python script form a macro?
i tried call(ij.plugin.Macro_Runner.runPython(pathToPythonScript, ""); but that did not work. Thank is advance --aryeh -- Aryeh Weiss Faculty of Engineering Bar Ilan University Ramat Gan 52900 Israel Ph: 972-3-5317638 FAX: 972-3-7384051 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Aryeh,
On Sun, Nov 15, 2015 at 2:40 PM, Aryeh Weiss <[hidden email]> wrote: > > Is there a way to run a python script form a macro? > i tried > call(ij.plugin.Macro_Runner.runPython(pathToPythonScript, ""); > but that did not work. There were 2 things that needed to be changed: jythonText = File.openAsString(pathToPythonScript); call("ij.plugin.Macro_Runner.runPython", jythonText, ""); ... namely: 1) One needs to put quotes around the first class.method parameter to call(...) as described in the function reference (http://imagej.nih.gov/ij/developer/macro/functions.html#C). 2) Instead of passing the path to the Jython file, one must instead read in the file text for runPython(...) to interpret. On the other hand if you are not trying to run an ImageJ Jython script (e.g. using ImageJ specific imports, etc) and are instead running a CPython script (https://learnwithtina.wordpress.com/2014/07/20/what-is-the-difference-between-python-and-cpython/) in a "traditional" Python interpreter outside of ImageJ in a separate process one would use exec(...): exec("python", pathToPythonScript). Note with exec(...) one must use commas to separate any part of the command that requires spaces. For example while one may normally run "python -c print(123)" on the command-line, in exec it is: output = exec("python", "-c", "print(123)"); print(output); Also note that "output" only seems to contain stdout not stderr *. See the exec(...) examples here: http://imagej.nih.gov/ij/macros/ExecExamples.txt > --aryeh Pariksheet * Maybe to also see stderr one should use e.g. 2>&1 redirection, but my weird system hangs on exec("sh", "-c", "ls") alone although exec("ls") works fine. A mystery for another time. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
> On Nov 15, 2015, at 5:16 PM, Pariksheet Nanda <[hidden email]> wrote:
> > Hi Aryeh, > > On Sun, Nov 15, 2015 at 2:40 PM, Aryeh Weiss <[hidden email]> wrote: >> >> Is there a way to run a python script form a macro? >> i tried >> call(ij.plugin.Macro_Runner.runPython(pathToPythonScript, ""); >> but that did not work. You can also run a python script using the eval() macro function, for example: script = File.openAsString(pathToPythonScript); eval("python", script); -wayne > There were 2 things that needed to be changed: > > jythonText = File.openAsString(pathToPythonScript); > call("ij.plugin.Macro_Runner.runPython", jythonText, ""); > > ... namely: > 1) One needs to put quotes around the first class.method parameter to > call(...) as described in the function reference > (http://imagej.nih.gov/ij/developer/macro/functions.html#C). > 2) Instead of passing the path to the Jython file, one must instead > read in the file text for runPython(...) to interpret. > > > On the other hand if you are not trying to run an ImageJ Jython script > (e.g. using ImageJ specific imports, etc) and are instead running a > CPython script (https://learnwithtina.wordpress.com/2014/07/20/what-is-the-difference-between-python-and-cpython/) > in a "traditional" Python interpreter outside of ImageJ in a separate > process one would use exec(...): > > exec("python", pathToPythonScript). > > Note with exec(...) one must use commas to separate any part of the > command that requires spaces. For example while one may normally run > "python -c print(123)" on the command-line, in exec it is: > > output = exec("python", "-c", "print(123)"); > print(output); > > Also note that "output" only seems to contain stdout not stderr *. > See the exec(...) examples here: > http://imagej.nih.gov/ij/macros/ExecExamples.txt > > >> --aryeh > > Pariksheet > > > * Maybe to also see stderr one should use e.g. 2>&1 redirection, but > my weird system hangs on exec("sh", "-c", "ls") alone although > exec("ls") works fine. A mystery for another time. -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Many thanks to Pariksheet and Wayne. Both methods work.
The first time I ran them, I was prompted to download Jython.jar to the plugins folder. Then, I think ImageJ attempted to refresh menus, but as I run Fiji, I had to restart the program. Then my test script for running python from a macro worked. Best regards, --aryeh On 16/11/2015 3:02 AM, Rasband, Wayne (NIH/NIMH) [E] wrote: >> On Nov 15, 2015, at 5:16 PM, Pariksheet Nanda <[hidden email]> wrote: >> >> Hi Aryeh, >> >> On Sun, Nov 15, 2015 at 2:40 PM, Aryeh Weiss <[hidden email]> wrote: >>> Is there a way to run a python script form a macro? >>> i tried >>> call(ij.plugin.Macro_Runner.runPython(pathToPythonScript, ""); >>> but that did not work. > You can also run a python script using the eval() macro function, for example: > > script = File.openAsString(pathToPythonScript); > eval("python", script); > > -wayne > > >> There were 2 things that needed to be changed: >> >> jythonText = File.openAsString(pathToPythonScript); >> call("ij.plugin.Macro_Runner.runPython", jythonText, ""); >> >> ... namely: >> 1) One needs to put quotes around the first class.method parameter to >> call(...) as described in the function reference >> (http://imagej.nih.gov/ij/developer/macro/functions.html#C). >> 2) Instead of passing the path to the Jython file, one must instead >> read in the file text for runPython(...) to interpret. >> >> >> On the other hand if you are not trying to run an ImageJ Jython script >> (e.g. using ImageJ specific imports, etc) and are instead running a >> CPython script (https://learnwithtina.wordpress.com/2014/07/20/what-is-the-difference-between-python-and-cpython/) >> in a "traditional" Python interpreter outside of ImageJ in a separate >> process one would use exec(...): >> >> exec("python", pathToPythonScript). >> >> Note with exec(...) one must use commas to separate any part of the >> command that requires spaces. For example while one may normally run >> "python -c print(123)" on the command-line, in exec it is: >> >> output = exec("python", "-c", "print(123)"); >> print(output); >> >> Also note that "output" only seems to contain stdout not stderr *. >> See the exec(...) examples here: >> http://imagej.nih.gov/ij/macros/ExecExamples.txt >> >> >>> --aryeh >> Pariksheet >> >> >> * Maybe to also see stderr one should use e.g. 2>&1 redirection, but >> my weird system hangs on exec("sh", "-c", "ls") alone although >> exec("ls") works fine. A mystery for another time. > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- Aryeh Weiss Faculty of Engineering Bar Ilan University Ramat Gan 52900 Israel Ph: 972-3-5317638 FAX: 972-3-7384051 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Free forum by Nabble | Edit this page |