在Python中显示控制台中的图像
从Python控制台显示在Python中显示控制台中的图像,python,image,Python,Image,从Python控制台显示.jpg或.gif图像的最简单方法是什么 我有一个Python控制台程序,它正在检查一个数据集,该数据集包含指向本地存储的图像的链接。我应该如何编写脚本,以便在弹出的图形窗口中显示图像?您不能在控制台窗口中显示图像。 您需要一个图形化工具包,如Tkinter、PyGTK、PyQt、PyKDE、wxPython、PyObjC或PyFLTK。 关于如何在python中创建简单窗口和加载图像,有很多教程。使用awesome库: 这将在默认图像查看器中打开图像。为什么不在用户的w
.jpg
或.gif
图像的最简单方法是什么
我有一个Python控制台程序,它正在检查一个数据集,该数据集包含指向本地存储的图像的链接。我应该如何编写脚本,以便在弹出的图形窗口中显示图像?您不能在控制台窗口中显示图像。 您需要一个图形化工具包,如Tkinter、PyGTK、PyQt、PyKDE、wxPython、PyObjC或PyFLTK。 关于如何在python中创建简单窗口和加载图像,有很多教程。使用awesome库:
这将在默认图像查看器中打开图像。为什么不在用户的web浏览器中显示图像?或者只需通过shell执行图像,如中所示
import subprocess
subprocess.call([ fname ], shell=True)
无论安装了什么处理图像的程序,都将启动。因为您可能正在运行Windows(通过查看标签),这将是从控制台打开和显示图像文件的最简单方法,而无需安装额外的东西,如PIL
import os
os.system('start pic.png')
在Xterm兼容终端中,您可以直接在终端中显示图像。看 在新窗口中使用枕头/PIL 安装(或PIL),例如: 现在你可以
from PIL import Image
with Image.open('path/to/file.jpg') as img:
img.show()
使用本机应用程序
其他常见的替代方法包括运行xdg open
或使用图像路径启动浏览器:
import webbrowser
webbrowser.open('path/to/file.jpg')
内联Linux控制台
如果您真的想在控制台内联显示图像,而不是作为新窗口显示,您可以这样做,但只能在使用see或其他类似ASCII艺术的方式下进行。我制作了一个简单的工具,可以显示给定文件名或图像对象或url的图像
这很粗糙,但很快就可以了 安装:
$ pip install simple-imshow
用法:
from simshow import simshow
simshow('some_local_file.jpg') # display from local file
simshow('http://mathandy.com/escher_sphere.png') # display from url
如果您想在新窗口中显示它,可以使用Tkinter+PIL库,如下所示:
import tkinter as tk
from PIL import ImageTk, Image
def show_imge(path):
image_window = tk.Tk()
img = ImageTk.PhotoImage(Image.open(path))
panel = tk.Label(image_window, image=img)
panel.pack(side="bottom", fill="both", expand="yes")
image_window.mainloop()
这是一个经过修改的示例,可以在web上找到。您还可以使用Python模块
Ipython
,它除了在Spyder控制台中显示图像外,还可以在Jupyter笔记本中嵌入图像。在Spyder中,图像将以全尺寸显示,而不是缩放以适应控制台
from IPython.display import Image, display
display(Image(filename="mypic.png"))
如果要在本机图像查看器中打开图像,请尝试
os.startfile
:
导入操作系统
os.startfile('文件')
或者,您可以使用GUI库将图像设置为背景,然后在需要时显示它。但是这种方式会使用更多的代码,并且可能会影响脚本运行所需的时间。但它确实允许您自定义ui。下面是一个使用wxpython的示例:
import wx
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_PAINT) # Was wx.BG_STYLE_CUSTOM)
self.frame = parent
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(4):
label = "Button %s" % num
btn = wx.Button(self, label=label)
sizer.Add(btn, 0, wx.ALL, 5)
hSizer.Add((1,1), 1, wx.EXPAND)
hSizer.Add(sizer, 0, wx.TOP, 100)
hSizer.Add((1,1), 0, wx.ALL, 75)
self.SetSizer(hSizer)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
#----------------------------------------------------------------------
def OnEraseBackground(self, evt):
"""
Add a picture to the background
"""
# yanked from ColourDB.py
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("file")
dc.DrawBitmap(bmp, 0, 0)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, size=(600,450))
panel = MainPanel(self)
self.Center()
########################################################################
class Main(wx.App):
""""""
#----------------------------------------------------------------------
def __init__(self, redirect=False, filename=None):
"""Constructor"""
wx.App.__init__(self, redirect, filename)
dlg = MainFrame()
dlg.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = Main()
app.MainLoop()
(源代码来自)
您甚至可以使用timg
在终端中显示图像:
import timg
obj = timg.Renderer()
obj.load_image_from_file("file")
obj.render(timg.SixelMethod)
(PyPI:)此解决方案是特定于Windows的(即将cmd.exe作为shell),在默认配置(如sh、bash等)下,它不适用于通用*nix shell。事实上,
webbrowser.open()
适用于您可以从浏览器打开的任何文件类型:图像、视频、office文档、mp3等(它启动了相应的程序,例如视频文件的MPlayer)。我认为这是一个非常简洁的横向思维。但它只适用于绝对路径。我期待着webbrowser.open(“Figure.png”)
将通过默认的internet浏览器打开图形。相反,图形是通过默认的图像查看器打开的。你知道为什么吗?我在Windows上。webbrowser
有webbrowser.\u tryorder
包含它将尝试的程序顺序。在我的计算机上它有['xdg-open'、'gvfs open'、…
,因此首先调用xdg open
程序。该程序在我的计算机上进一步调用eog
。在Python 2.4及更高版本中,子进程优先于os.system。但这并不能回答问题。它给出了一个错误“在中找不到图像”file:///tmp/tmp...PNG“@Röyal这是因为PIL.Image.show()是一个调试工具。要使用它,您需要分别为Linux或Windows安装xz或paint。如果您显示一个图像,保持其打开状态,然后显示另一个图像,也会遇到此错误。
import wx
########################################################################
class MainPanel(wx.Panel):
""""""
#----------------------------------------------------------------------
def __init__(self, parent):
"""Constructor"""
wx.Panel.__init__(self, parent=parent)
self.SetBackgroundStyle(wx.BG_STYLE_PAINT) # Was wx.BG_STYLE_CUSTOM)
self.frame = parent
sizer = wx.BoxSizer(wx.VERTICAL)
hSizer = wx.BoxSizer(wx.HORIZONTAL)
for num in range(4):
label = "Button %s" % num
btn = wx.Button(self, label=label)
sizer.Add(btn, 0, wx.ALL, 5)
hSizer.Add((1,1), 1, wx.EXPAND)
hSizer.Add(sizer, 0, wx.TOP, 100)
hSizer.Add((1,1), 0, wx.ALL, 75)
self.SetSizer(hSizer)
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
#----------------------------------------------------------------------
def OnEraseBackground(self, evt):
"""
Add a picture to the background
"""
# yanked from ColourDB.py
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("file")
dc.DrawBitmap(bmp, 0, 0)
########################################################################
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, size=(600,450))
panel = MainPanel(self)
self.Center()
########################################################################
class Main(wx.App):
""""""
#----------------------------------------------------------------------
def __init__(self, redirect=False, filename=None):
"""Constructor"""
wx.App.__init__(self, redirect, filename)
dlg = MainFrame()
dlg.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = Main()
app.MainLoop()
import timg
obj = timg.Renderer()
obj.load_image_from_file("file")
obj.render(timg.SixelMethod)