Search code examples
pythonjythonsikuli

Opening an image file using Sikuli/Jython without clicking


I'm writing a script that saves an image file to the desktop (so "image.JPG"). After doing that, I want to open the image file so that it is displayed on the screen. I've been trying to use subprocess (which I have no experience with), but can't seem to get it to work.

import subprocess
subprocess.Popen("C:/Users/first.last/Desktop/image.JPG")

However, that results in the following error:

[error] OSError ( Cannot run program "C:/Users/sean.sheehan/Desktop/image.JPG" (in directory "C:\SikuliX"): CreateProcess error=193, %1 is not a valid Win32 application )

I'm assuming this is because it is a file rather than an application (maybe I have to open an application that allows you to view an image?)

So, is there a way to open an image file in sikuli/jython/python without having to double click it with sikuli? I'd also prefer not to download any additional packages.

Thanks.


Solution

  • If it was a normal Python, you would use os.startfile():

    Start a file with its associated application.

    import os
    
    filename = "C:/Users/first.last/Desktop/image.JPG"
    os.startfile(filename)
    

    But, the problem is that the are things (C-based functionality) that exist in Python, but are missing in Jython. And os.startfile() is one of these things.

    subprocess.call() should work in Jython:

    import subprocess
    
    filename = "C:/Users/first.last/Desktop/image.JPG"
    subprocess.call('start ' + filename)
    

    Also see more options at: