]>
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) 
  13 # o V2.5 compatability update  
  18 img2py.py  --  Convert an image to PNG format and embed it in a Python 
  19                module with appropriate code so it can be loaded into 
  20                a program at runtime.  The benefit is that since it is 
  21                Python source code it can be delivered as a .pyc or 
  22                'compiled' into the program using freeze, py2exe, etc. 
  26     img2py.py [options] image_file python_file 
  30     -m <#rrggbb>   If the original image has a mask or transparency defined 
  31                    it will be used by default.  You can use this option to 
  32                    override the default or provide a new mask by specifying 
  33                    a colour in the image to mark as transparent. 
  35     -n <name>      Normally generic names (getBitmap, etc.) are used for the 
  36                    image access functions.  If you use this option you can 
  37                    specify a name that should be used to customize the access 
  38                    fucntions, (getNameBitmap, etc.) 
  40     -c             Maintain a catalog of names that can be used to reference 
  41                    images.  Catalog can be accessed via catalog and index attributes 
  42                    of the module.  If the -n <name> option is specified then <name> 
  43                    is used for the catalog key and index value, otherwise 
  44                    the filename without any path or extension is used as the key. 
  46     -a             This flag specifies that the python_file should be appended 
  47                    to instead of overwritten.  This in combination with -n will 
  48                    allow you to put multiple images in one Python source file. 
  50     -u             Don't use compression.  Leaves the data uncompressed. 
  52     -i             Also output a function to return the image as a wxIcon. 
  58 #    - Cliff Wells <LogiplexSoftware@earthlink.net> 
  59 #      20021206: Added catalog (-c) option. 
  61 # 12/21/2003 - Jeff Grimmett (grimmtooth@softhome.net) 
  63 # o V2.5 compatability update  
  80 def crunch_data(data
, compressed
): 
  83         data 
= zlib
.compress(data
, 9) 
  85     # convert to a printable format, so it can be in a Python source file 
  88     # This next bit is borrowed from PIL.  It is used to wrap the text intelligently. 
  89     fp 
= cStringIO
.StringIO() 
  90     data 
= data 
+ " "  # buffer for the +1 test 
  93     octdigits 
= "01234567" 
  94     hexdigits 
= "0123456789abcdef" 
 100             if data
[i
+1] in octdigits
: 
 101                 for n 
in range(2, 5): 
 102                     if data
[i
+n
] not in octdigits
: 
 106             elif data
[i
+1] == 'x': 
 107                 for n 
in range(2, 5): 
 108                     if data
[i
+n
] not in hexdigits
: 
 123     # return the formatted compressed data 
 129     if not args 
or ("-h" in args
): 
 133     # some bitmap related things need to have a wxApp initialized... 
 134     if wx
.GetApp() is None: 
 135         app 
= wx
.PySimpleApp() 
 145         opts
, fileArgs 
= getopt
.getopt(args
, "auicn:m:") 
 146     except getopt
.GetoptError
: 
 150     for opt
, val 
in opts
: 
 164     if len(fileArgs
) != 2: 
 168     image_file
, python_file 
= fileArgs
 
 170     # convert the image file to a temporary file 
 171     tfname 
= tempfile
.mktemp() 
 172     ok
, msg 
= img2img
.convert(image_file
, maskClr
, None, tfname
, wx
.BITMAP_TYPE_PNG
, ".png") 
 177     data 
= open(tfname
, "rb").read() 
 178     data 
= crunch_data(data
, compressed
) 
 182         out 
= open(python_file
, "a") 
 184         out 
= open(python_file
, "w") 
 187         pyPath
, pyFile 
= os
.path
.split(python_file
) 
 188         imgPath
, imgFile 
= os
.path
.split(image_file
) 
 191             imgName 
= os
.path
.splitext(imgFile
)[0] 
 192             print "\nWarning: -n not specified. Using filename (%s) for catalog entry." % imgName
 
 196             # check to see if catalog exists already (file may have been created 
 197             # with an earlier version of img2py or without -c option) 
 198             oldSysPath 
= sys
.path
[:] 
 199             sys
.path 
= [pyPath
] # make sure we don't import something else by accident 
 200             mod 
= __import__(os
.path
.splitext(pyFile
)[0]) 
 201             if 'index' not in dir(mod
): 
 202                 print "\nWarning: %s was originally created without catalog." % python_file
 
 203                 print "         Any images already in file will not be cataloged.\n" 
 204                 out
.write("\n# ***************** Catalog starts here *******************") 
 205                 out
.write("\n\ncatalog = {}\n") 
 206                 out
.write("index = []\n\n") 
 207                 out
.write("class ImageClass: pass\n\n") 
 208             else: # save a copy of the old index so we can warn about duplicate names 
 209                 old_index
[:] = mod
.index
[:] 
 211             sys
.path 
= oldSysPath
[:] 
 213     out
.write("#" + "-" * 70 + "\n") 
 215         out
.write("# This file was generated by %s\n#\n" % sys
.argv
[0]) 
 216         out
.write("from wx import ImageFromStream, BitmapFromImage\n") 
 218             out
.write("from wx import EmptyIcon\n") 
 220             out
.write("import cStringIO, zlib\n\n\n") 
 222             out
.write("import cStringIO\n\n\n") 
 225             out
.write("catalog = {}\n") 
 226             out
.write("index = []\n\n") 
 227             out
.write("class ImageClass: pass\n\n") 
 230         out
.write("def get%sData():\n" 
 231                   "    return zlib.decompress(\n%s)\n\n" 
 234         out
.write("def get%sData():\n" 
 239     out
.write("def get%sBitmap():\n" 
 240               "    return BitmapFromImage(get%sImage())\n\n" 
 241               "def get%sImage():\n" 
 242               "    stream = cStringIO.StringIO(get%sData())\n" 
 243               "    return ImageFromStream(stream)\n\n" 
 244               % tuple([imgName
] * 4)) 
 246         out
.write("def get%sIcon():\n" 
 247                   "    icon = EmptyIcon()\n" 
 248                   "    icon.CopyFromBitmap(get%sBitmap())\n" 
 250                   % tuple([imgName
] * 2)) 
 253         if imgName 
in old_index
: 
 254             print "Warning: %s already in catalog." % imgName
 
 255             print "         Only the last entry will be accessible.\n" 
 256         old_index
.append(imgName
) 
 257         out
.write("index.append('%s')\n" % imgName
) 
 258         out
.write("catalog['%s'] = ImageClass()\n" % imgName
) 
 259         out
.write("catalog['%s'].getData = get%sData\n" % tuple([imgName
] * 2)) 
 260         out
.write("catalog['%s'].getImage = get%sImage\n" % tuple([imgName
] * 2)) 
 261         out
.write("catalog['%s'].getBitmap = get%sBitmap\n" % tuple([imgName
] * 2)) 
 263             out
.write("catalog['%s'].getIcon = get%sIcon\n" % tuple([imgName
] * 2)) 
 267         n_msg 
= ' using "%s"' % imgName
 
 271         m_msg 
= " with mask %s" % maskClr
 
 274     print "Embedded %s%s into %s%s" % (image_file
, n_msg
, python_file
, m_msg
) 
 277 if __name__ 
== "__main__":