如何在python界面显示图片
如何在python的GUI界面显示打开的图像文件,比如Tkinter(或者wxpython),如何在界面显示出要打开的图片,需要什么控件吗?如果有,怎么调用?能否给个具体...
如何在python的GUI界面显示打开的图像文件,比如Tkinter(或者wxpython),如何在界面显示出要打开的图片,需要什么控件吗?如果有,怎么调用?
能否给个具体实现的例子,可以运行的 展开
能否给个具体实现的例子,可以运行的 展开
2个回答
展开全部
wxpython:
# 使用wx.Image得到对象
bmp = wx.Image('bitmaps/image.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
gif = wx.Image('bitmaps/image.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap()
png = wx.Image('bitmaps/image.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
jpg = wx.Image('bitmaps/image.jpg', wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
# 把它们显示出来
pos = 10
wx.StaticBitmap(frame, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))
pos = pos + bmp.GetHeight() + 10
wx.StaticBitmap(frame, -1, gif, (10, pos), (gif.GetWidth(), gif.GetHeight()))
pos = pos + gif.GetHeight() + 10
wx.StaticBitmap(panel, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))
pos = pos + png.GetHeight() + 10
wx.StaticBitmap(frame, -1, jpg, (10, pos), (jpg.GetWidth(), jpg.GetHeight()))
具体的请根据你的实际情况修改,最好去http://www.wxpython.org/download.php#binaries
下载wxpython和wxpython demo看看,这个demo很强大的。
# 使用wx.Image得到对象
bmp = wx.Image('bitmaps/image.bmp', wx.BITMAP_TYPE_BMP).ConvertToBitmap()
gif = wx.Image('bitmaps/image.gif', wx.BITMAP_TYPE_GIF).ConvertToBitmap()
png = wx.Image('bitmaps/image.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
jpg = wx.Image('bitmaps/image.jpg', wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
# 把它们显示出来
pos = 10
wx.StaticBitmap(frame, -1, bmp, (10, pos), (bmp.GetWidth(), bmp.GetHeight()))
pos = pos + bmp.GetHeight() + 10
wx.StaticBitmap(frame, -1, gif, (10, pos), (gif.GetWidth(), gif.GetHeight()))
pos = pos + gif.GetHeight() + 10
wx.StaticBitmap(panel, -1, png, (10, pos), (png.GetWidth(), png.GetHeight()))
pos = pos + png.GetHeight() + 10
wx.StaticBitmap(frame, -1, jpg, (10, pos), (jpg.GetWidth(), jpg.GetHeight()))
具体的请根据你的实际情况修改,最好去http://www.wxpython.org/download.php#binaries
下载wxpython和wxpython demo看看,这个demo很强大的。
展开全部
代码:
# show a jpeg (.jpg) image using wxPython, newer coding style
# two different ways to load and display are given
# tested with Python24 and wxPython25 vegaseat 24jul2005
import wx
import cStringIO
class Panel1(wx.Panel):
""" class Panel1 creates a panel with an image on it, inherits wx.Panel """
def __init__(self, parent, id):
# create the panel
wx.Panel.__init__(self, parent, id)
try:
# pick a .jpg file you have in the working folder
imageFile = 'Moo.jpg'
data = open(imageFile, "rb").read()
# convert to a data stream
stream = cStringIO.StringIO(data)
# convert to a bitmap
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
# show the bitmap, (5, 5) are upper left corner coordinates
wx.StaticBitmap(self, -1, bmp, (5, 5))
# alternate (simpler) way to load and display a jpg image from a file
# actually you can load .jpg .png .bmp or .gif files
jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
# bitmap upper left corner is in the position tuple (x, y) = (5, 5)
wx.StaticBitmap(self, -1, jpg1, (10 + jpg1.GetWidth(), 5), (jpg1.GetWidth(), jpg1.GetHeight()))
except IOError:
print "Image file %s not found" % imageFile
raise SystemExit
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# increase the size of the frame for larger images
frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300))
# call the derived class
Panel1(frame1,-1)
frame1.Show(1)
app.MainLoop()
# show a jpeg (.jpg) image using wxPython, newer coding style
# two different ways to load and display are given
# tested with Python24 and wxPython25 vegaseat 24jul2005
import wx
import cStringIO
class Panel1(wx.Panel):
""" class Panel1 creates a panel with an image on it, inherits wx.Panel """
def __init__(self, parent, id):
# create the panel
wx.Panel.__init__(self, parent, id)
try:
# pick a .jpg file you have in the working folder
imageFile = 'Moo.jpg'
data = open(imageFile, "rb").read()
# convert to a data stream
stream = cStringIO.StringIO(data)
# convert to a bitmap
bmp = wx.BitmapFromImage( wx.ImageFromStream( stream ))
# show the bitmap, (5, 5) are upper left corner coordinates
wx.StaticBitmap(self, -1, bmp, (5, 5))
# alternate (simpler) way to load and display a jpg image from a file
# actually you can load .jpg .png .bmp or .gif files
jpg1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
# bitmap upper left corner is in the position tuple (x, y) = (5, 5)
wx.StaticBitmap(self, -1, jpg1, (10 + jpg1.GetWidth(), 5), (jpg1.GetWidth(), jpg1.GetHeight()))
except IOError:
print "Image file %s not found" % imageFile
raise SystemExit
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# increase the size of the frame for larger images
frame1 = wx.Frame(None, -1, "An image on a panel", size = (400, 300))
# call the derived class
Panel1(frame1,-1)
frame1.Show(1)
app.MainLoop()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询