]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/wx/tools/img2py.py
   1 #---------------------------------------------------------------------- 
   2 # Name:        wxPython.tools.img2py 
   3 # Purpose:     Convert an image to Python code. 
   8 # Copyright:   (c) 2002 by Total Control Software 
   9 # Licence:     wxWindows license 
  10 #---------------------------------------------------------------------- 
  11 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  14 # 2/25/2007 - Gianluca Costa (archimede86@katamail.com) 
  17 # o V2.5 compatibility update  
  21 img2py.py  --  Convert an image to PNG format and embed it in a Python 
  22                module with appropriate code so it can be loaded into 
  23                a program at runtime.  The benefit is that since it is 
  24                Python source code it can be delivered as a .pyc or 
  25                'compiled' into the program using freeze, py2exe, etc. 
  29     img2py.py [options] image_file python_file 
  33     -m <#rrggbb>   If the original image has a mask or transparency defined 
  34                    it will be used by default.  You can use this option to 
  35                    override the default or provide a new mask by specifying 
  36                    a colour in the image to mark as transparent. 
  38     -n <name>      Normally generic names (getBitmap, etc.) are used for the 
  39                    image access functions.  If you use this option you can 
  40                    specify a name that should be used to customize the access 
  41                    fucntions, (getNameBitmap, etc.) 
  43     -c             Maintain a catalog of names that can be used to reference 
  44                    images.  Catalog can be accessed via catalog and  
  45                    index attributes of the module.   
  46                    If the -n <name> option is specified then <name> 
  47                    is used for the catalog key and index value, otherwise 
  48                    the filename without any path or extension is used  
  51     -a             This flag specifies that the python_file should be appended 
  52                    to instead of overwritten.  This in combination with -n will 
  53                    allow you to put multiple images in one Python source file. 
  55     -u             Don't use compression.  Leaves the data uncompressed. 
  57     -i             Also output a function to return the image as a wxIcon. 
  60 You can also import this module from your Python scripts, and use its img2py() 
  61 function. See its docstring for more info. 
  66 #    - Cliff Wells <LogiplexSoftware@earthlink.net> 
  67 #      20021206: Added catalog (-c) option. 
  69 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  72 # 2/25/2007 - Gianluca Costa (archimede86@katamail.com) 
  73 #        -Refactorization of the script-creation code in a specific "img2py()" function 
  74 #        -Added regex parsing instead of module importing 
  75 #        -Added some "try/finally" statements 
  76 #        -Added default values as named constants 
  77 #        -Made some parts of code a bit easier to read 
  78 #        -Updated the module docstring 
  79 #        -Corrected a bug with EmptyIcon 
  81 # o V2.5 compatibility update  
  98 def crunch_data(data
, compressed
): 
 101         data 
= zlib
.compress(data
, 9) 
 103     # convert to a printable format, so it can be in a Python source file 
 106     # This next bit is borrowed from PIL.  It is used to wrap the text intelligently. 
 107     fp 
= cStringIO
.StringIO() 
 108     data 
+= " "  # buffer for the +1 test 
 111     octdigits 
= "01234567" 
 112     hexdigits 
= "0123456789abcdef" 
 118             if data
[i
+1] in octdigits
: 
 119                 for n 
in xrange(2, 5): 
 120                     if data
[i
+n
] not in octdigits
: 
 124             elif data
[i
+1] == 'x': 
 125                 for n 
in xrange(2, 5): 
 126                     if data
[i
+n
] not in hexdigits
: 
 141     # return the formatted compressed data 
 146 DEFAULT_APPEND 
= False 
 147 DEFAULT_COMPRESSED 
= True 
 148 DEFAULT_MASKCLR 
= None 
 151 DEFAULT_CATALOG 
= False 
 153 #THIS IS USED TO IDENTIFY, IN THE GENERATED SCRIPT, LINES IN THE FORM "index.append('Image name')" 
 154 indexPattern 
= re
.compile(r
"\s*index.append\('(.+)'\)\s*") 
 156 def img2py(image_file
, python_file
, append
=DEFAULT_APPEND
, compressed
=DEFAULT_COMPRESSED
, maskClr
=DEFAULT_MASKCLR
, imgName
=DEFAULT_IMGNAME
, icon
=DEFAULT_ICON
, catalog
=DEFAULT_CATALOG
): 
 158     Converts an image file to a data structure written in a Python file 
 159     --image_file: string; the path of the source image file 
 160     --python_file: string; the path of the destination python file 
 161     --other arguments: they are equivalent to the command-line arguments 
 165         app 
= wx
.PySimpleApp() 
 167     # convert the image file to a temporary file 
 168     tfname 
