1 # This module provides a registry that you can use to register your own extension types so 2 # you can serialization and de-serialization them in Python object structure. 3 # Copyright (c) 2007 Boringlamb 4 5 import System.IO 6 import System.Text 7 import System.Runtime.Serialization.Formatters.Binary 8 import copy_reg 9 10 _formatter = System.Runtime.Serialization.Formatters.Binary.BinaryFormatter() 11 _encoding = System.Text.Encoding.GetEncoding( ' iso-8859-1 ' ) 12 13 # 14 # Registering a object handler. Such a handler consists of two parts: 15 # a pickler which takes object and returns a tuple that can only contain a string, and 16 # an unpickler which takes the contents of such a tuple as its arguments 17 #18 def _unpickler(string): 19 bytes = _encoding.GetBytes(string) 20 stream = System.IO.MemoryStream(bytes) 21 return _formatter.Deserialize(stream) 22 23 def _pickler(obj): 24 stream = System.IO.MemoryStream() 25 _formatter.Serialize(stream, obj)26 bytes = stream.ToArray() 27 string = _encoding.GetString(bytes).encode( ' iso-8859-1 ' ) 28 return _unpickler, (string,) 29 30 # 31 # Register your own extension types 32 #33 def register(type): 34 copy_reg.pickle(type, _pickler, _unpickler)35
大概讲一下这个模块的作用,借助它可以将图片,图标等等类似的资源文件转换成Python 结构,存储在.py文件中
也就相当于将图片写入.Py文件中,如果用Python 写Winform的时候要用图片的话调用的时候相当方便,使用:
self.BackgroundImage = cPickle.loads(资源文件名.资源名称)
by the way,发一个资源存储于py文件的样子,他只占一行:
Icon1 = """ cresPickle\n_unpickler\np1\n(S'\x00\x01\x00\x00\x00\xff\xff\xff\xff\x01\x00\x00\x00\x00\x00\x00\x00\x0c\x02\x00\x00\x00QSystem.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\x05\x01\x00\x00\x00\x15System.Drawing.Bitmap\x01\x00\x00\x00\x04Data\x07\x02\x02\x00\x00\x00\t\x03\x00\x00\x00\x0f\x03\x00\x00\x006\x03\x00\x00\x02BM6\x03\x00\x00\x00\x00\x00\x006\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00\x10\x00\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x0b\x00\x00\x12\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\xff\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\xff\x00\xff\xff\x00\xff\x07\x8d\xbe%\xa1\xd1r\xc7\xe7\x85\xd7\xfaf\xcd\xf9e\xcd\xf9e\xcd\xf9e\xcd\xf9e\xcd\xf8e\xcd\xf9e\xcd\xf8f\xce\xf99\xad\xd8\x07\x8d\xbe\xff\x00\xff\xff\x00\xff\x07\x8d\xbeL\xbc\xe79\xa8\xd1\xa0\xe2\xfbo\xd4\xfao\xd4\xf9n\xd4\xfao\xd4\xf9o\xd4\xfao\xd4\xfao\xd4\xfan\xd4\xf9>\xb1\xd9\x84\xd7\xeb\x07\x8d\xbe\xff\x00\xff\x07\x8d\xber\xd6\xfa\x07\x8d\xbe\xae\xea\xfcy\xdc\xfby\xdc\xfby\xdc\xfby\xdc\xfby\xdc\xfbz\xdc\xfby\xdc\xfay\xdc\xfaD\xb5\xd9\xae\xf1\xf9\x07\x8d\xbe\xff\x00\xff\x07\x8d\xbey\xdd\xfb\x18\x99\xc7\x9a\xdf\xf3\x92\xe7\xfb\x84\xe4\xfb\x83\xe4\xfc\x83\xe4\xfc\x84\xe4\xfc\x83\xe4\xfc\x83\xe4\xfb\x84\xe5\xfcH\xb9\xda\xb3\xf4\xf9\x07\x8d\xbe\xff\x00\xff\x07\x8d\xbe\x82\xe3\xfcC\xb7\xdce\xc3\xe0\xac\xf0\xfd\x8d\xeb\xfc\x8d\xeb\xfc\x8d\xeb\xfd\x8d\xeb\xfd\x8d\xeb\xfc\x8d\xeb\xfd\x0c\x85\x18L\xbb\xda\xb6\xf7\xf9m\xca\xe0\x07\x8d\xbe\x07\x8d\xbe\x8a\xea\xfcw\xdc\xf3"\x9c\xc6\xfd\xff\xff\xc8\xf7\xfe\xc9\xf7\xfe\xc9\xf7\xfe\xc9\xf7\xfe\xc8\xf7\xfe\x0c\x85\x18<\xbc]\x0c\x85\x18\xde\xf9\xfb\xd6\xf6\xf9\x07\x8d\xbe\x07\x8d\xbe\x93\xf0\xfe\x93\xf0\xfd\x16\x97\xc5\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x0c\x85\x18R\xd9\x7fb\xed\x97A\xc4e\x0c\x85\x18\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x9b\xf5\xfe\x9a\xf6\xfe\x9a\xf6\xfe\x9b\xf5\xfd\x9b\xf6\xfe\x9a\xf6\xfe\x9b\xf5\xfe\x0c\x85\x18F\xcelY\xe4\x88X\xe1\x88a\xeb\x94@\xc1e\x0c\x85\x18\xff\x00\xff\x07\x8d\xbe\xfe\xfe\xfe\xa0\xfb\xff\xa0\xfb\xfe\xa0\xfb\xfe\xa1\xfa\xfe\xa1\xfb\xfe\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18V\xe1\x84G\xcdn\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\xff\x00\xff\x07\x8d\xbe\xfe\xfe\xfe\xa5\xfe\xff\xa5\xfe\xff\xa5\xfe\xff\x07\x8c\xb6C\xb7\xdcC\xb7\xdcC\xb7\xdc\x0c\x85\x18N\xddy6\xbaT\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\x07\x8d\xbe\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0c\x85\x18@\xd0e\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0c\x85\x18*\xb7C-\xbaI\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0c\x85\x18!\xb58\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\x0c\x85\x18\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\x0b'\ntRp2\n. """
这样你写一个程序不用把图片、图标带着到处走了,路径又不好找,只需要带着.py文件就是了:)
相关文章:
2022-12-23
2022-12-23
2021-07-15
2021-06-30
2021-06-05
2021-12-23
2022-01-26
2021-07-28
猜你喜欢
2022-03-06
2021-09-20
2022-12-23
2021-04-12
2021-10-27
2022-12-23
2021-05-25
相关资源
下载
2022-12-04
下载
2023-01-21
下载
2022-12-18
下载
2023-01-27