缩放python UI图像而不缩小其大小(使用wxpython,PIL或其他)

时间:2018-09-05 11:56:16

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

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

是否有一种方法可以缩小图像以适合UI,但保留其细节?

使用Python,PIL(枕头),wxPython,但可以使用任何东西

1 个答案:

答案 0 :(得分:0)

您需要检查所使用的工具是否具有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()