【问题标题】:Scale python UI image without downrezing it (with wxpython, PIL or other)缩放 python UI 图像而不缩小它(使用 wxpython、PIL 或其他)
【发布时间】:2018-09-05 11:56:16
【问题描述】:

正如标题所说,我的 wxpython 界面中有一个太大的图像,但是当我将其缩小时(在 Photoshop 中或使用 wxpython/PIL 方法),它实际上只是进一步像素化了它。

有没有办法缩小图像以适应 UI 但保留其细节?

使用 Python、PIL (Pillow)、wxPython,但可以使用任何东西

【问题讨论】:

    标签: python image user-interface wxpython python-imaging-library


    【解决方案1】:

    您需要检查您使用的工具是否有resample 选项,该选项将更改生成图像中的像素数。重采样算法决定添加或删除像素时会发生什么。

    您可以在 wxpython 中对图像使用 Scale()Rescale() 选项,并使用 quality 参数说明应如何重新采样。

    img.Scale(width, height, quality=wx.IMAGE_QUALITY_HIGH)
    

    quality 参数将接受以下选项:

    wx.IMAGE_QUALITY_NEAREST        Simplest and fastest algorithm.
    wx.IMAGE_QUALITY_BILINEAR       Compromise between wx.IMAGE_QUALITY_NEAREST and wx.IMAGE_QUALITY_BICUBIC.
    wx.IMAGE_QUALITY_BICUBIC        Highest quality but slowest execution time.
    wx.IMAGE_QUALITY_BOX_AVERAGE    Use surrounding pixels to calculate an average that will be used for new pixels.
    wx.IMAGE_QUALITY_NORMAL         Default image resizing algorithm used by wx.Image.Scale .
    wx.IMAGE_QUALITY_HIGH           Best image resizing algorithm.
    

    根据您的评论,这是一个简单的示例。
    只需调整图像的窗口大小即可跟踪窗口大小的变化。

    import wx
    
    class MyFrame ( wx.Frame ):
    
        def __init__( self, parent ):
            wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = "Resize This Window", size = wx.Size( 500,300 ))
            bSizer = wx.BoxSizer( wx.HORIZONTAL )
            self.img1=wx.Image("bgimage1.png", wx.BITMAP_TYPE_PNG)
            self.img2=wx.Image("bgimage1.png", wx.BITMAP_TYPE_PNG)
            self.m_bitmap1 = wx.StaticBitmap( self, wx.ID_ANY, wx.Bitmap(self.img1))
            self.m_bitmap2 = wx.StaticBitmap( self, wx.ID_ANY, wx.Bitmap(self.img2))
            bSizer.Add( self.m_bitmap1, 1, wx.EXPAND|wx.ALL, 0 )
            bSizer.Add( self.m_bitmap2, 1, wx.EXPAND|wx.ALL, 0 )
            self.Bind(wx.EVT_SIZE, self.onResize)
            self.SetSizer( bSizer )
            self.Layout()
            self.Centre(wx.BOTH)
    
        def onResize(self, event):
            frame_size = self.GetSize()
            frame_h = (frame_size[0]-10) / 2
            frame_w = (frame_size[1]-10) / 2
            img1 = self.img1.Scale(frame_h, frame_w, quality=wx.IMAGE_QUALITY_HIGH)
            img2 = self.img2.Scale(frame_h, frame_w, quality=wx.IMAGE_QUALITY_HIGH)
            self.m_bitmap1.SetBitmap(wx.Bitmap(img1))
            self.m_bitmap2.SetBitmap(wx.Bitmap(img2))
            self.Refresh()
            self.Layout()
    
    app = wx.App()
    MyFrame(None).Show()
    app.MainLoop()
    

    【讨论】:

    • 这是我到处读到的解决方案,但我仍然无法让它工作。我收到这个错误:AttributeError: 'Bitmap' object has no attribute 'Scale'我已经尝试了几乎所有的变体都无济于事:image_file.Scale(200, 200, quality=wx.IMAGE_QUALITY_HIGH) image_file = wx.Image('bgimage.png', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
    猜你喜欢
    • 2011-10-31
    • 2018-05-13
    • 2019-10-13
    • 1970-01-01
    • 2016-05-24
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    相关资源
    最近更新 更多