How does exec("dir /w"); function work in imageJ? - python

How do I use exec() in imageJ, http://fiji.sc/ImageJ_tricks#Execute_external_programs? Can I use the exec() function to launch an external python program with something like:
exec("python", d:\py\program1.py");
Is it possible to return an image array to exec from python?

The exec() macro function is hard coded to return STDOUT, as outlined in Curtis Rueden's answer to a similar question on the ImageJ mailing list.
However, you can run Python (or, for that matter, Jython) scripts directly from within Fiji via the script editor or by placing them into the Fiji.app/plugins/ folder.

For anyone interested in using additional python libraries with imageJ, exec() can get the job done. Use:
exec("python", "d:\\pathtoyour\\pythonprogram.py");
Save this using the imageJ script editor as .ijm file. This can then be installed as a plugin to launch your python program from imageJ.

Related

Automatic GUI for python script with command line arguments?

Is there any software that auto generates GUI wrappers around python scripts?
My specific scenario is that i wrote a simple script for my father in law to bulk download some stuff from a given url.
Normally you just run the script via
python my_script.py --url https://test.com --dir C:\Downloads
and it just downloads all the relevant files from test.com to the Downloads folder.
I think he might be able to handle that but i am not sure and so i was thinking if there is any simple software out there that would allow me to take the script and turn it into an executable that just asks for all arguments and then has a simple run button to execute the script and download the things.
Ideally this would mean that he doesnt have to install python but at the very least allow for easier handling for him.
I am aware that there are libraries that allow for the creation of custom GUIs for python but thought that maybe there already exists something simpler and generic for my very simple and i also think fairly common use case.
I ended up using PyInstaller (thanks #Dexty) and rewriting the script to grab the arguments by asking for them via input.
Not exactly a GUI but that still allows the user to just double click the .exe and go from there instead of having to proactively use a CLI.

QGIS - Cannot run executable .exe file or run matlab script through Python Console

i have matlab script which using few matlab libraries like Map ToolKit. I can run this script on MATLAB without any error. But i need to run this with QGIS and get output files.
Simply, script asks few question about Map Tools and get like .shp files and give output folder.
So to get these output files, i tried 3 ways:
I tried to run this code on Python Console. For that i installed matlab.engine and called script just like in documentation. But these matlab.engine doesn't let python to use matlab's own libraries so error on using shapeinfo function. It just let python to use few variables i think. So after failing on this i tried second way:
I created executable matlab file to run .exe file on python console. In here, there is a two way to get this exe file. First one is let you smaller file and doesn't include matlab runtime so you need to add matlab runtime on your path and i did it. Actually Matlab does that as default and i checked that it was okey. Executable file runs perfectly after clicking or on normal python scripts which is running on through CMD. But if i call Python Script in QGIS, it trying to open but returns Matlab runtime dll missing error. After i tried to compile with matlab runtime selection and results were same.
Lastly, i tried to run matlab.exe -r "try, run SCRPIT_PATH\script.m; end; quit on CMD and it works fine but if i call this on QGIS Python Console with os.system or subprocess.call functions, cmd appearing and closing immediately.
I guess QGIS doesn't let you to open some other applications somehow and i need to let it run. Or i just need to find import matlab libraries to python for work perfectly. I would be pleasure if you give any suggestion. Any solution for these ways or a different way would be great. Thanks.

python or pythonw in creating a cross-platform standalone GUI app

I am developing a simple standalone, graphical application in python. My development has been done on linux but I would like to distribute the application cross-platform.
I have a launcher script which checks a bunch of environment variables and then sets various configuration options, and then calls the application with what amounts to python main.py (specifically os.system('python main.py %s'% (arg1, arg2...)) )
On OS X (without X11), the launcher script crashed with an error like Could not run application, need access to screen. A very quick google search later, the script was working locally by replacing python main.py with pythonw main.py.
My question is, what is the best way to write the launcher script so that it can do the right thing across platforms and not crash? Note that this question is not asking how to determine what platform I am on. The solution "check to see if I am on OS X, and if so invoke pythonw instead" is what I have done for now, but it seems like a somewhat hacky fix because it depends on understanding the details of the windowing system (which could easily break sometime in the future) and I wonder if there is a cleaner way.
This question does not yet have a satisfactory answer.
If you save the file as main.pyw, it should run the script without opening up a new cmd/terminal.
Then you can run it as python main.pyw
Firstly, you should always use .pyw for GUIs.
Secondly, you could convert it to .exe if you want people without python to be able to use your program. The process is simple. The hardest part is downloading one of these:
for python 2.x: p2exe
for python 3.x: cx_Freeze
You can simply google instructions on how to use them if you decide to go down that path.
Also, if you're using messageboxes in your GUI, it won't work. You will have to create windows/toplevels instead.

How do you write commands in blender from a python script run outside blender?

I am trying to find a wat to add virices in a subprocess.Popen opened blender using the script that opened it to write the bpy codes into the blender proces. If this isn't possible, is there a wat to make the blender python receive (through a script run in blender) messages from the python script run in terminal (I am using fedora)
Thanks in advance
Indeed - you have thought half the answer for yourself -
The blender modules won't be available from outside blender, but
you can have a script inside blender to receive data from outside.
The easiest way would be to use XMLRPC - use a script that
loads with Blender, and stars a Python XMLRPC server -
then you will be able to send commands into that script from
outside.
It is easier than it sounds - check the Python documentation
for XMLRPC (it will even allow you to have python 2.7 scripts
outside blender communicating with the Python 3 that
runs inside blender) -
http://docs.python.org/3.3/library/xmlrpc.server.html#module-xmlrpc.server

Running Fiji script in python

I am trying to get a home made Fiji script to sun inside Python by calling Fiji, but there's little documentation on how to do it.
What I need is something like this:
def myfijiscript:
[CODE]
and then in Python:
fiji(myfijiscript)
is there a way to do this?
Python (or, to be precise, Jython) scripts within Fiji are executed using the org.python.util.PythonInterpreter class (see source code).
It doesn't make much sense to run a Jython script within a Java instance that is started from with Python, but have a look at those two questions concerning how to run external commands in python. You can save your script in a file myscript.py and then do:
call(["./ImageJ-linux64", "myscript.py"])
using the ImageJ launcher from the command line.
The other way is to use ImageJ as a library and just import the classes you need for your script, as others have suggested:
from ij import IJ

Categories

Resources