= tempfile
.mktemp() 
 170         ok
, msg 
= img2img
.convert(image_file
, maskClr
, None, tfname
, wx
.BITMAP_TYPE_PNG
, ".png") 
 175         data 
= open(tfname
, "rb").read() 
 176         data 
= crunch_data(data
, compressed
) 
 178         if os
.path
.exists(tfname
): 
 183     if catalog 
and append
: 
 184         # check to see if catalog exists already (file may have been created 
 185         # with an earlier version of img2py or without -c option) 
 186         pyPath
, pyFile 
= os
.path
.split(python_file
) 
 188         append_catalog 
= True 
 190         sourcePy 
= open(python_file
, "r") 
 192             for line 
in sourcePy
: 
 194                 if line 
== "catalog = {}\n": 
 195                     append_catalog 
= False 
 197                     lineMatcher 
= indexPattern
.match(line
) 
 199                         old_index
.append(lineMatcher
.groups()[0]) 
 205             out 
= open(python_file
, "a") 
 207                 out
.write("\n# ***************** Catalog starts here *******************") 
 208                 out
.write("\n\ncatalog = {}\n") 
 209                 out
.write("index = []\n\n") 
 210                 out
.write("class ImageClass: pass\n\n") 
 217         out 
= open(python_file
, "a") 
 219         out 
= open(python_file
, "w") 
 223             imgPath
, imgFile 
= os
.path
.split(image_file
) 
 226                 imgName 
= os
.path
.splitext(imgFile
)[0] 
 227                 print "\nWarning: -n not specified. Using filename (%s) for catalog entry." % imgName
 
 229         out
.write("#" + "-" * 70 + "\n") 
 231             out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0]) 
 232             out
.write("from wx import ImageFromStream, BitmapFromImage, EmptyIcon\n")         
 234                 out
.write("import cStringIO, zlib\n\n\n") 
 236                 out
.write("import cStringIO\n\n\n") 
 239                 out
.write("catalog = {}\n") 
 240                 out
.write("index = []\n\n") 
 241                 out
.write("class ImageClass: pass\n\n") 
 244             out
.write("def get%sData():\n" 
 245                       "    return zlib.decompress(\n%s)\n\n" 
 248             out
.write("def get%sData():\n" 
 253         out
.write("def get%sBitmap():\n" 
 254                   "    return BitmapFromImage(get%sImage())\n\n" 
 255                   "def get%sImage():\n" 
 256                   "    stream = cStringIO.StringIO(get%sData())\n" 
 257                   "    return ImageFromStream(stream)\n\n" 
 258                   % tuple([imgName
] * 4)) 
 260             out
.write("def get%sIcon():\n" 
 261                       "    icon = EmptyIcon()\n" 
 262                       "    icon.CopyFromBitmap(get%sBitmap())\n" 
 264                       % tuple([imgName
] * 2)) 
 267             if imgName 
in old_index
: 
 268                 print "Warning: %s already in catalog." % imgName
 
 269                 print "         Only the last entry will be accessible.\n" 
 270             old_index
.append(imgName
) 
 271             out
.write("index.append('%s')\n" % imgName
) 
 272             out
.write("catalog['%s'] = ImageClass()\n" % imgName
) 
 273             out
.write("catalog['%s'].getData = get%sData\n" % tuple([imgName
] * 2)) 
 274             out
.write("catalog['%s'].getImage = get%sImage\n" % tuple([imgName
] * 2)) 
 275             out
.write("catalog['%s'].getBitmap = get%sBitmap\n" % tuple([imgName
] * 2)) 
 277                 out
.write("catalog['%s'].getIcon = get%sIcon\n" % tuple([imgName
] * 2)) 
 282             n_msg 
= ' using "%s"' % imgName
 
 287             m_msg 
= " with mask %s" % maskClr 
 
 291         print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
) 
 298     if not args 
or ("-h" in args
): 
 302     append 
= DEFAULT_APPEND
 
 303     compressed 
= DEFAULT_COMPRESSED
 
 304     maskClr 
= DEFAULT_MASKCLR
 
 305     imgName 
= DEFAULT_IMGNAME
 
 307     catalog 
= DEFAULT_CATALOG
 
 310         opts
, fileArgs 
= getopt
.getopt(args
, "auicn:m:") 
 311     except getopt
.GetoptError
: 
 315     for opt
, val 
in opts
: 
 329     if len(fileArgs
) != 2: 
 333     image_file
, python_file 
= fileArgs
 
 334     img2py(image_file
, python_file
, append
, compressed
, maskClr
, imgName
, icon
, catalog
) 
 338 if __name__ 
== "__main__":