• wxPython 读取 SVG(3)


    wxPython 读取 SVG

    wx.Image 支持图片格式如下所示:

    • wx.BITMAP_TYPE_BMP -- Load a Windows bitmap file.
    • wx.BITMAP_TYPE_GIF -- Load a GIF bitmap file.
    • wx.BITMAP_TYPE_JPEG -- Load a JPEG bitmap file.
    • wx.BITMAP_TYPE_PNG -- Load a PNG bitmap file.
    • wx.BITMAP_TYPE_PCX -- Load a PCX bitmap file.
    • wx.BITMAP_TYPE_PNM -- Load a PNM bitmap file.
    • wx.BITMAP_TYPE_TIFF -- Load a TIFF bitmap file.
    • wx.BITMAP_TYPE_TGA -- Load a TGA bitmap file.
    • wx.BITMAP_TYPE_XPM -- Load a XPM bitmap file.
    • wx.BITMAP_TYPE_ICO -- Load a Windows icon file (ICO).
    • wx.BITMAP_TYPE_CUR -- Load a Windows cursor file (CUR).
    • wx.BITMAP_TYPE_ANI -- Load a Windows animated cursor file (ANI).
    • wx.BITMAP_TYPE_ANY -- Will try to autodetect the format.

    读取 SVG 则需要使用 wxPython 提供的一个专门的 SVG 读取类 wx.svg.SVGimage,它是继承 wx.svg.SVGimageBase

    继承关系

    method instruction
    CreateFromBytes Loads an SVG image from a bytes object.
    CreateFromFile Loads an SVG image from a file.
    Rasterize Renders the SVG image to a bytes object as a series of RGBA values.
    RasterizeToBuffer Renders the SVG image to an existing buffer as a series of RGBA values.
    • wx.svg.SVGimage 有关的方法,SVGimage 可以调用其父类的方法
    method instruction
    ConvertToBitmap Creates a wx.Bitmap containing a rasterized version of the SVG image.
    ConvertToScaledBitmap Automatically scales the SVG image so it will fit in the given size,
    RenderToGC Draw the collection of shapes and paths in the SVG image

    参考代码

    from wx.svg import SVGimage
    
    def svg2bmp(svg_fliename, bmp_size):
        """SVG 转换为 bitmap
    
        Args:
            svg_fliename (str): SVG 文件路径
            bmp_size (wx.Sizer): bitmap 大小
    
        Returns:
            wx.Bitmap: 对应大小的位图
        """
        img = SVGimage.CreateFromFile(svg_fliename)
        bmp = img.ConvertToScaledBitmap(bmp_size)
        return bmp
    

    一个简单的演示

    # -*- encoding: utf-8 -*-
    # Python 3.9.6 64bit
    '''
    @File        : read_svg.py
    @Time        : 2022/01/06 14:53
    @Author      : Wreng
    @Description : 使用 wxpython 读取 SVG 简单演示
    @Other       : version - Python 3.9.6 64bit, wxPython 4.1.1
    '''
    
    import wx
    from wx.svg import SVGimage
    
    class SVGRendderPanel(wx.Panel):
    
        def __init__(self, parent, bmp_size, *args, **kw):
            super().__init__(parent, *args, **kw)
            
            self.bmp_size = wx.Size(*bmp_size)
            self.statbmp = wx.StaticBitmap(self, bitmap=wx.Bitmap(*self.bmp_size))
            label = '{}x{}'.format(self.bmp_size.width, self.bmp_size.height)
    
            sbox = wx.StaticBoxSizer(wx.VERTICAL, self, label)
            sbox.Add(self.statbmp)
    
            self.SetSizer(sbox)
    
        def UpdateSVG(self, svg_filename):
            img = SVGimage.CreateFromFile(svg_filename)
            bmp = img.ConvertToScaledBitmap(self.bmp_size, self)
            self.statbmp.SetBitmap(bmp)
    
    class TestFrame(wx.Frame):
    
        def __init__(self, *args, **kw):
            super().__init__(*args, **kw)
    
            self.svg_name = ".\\vscode-icon\\default_folder_opened.svg"
    
            self.SetBackgroundColour('white')
            self.SetTitle(self.svg_name.split('\\')[-1])
    
            sbox = wx.BoxSizer(wx.HORIZONTAL)
            
            for d in [32, 64, 128]:
                sbd = SVGRendderPanel(self, (d,d))
                sbd.UpdateSVG(self.svg_name)
                sbox.Add(sbd, 0, wx.ALL, 10)
            
            self.SetSizer(sbox)
    
    if __name__ == '__main__':
    
        app = wx.App()
        frm = TestFrame(None)
        frm.Show()
        app.MainLoop()
    

    运行结果:

    运行结果

    图标来自 vscode-icons,可点此下载


    相关参考

  • 相关阅读:
    mysql 主从复制
    通过git-bash 批量管理VMware虚拟机
    MySQL基础
    lnmp架构
    搭建yum仓库服务器
    什什么是集群?么是分布式?
    nginx介绍1
    网络抓包工具 wireshark 入门教程
    DNS原理总结及其解析过程详解
    PetaPoco中使用Exists
  • 原文地址:https://www.cnblogs.com/wreng/p/15771548.html
Copyright © 2020-2023  润新知