zoukankan      html  css  js  c++  java
  • 使用 wx.tools.img2py (4)

    img2py.py 将图像转换为PNG格式,并将其嵌入Python模块,以便在运行时将其加载到程序中。这样做的好处是,可以通过*.pyc来调用或使用freeze, py2exe等编译到程序中

    具体用法:wx.tools.img2py — wxPython Phoenix 4.1.2a1 documentation


    下面的脚本实现将当前所在目录下的 png 文件夹的图片转换为 images.py

    from wx.tools import img2py
    import os
    
    
    class EncodeBitmapsUtil:
        def __init__(self, images_dir, target_file):
            self.images_dir = images_dir
            self.target_file = target_file
    
        def run(self):
            is_first = True
    
            for root, directories, files in os.walk(self.images_dir):
                for image in files:
                    relative_path = os.path.relpath(os.path.join(root, image), ".")
                    file_name = image.split('.')[0]
                    if is_first:
                        arg_str = "-F -n {} {} {}".format(file_name, relative_path, self.target_file)
                        is_first = False
                    else:
                        arg_str = "-a -F -n {} {} {}".format(file_name, relative_path, self.target_file)
                    img2py.main(arg_str.split())
    
    if __name__ == '__main__':
        root_path = '.'
        images_dir = os.path.join(root_path, 'png')
        target_image_file = os.path.join(root_path, 'images.py')
        EncodeBitmapsUtil(images_dir, target_image_file).run()
        
        # bitmap = getattr(images, image_id).GetBitmap()
    

    在其他脚本只需导入 images,就可以使用对应的图标,其中 icon_name 是对应 png 的图片名:

    import images
    # 下面两种方式时等价的
    bitmap = getattr(images, ”icon_name“).GetBitmap()
    bitmap = images.icon_name.GetBitmap()
    

    参考

  • 相关阅读:
    Gin+Gorm小项目
    python实现监控信息收集
    Vue引入Stylus
    Go搭建一个Web服务器
    saltstack高效运维
    04-01 Django之模板层
    03-01 Django之视图层
    02-01 Django之路由层
    HTTP协议
    01-01 Web应用
  • 原文地址:https://www.cnblogs.com/wreng/p/15785686.html
Copyright © 2011-2022 走看看