1

In a java application I need to use a specific image processing algorithm that is currently implemented in python. What would be the best approach, knowing that this script uses the Numpy library ?

I alreayd tried to compile the script to java using the jythonc compiler but it seems that it doesn't support dependencies on native libraries like Numpy. I also tried to use Jepp but I get an ImportError when importing Numpy, too.

Any suggestion ?

3 Answers 3

3

If you're using Numpy you probably have to just use C Python, as it's a compiled extension. I'd recommend saving the image to disk, perhaps as a temporary file, and then calling the Python as a subprocess. If you're dealing with binary data you could even try memory mapping the data in Java and passing in in the path to the subprocess.

Alternatively, depending on your circumstances, you could set up a simple data processing server in Python which accepts requests and returns processed data.

0

While Joe's answer may be simple, you will have performance issue if the image is big (2 complete write and read). I am no Java expert, but according to http://wiki.cacr.caltech.edu/danse/index.php/Communication_between_Java_and_Python#Communication_through_bindings, you can developp c <--> java bindings. You can do the same with python (with ctypes or cython if you don't want to learn the python c api).

With those, you can build yourself a nice python <--> java bridge in c, and pass around a pointer to the image data (which will be faster than writing and reading it on the hard drive).

2
  • Are are you suggesting passing a pointer to a memory buffer in the Java process onto a native Python function? Are you sure you can do that? And FWIW the Python C API coudldn't be nicer! Commented Oct 25, 2011 at 22:52
  • As I said, I am not a java developper and I don't know the platform very well. I know the python C API is nice, and so is the numpy API. But to write a python C extension for numpy array processing, Cython is way easier to learn. Commented Oct 26, 2011 at 7:46
0

For using numpy within a Java process, Jep now supports numpy. You can also use jpy or JyNI.

With those libraries you could pass the images between Java and Python as byte arrays and ndarrays as you do your calculations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.