1

BACKGROUND

I use Python2.7 and have installed Imagej following the instructions on www.scivision.dev without any issues. The app works fine when run from the terminal.

The Java version I use is

parovelb@Latitude-E6510:~$ java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment (build 11.0.2+9-Ubuntu-3ubuntu118.04.3)
OpenJDK 64-Bit Server VM (build 11.0.2+9-Ubuntu-3ubuntu118.04.3, mixed mode, sharing)

The path to the OpenJDK is set in my ~/.bashrc

# java path
PATH=/usr/lib/jvm/java-11-openjdk-amd64/bin/java:$PATH
export PATH

QUESTION

I have trouble running an ImageJ macro from my script in Python2.7. The script calls an ImageJ macro with subprocess.Popen. The macro creates a CSV file and writes the results into it. Here is my script:

#!/usr/bin/python2.7 python2.7
#!/bin/sh -i
# -*- coding: utf-8 -*-


"""
This program runs an ImageJ macro.
"""


import os
import csv
import sys
import subprocess
from subprocess import PIPE, STDOUT
from collections import defaultdict


def Connector_Check():
    cmd = ["imagej", "-macro", "luerlocktest.ijm"]
    process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
    (stdoutdata, stderrdata) = process.communicate()
    print 'stdoutdata', stdoutdata

    # read result in row 1, col 2
    with open(" ~/results.csv","rb") as res:
        resReader = csv.reader(res)
        resReader = list(resReader)
        val = resReader[1][2]
    print "Connector check value = ", val


if __name__ == "__main__":
    # execute connector check
    Connector_Check()

If I run the command imagej -macro luerlocktest.ijm from the terminal, it works fine but running my script gives me the error:

stdoutdata -macro: 1: -macro: imagej: not found

Traceback (most recent call last):
  File "wrapper_imagej.py", line 34, in <module>
    Connector_Check()
  File "wrapper_imagej.py", line 25, in Connector_Check
    with open("/home/parovelb/Desktop/CV/Results.csv","rb") as res:
IOError: [Errno 2] No such file or directory: '/home/parovelb/Desktop/CV/Results.csv'

These Q&A below unfortunately does not offer a solution to my problem:

  1. How to connect ImageJ to python?
  2. Using ImageJ Jython in Standalone Python IDE
  3. Docker image error: “/bin/sh: 1: python: not found”

Any suggestions on why imagej: not found?

1. UPDATE

Using Shell=False gives the error:

Traceback (most recent call last):
  File "wrapper_imagej.py", line 34, in <module>
    Connector_Check()
  File "wrapper_imagej.py", line 20, in Connector_Check
    process = subprocess.Popen(cmd, shell=False, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT)
  File "/usr/lib/python2.7/subprocess.py", line 394, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1047, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
parovelb
  • 193
  • 1
  • 11

2 Answers2

0

Opening a shell with subprocess doesn't make it execute .bashrc, therefore it will never see the modified version of $PATH. You might try adding the shell=False argument to the Popen() call. Other options include adding imagej to your PATH in another way, or calling imagej with the full path to the program itself, not requiring the $PATH variable at all.

e.g.:

cmd = ["/usr/lib/jvm/java-11-openjdk-amd64/bin/java/imagej", "-macro", "luerlocktest.ijm"]
Joost
  • 2,971
  • 1
  • 9
  • 25
0

I did change the format of the cmd and put the path where ImageJ is installed in $HOME/ImageJ/ImageJ. Also I did use a single command line instead of a list.

Incorrect:

cmd = ["imagej", "-macro", "luerlocktest.ijm"]

Correct:

cmd = ["$HOME/ImageJ/ImageJ -macro luerlocktest.ijm"]
parovelb
  • 193
  • 1
  • 11