在python文件中集成图片

方法一:

http://leo108.com/pid-938.asp
使用base64方式编解码。

核心代码如下:

1.将图片文件编码为base64字符串:

import base64                    #导入base64库 
f = open(r'/home/1.ico','rb')    #用二进制方式打开图片文件
str = base64.b64encode(f.read()) #读取文件内容,编码为base64字符串
f.close()                        #关闭文件
print str                        #输出base64编码结果

 

2.将base64字符串解码为图片:

import base64  
import wx  
import cStringIO  

def GetMondrianData():  
    iconData = "图片BASE64字符串" 
    iconData = base64.b64decode(iconData)  
    return iconData  
def GetMondrianBitmap():  
    return wx.BitmapFromImage(GetMondrianImage())  
def GetMondrianImage():  
    stream = cStringIO.StringIO(GetMondrianData())  
    return wx.ImageFromStream(stream)  
def GetMondrianIcon():  
    icon = wx.EmptyIcon()  
    icon.CopyFromBitmap(GetMondrianBitmap())  
    return icon


调用GetMondrianIcon()函数即可

 

方法二:

使用函数im2py.py,下面这个是旧版wxpython的使用

http://www.blog.pythonlibrary.org/2008/05/23/wxpython-embedding-an-image-in-your-title-bar/

wxpython_2.9.2_py27中的使用:

打开cmd,打开文件夹C:\Python27\Lib\site-packages\wx-2.9.2-msw\wx\tools,输入命令

python img2py.py -i (-n ***)28.ico myIcon.py

option中-n, -i的注释:

-n <name>      Normally generic names (getBitmap, etc.) are used for the
                        image access functions.  If you use this option you can
                        specify a name that should be used to customize the access
                        fucntions, (getNameBitmap, etc.),否则默认为下划线+ico的名字

                        本例中为_28

-i                     Also output a function to return the image as a wxIcon

输出文件为myIcon,

本例中的使用方法为:

import myIcon
ico = myIcon._28.getIcon()
self.SetIcon(ico)

当然也可以在myIcon.py的文件末尾加

get_Icon = _28.getIcon

则使用方法为

import myIcon
ico = myIcon.get_Icon()
self.SetIcon(ico)



 

 


                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值