Python: 在默认图片查看器中打开多张图片
Python: Open multiple images in default image viewer
我正在使用 PIL 在默认图像查看器中打开单个图像:
from PIL import Image
img = Image.open('example.jpg')
img.show()
是否有任何 Python 模块包含在当前系统的默认图像查看器中启用打开多个图像 的功能?例如,当在 OS X 上时,Preview.app 应该打开侧边栏中的图像列表。从命令行这完全没有问题:
$ open my_picture_number_*
用例是用户应该只能浏览几十张图片。
使用subprocess.run
到运行操作系统的默认图像查看应用程序。 subprocess.run 很像命令行。您只需要知道您所使用的操作系统的命令是什么。对于 windows,"explorer" 即可;对于 OS X,正如您指出的那样,它是 "open." 我不确定它对于 Linux 是什么,也许 "eog"?
因此,您的代码将如下所示:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.run([imageViewerFromCommandLine, path])
我尝试使用@jgfoot 的答案,该答案有效,但在查看器启动后使我的程序挂起。我已经通过使用 subprocess.Popen 解决了这个问题,像这样:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.Popen([imageViewerFromCommandLine, path])
我正在使用 PIL 在默认图像查看器中打开单个图像:
from PIL import Image
img = Image.open('example.jpg')
img.show()
是否有任何 Python 模块包含在当前系统的默认图像查看器中启用打开多个图像 的功能?例如,当在 OS X 上时,Preview.app 应该打开侧边栏中的图像列表。从命令行这完全没有问题:
$ open my_picture_number_*
用例是用户应该只能浏览几十张图片。
使用subprocess.run
到运行操作系统的默认图像查看应用程序。 subprocess.run 很像命令行。您只需要知道您所使用的操作系统的命令是什么。对于 windows,"explorer" 即可;对于 OS X,正如您指出的那样,它是 "open." 我不确定它对于 Linux 是什么,也许 "eog"?
因此,您的代码将如下所示:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.run([imageViewerFromCommandLine, path])
我尝试使用@jgfoot 的答案,该答案有效,但在查看器启动后使我的程序挂起。我已经通过使用 subprocess.Popen 解决了这个问题,像这样:
import sys
import subprocess
def openImage(path):
imageViewerFromCommandLine = {'linux':'xdg-open',
'win32':'explorer',
'darwin':'open'}[sys.platform]
subprocess.Popen([imageViewerFromCommandLine, path])