From 19d93932118b5e4a2325bd9d88efc5cf1b3c1ac7 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Tue, 10 Dec 2002 22:06:33 +0000 Subject: [PATCH] img2py can now output a "catalog" of the images in the .py file, also added header comments to the files. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_4_BRANCH@18187 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/wxPython/tools/img2img.py | 15 ++++-- wxPython/wxPython/tools/img2png.py | 11 +++++ wxPython/wxPython/tools/img2py.py | 73 +++++++++++++++++++++++++++++- wxPython/wxPython/tools/img2xpm.py | 11 +++++ 4 files changed, 106 insertions(+), 4 deletions(-) diff --git a/wxPython/wxPython/tools/img2img.py b/wxPython/wxPython/tools/img2img.py index 3ce1e9f7ff..802ac73ad1 100644 --- a/wxPython/wxPython/tools/img2img.py +++ b/wxPython/wxPython/tools/img2img.py @@ -1,6 +1,15 @@ -""" -Common routines for the image converter utilities. -""" +#---------------------------------------------------------------------- +# Name: wxPython.tools.img2img +# Purpose: Common routines for the image converter utilities. +# +# Author: Robin Dunn +# +# RCS-ID: $Id$ +# Copyright: (c) 2002 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + + import sys, os, glob, getopt, string from wxPython.wx import * diff --git a/wxPython/wxPython/tools/img2png.py b/wxPython/wxPython/tools/img2png.py index 02d048a5e9..2ddfee329e 100644 --- a/wxPython/wxPython/tools/img2png.py +++ b/wxPython/wxPython/tools/img2png.py @@ -1,3 +1,14 @@ +#---------------------------------------------------------------------- +# Name: wxPython.tools.img2png +# Purpose: Convert an image to PNG format +# +# Author: Robin Dunn +# +# RCS-ID: $Id$ +# Copyright: (c) 2002 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + """ img2png.py -- convert several image formats to PNG format diff --git a/wxPython/wxPython/tools/img2py.py b/wxPython/wxPython/tools/img2py.py index 127c80130e..e401be78b4 100644 --- a/wxPython/wxPython/tools/img2py.py +++ b/wxPython/wxPython/tools/img2py.py @@ -1,3 +1,15 @@ +#---------------------------------------------------------------------- +# Name: wxPython.tools.img2py +# Purpose: Convert an image to Python code. +# +# Author: Robin Dunn +# +# RCS-ID: $Id$ +# Copyright: (c) 2002 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + + """ img2py.py -- Convert an image to PNG format and embed it in a Python module with appropriate code so it can be loaded into @@ -21,6 +33,12 @@ Options: specify a name that should be used to customize the access fucntions, (getNameBitmap, etc.) + -c Maintain a catalog of names that can be used to reference + images. Catalog can be accessed via catalog and index attributes + of the module. If the -n option is specified then + is used for the catalog key and index value, otherwise + the filename without any path or extension is used as the key. + -a This flag specifies that the python_file should be appended to instead of overwritten. This in combination with -n will allow you to put multiple images in one Python source file. @@ -31,6 +49,11 @@ Options: """ +# +# Changes: +# - Cliff Wells +# 20021206: Added catalog (-c) option. +# import sys, os, glob, getopt, tempfile, string @@ -97,9 +120,10 @@ def main(args): maskClr = None imgName = "" icon = 0 + catalog = 0 try: - opts, fileArgs = getopt.getopt(args, "auin:m:") + opts, fileArgs = getopt.getopt(args, "auicn:m:") except getopt.GetoptError: print __doc__ return @@ -115,6 +139,8 @@ def main(args): maskClr = val elif opt == "-i": icon = 1 + elif opt == "-c": + catalog = 1 if len(fileArgs) != 2: print __doc__ @@ -138,6 +164,33 @@ def main(args): else: out = open(python_file, "w") + if catalog: + pyPath, pyFile = os.path.split(python_file) + imgPath, imgFile = os.path.split(image_file) + + if not imgName: + imgName = os.path.splitext(imgFile)[0] + print "\nWarning: -n not specified. Using filename (%s) for catalog entry." % imgName + + old_index = [] + if append: + # check to see if catalog exists already (file may have been created + # with an earlier version of img2py or without -c option) + oldSysPath = sys.path[:] + sys.path = [pyPath] # make sure we don't import something else by accident + mod = __import__(os.path.splitext(pyFile)[0]) + if 'index' not in dir(mod): + print "\nWarning: %s was originally created without catalog." % python_file + print " Any images already in file will not be cataloged.\n" + out.write("\n# ***************** Catalog starts here *******************") + out.write("\n\ncatalog = {}\n") + out.write("index = []\n\n") + out.write("class ImageClass: pass\n\n") + else: # save a copy of the old index so we can warn about duplicate names + old_index[:] = mod.index[:] + del mod + sys.path = oldSysPath[:] + out.write("#" + "-" * 70 + "\n") if not append: out.write("# This file was generated by %s\n#\n" % sys.argv[0]) @@ -149,6 +202,11 @@ def main(args): else: out.write("import cStringIO\n\n\n") + if catalog: + out.write("catalog = {}\n") + out.write("index = []\n\n") + out.write("class ImageClass: pass\n\n") + if compressed: out.write("def get%sData():\n" " return zlib.decompress(\n%s)\n\n" @@ -172,6 +230,19 @@ def main(args): " return icon\n\n" % tuple([imgName] * 2)) + if catalog: + if imgName in old_index: + print "Warning: %s already in catalog." % imgName + print " Only the last entry will be accessible.\n" + old_index.append(imgName) + out.write("index.append('%s')\n" % imgName) + out.write("catalog['%s'] = ImageClass()\n" % imgName) + out.write("catalog['%s'].getData = get%sData\n" % tuple([imgName] * 2)) + out.write("catalog['%s'].getImage = get%sImage\n" % tuple([imgName] * 2)) + out.write("catalog['%s'].getBitmap = get%sBitmap\n" % tuple([imgName] * 2)) + if icon: + out.write("catalog['%s'].getIcon = get%sIcon\n" % tuple([imgName] * 2)) + out.write("\n\n") if imgName: n_msg = ' using "%s"' % imgName diff --git a/wxPython/wxPython/tools/img2xpm.py b/wxPython/wxPython/tools/img2xpm.py index c4a495b2c0..e49bff180a 100644 --- a/wxPython/wxPython/tools/img2xpm.py +++ b/wxPython/wxPython/tools/img2xpm.py @@ -1,3 +1,14 @@ +#---------------------------------------------------------------------- +# Name: wxPython.tools.img2xpm +# Purpose: Convert an image to XPM format +# +# Author: Robin Dunn +# +# RCS-ID: $Id$ +# Copyright: (c) 2002 by Total Control Software +# Licence: wxWindows license +#---------------------------------------------------------------------- + """ img2xpm.py -- convert several image formats to XPM