Skip to main content
 首页 » 编程设计

python之使用 Sikuli/Jython 打开图像文件而不用点击

2025年05月04日6shanyou

我正在编写一个将图像文件保存到桌面的脚本(所以是“image.JPG”)。这样做之后,我想打开图像文件,以便它显示在屏幕上。我一直在尝试使用子流程(我没有这方面的经验),但似乎无法让它工作。

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

但是,这会导致以下错误:

[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 ) 

我假设这是因为它是一个文件而不是一个应用程序(也许我必须打开一个允许您查看图像的应用程序?)

那么,有没有办法不用用sikuli双击就可以在sikuli/jython/python中打开一个图像文件呢?我也不想下载任何额外的包。

谢谢。

请您参考如下方法:

如果是普通的 Python,你会使用 os.startfile() :

Start a file with its associated application.

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

但是,问题是存在于 Python 中但在 Jython 中缺失的东西(基于 C 的功能)。 os.startfile() 就是其中之一。

subprocess.call()应该在 Jython 中工作:

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

还可以在以下位置查看更多选项: