使用wxPython显示缩略图的简单方法

2024-06-07 07:21:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一个使用wxPython显示缩略图的简单解决方案。这不是关于创建缩略图。我有一个缩略图目录,想在屏幕上显示它们。我故意不使用(面板、框架、窗口、滚动窗口)之类的术语,因为我对各种解决方案持开放态度。在

另外请注意,我发现了多个显示单个图像的示例,因此引用任何此类解决方案都不会对我有所帮助。解决方案必须是在wx中同时显示多个图像。在

我想做的似乎是在ThumbnailCtrl中完成的,但是Andrea的代码很复杂,我找不到执行屏幕显示的部分。我在marklutz的Python编程书中找到了一个简单的解决方案,但是当他的查看器_拇指.py这个例子绝对具有我所寻找的简单性,它是用Tkinter完成的。在

所以请任何wx解决方案将不胜感激。在

编辑:我正在添加一个链接到一个地方,在那里可以找到马克·卢茨的工作代码。有人能想到wx的等价物吗?在

http://codeidol.com/community/python/viewing-and-processing-images-with-pil/17565/#part-33


Tags: 代码图像目录框架面板示例屏幕wxpython
3条回答

不知道我是否应该回答我自己的问题,但我确实找到了解决问题的方法,我想和大家分享。我用的是wx2.8版。我发现在2.9和3.0中有一个小部件被添加到WrapSizer。有一次我把wx版本升级到了3.0版本,这使得解决方案变得非常简单。下面是重要的代码片段。在

    self.PhotoMaxWidth = 100
    self.PhotoMaxHeight = 100

    self.GroupOfThumbnailsSizer = wx.WrapSizer()      

    self.CreateThumbNails(len(ListOfPhotots),ListOfPhotots)

    self.GroupOfThumbnailsSizer.SetSizeHints(self.whateverPanel) 
    self.whateverPanel.SetSizer(self.GroupOfThumbnailsSizer)

    self.whateverPanel.Layout()


def CreateThumbNails(self, n, ListOfFiles):
    thumbnails = []
    backgroundcolor = "white"

    for i in range(n):

        ThumbnailSizer = wx.BoxSizer(wx.VERTICAL)
        self.GroupOfThumbnailsSizer.Add(ThumbnailSizer, 0, 0, 0)
        thumbnails.append(ThumbnailSizer)

    for thumbnailcounter, thumbsizer in enumerate(thumbnails):

        image = Image.open(ListOfFiles[thumbnailcounter])

        image = self.ResizeAndCenterImage(image, self.PhotoMaxWidth, self.PhotoMaxHeight, backgroundcolor)

        img = self.pil_to_image(image)

        thumb= wx.StaticBitmap(self.timelinePanel, wx.ID_ANY, wx.BitmapFromImage(img))

        thumbsizer.Add(thumb, 0, wx.ALL, 5)

    return

def pil_to_image(self, pil, alpha=True):
    """ Method will convert PIL Image to wx.Image """
    if alpha:
        image = apply( wx.EmptyImage, pil.size )
        image.SetData( pil.convert( "RGB").tostring() )
        image.SetAlphaData(pil.convert("RGBA").tostring()[3::4])
    else:
        image = wx.EmptyImage(pil.size[0], pil.size[1])
        new_image = pil.convert('RGB')
        data = new_image.tostring()
        image.SetData(data)
    return image

def ResizeAndCenterImage(self, image, NewWidth, NewHeight, backgroundcolor):
    width_ratio = NewWidth / float(image.size[0])
    temp_height = int(image.size[1] * width_ratio)
    if temp_height < NewHeight:
        img2 = image.resize((NewWidth, temp_height), Image.ANTIALIAS)
    else:
        height_ratio = NewHeight / float(image.size[1])
        temp_width = int(image.size[0] * height_ratio)
        img2 = image.resize((temp_width, NewHeight), Image.ANTIALIAS)

    background = Image.new("RGB", (NewWidth, NewHeight), backgroundcolor)
    masterwidth = background.size[0]
    masterheight = background.size[1]
    subwidth = img2.size[0]
    subheight = img2.size[1]
    mastercenterwidth = masterwidth // 2
    mastercenterheight = masterheight // 2
    subcenterwidth = subwidth // 2
    subcenterheight = subheight // 2
    insertpointwidth = mastercenterwidth - subcenterwidth
    insertpointheight = mastercenterheight - subcenterheight
    background.paste(img2, (insertpointwidth, insertpointheight))

    return background

我从另一个stackoverflow帖子中得到了pil-tu-to-tu图像部分,我写了ResizeAndCenterImage部分,使我所有的缩略图大小相同,同时保持纵横比不变,不做任何裁剪。如果您愿意,可以一起跳过resize和center调用。在

我会把它们显示为wx图像在框架内。在

http://www.wxpython.org/docs/api/wx.Image-class.html

来自班级:“一个独立于平台的图像类。图像可以从数据创建,也可以使用wx.Bitmap.ConvertToImage,或以各种格式从文件加载。函数可用于设置和获取图像位,因此可用于基本图像操作。”

似乎它应该能做你想做的,除非我遗漏了什么。在

我建议使用ThumbNailCtrl小部件:http://wxpython.org/Phoenix/docs/html/lib.agw.thumbnailctrl.html。wxPython演示中有一个很好的例子。或者你可以使用文档中的这个。请注意,ThumbNailCtrl需要安装Python映像库。在

import os

import wx
import wx.lib.agw.thumbnailctrl as TC

class MyFrame(wx.Frame):

    def __init__(self, parent):

        wx.Frame.__init__(self, parent, -1, "ThumbnailCtrl Demo")

        panel = wx.Panel(self)

        sizer = wx.BoxSizer(wx.VERTICAL)

        thumbnail = TC.ThumbnailCtrl(panel, imagehandler=TC.NativeImageHandler)
        sizer.Add(thumbnail, 1, wx.EXPAND | wx.ALL, 10)

        thumbnail.ShowDir(os.getcwd())
        panel.SetSizer(sizer)


# our normal wxApp-derived class, as usual

app = wx.App(0)

frame = MyFrame(None)
app.SetTopWindow(frame)
frame.Show()

app.MainLoop()

只需换行缩略图.ShowDir(操作系统getcwd())使其指向计算机上正确的文件夹。在

我还在这里写了一篇关于查看照片的文章:http://www.blog.pythonlibrary.org/2010/03/26/creating-a-simple-photo-viewer-with-wxpython/不过它不使用缩略图。在

相关问题 更多 >