]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/tools/img2py.py
typo fixed: should be lpthread, not -lpthread in THREAD_OPTS
[wxWidgets.git] / wxPython / tools / img2py.py
index d6e7a0f0af28c1e1d29efe6e7a77bae95ddb77f7..21957dc8bc1fc46578ee29f32c6f123f7f37bcf0 100644 (file)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 """
 #!/usr/bin/env python
 """
-img2py.py  --  Convert an image to XPM format and embed it in a Python
+img2py.py  --  Convert an image to PNG format and embed it in a Python
                module with appropriate code so it can be loaded into
                a program at runtime.  The benefit is that since it is
                Python source code it can be delivered as a .pyc or
                module with appropriate code so it can be loaded into
                a program at runtime.  The benefit is that since it is
                Python source code it can be delivered as a .pyc or
@@ -28,30 +28,25 @@ Options:
 
     -u             Don't use compression.  Leaves the data uncompressed.
 
 
     -u             Don't use compression.  Leaves the data uncompressed.
 
+    -i             Also output a function to return the image as a wxIcon.
+
 """
 
 
 
 """
 
 
 
-import sys, os, glob, getopt, tempfile
+import sys, os, glob, getopt, tempfile, string
 import cPickle, cStringIO, zlib
 import cPickle, cStringIO, zlib
-import img2xpm
+import img2img
+from wxPython import wx
 
 
 def crunch_data(data, compressed):
 
 
 def crunch_data(data, compressed):
-    # convert the lines to a Python list, pickle it and compress the result.
-    lines = []
-    for line in data[2:]:  # skip the first two lines
-        lines.append(line[1:-3])  # chop one char from the front and three from the end
-
-    # chop one extra char from the last line
-    lines[-1] = lines[-1][:-1]
-
-    # pickle, crunch and convert it to a form suitable for embedding in code
-    data = cPickle.dumps(lines)
+    # compress it?
     if compressed:
         data = zlib.compress(data, 9)
     if compressed:
         data = zlib.compress(data, 9)
-    data = repr(data)
 
 
+    # convert to a printable format, so it can be in a Python source file
+    data = repr(data)
 
     # This next bit is borrowed from PIL.  It is used to wrap the text intelligently.
     fp = cStringIO.StringIO()
 
     # This next bit is borrowed from PIL.  It is used to wrap the text intelligently.
     fp = cStringIO.StringIO()
@@ -59,6 +54,7 @@ def crunch_data(data, compressed):
     c = i = 0
     word = ""
     octdigits = "01234567"
     c = i = 0
     word = ""
     octdigits = "01234567"
+    hexdigits = "0123456789abcdef"
     while i < len(data):
         if data[i] != "\\":
             word = data[i]
     while i < len(data):
         if data[i] != "\\":
             word = data[i]
@@ -70,9 +66,16 @@ def crunch_data(data, compressed):
                         break
                 word = data[i:i+n]
                 i = i + n
                         break
                 word = data[i:i+n]
                 i = i + n
+            elif data[i+1] == 'x':
+                for n in range(2, 5):
+                    if data[i+n] not in hexdigits:
+                        break
+                word = data[i:i+n]
+                i = i + n
             else:
                 word = data[i:i+2]
                 i = i + 2
             else:
                 word = data[i:i+2]
                 i = i + 2
+
         l = len(word)
         if c + l >= 78-1:
             fp.write("\\\n")
         l = len(word)
         if c + l >= 78-1:
             fp.write("\\\n")
@@ -94,9 +97,10 @@ def main(args):
     compressed = 1
     maskClr = None
     imgName = ""
     compressed = 1
     maskClr = None
     imgName = ""
+    icon = 0
 
     try:
 
     try:
-        opts, fileArgs = getopt.getopt(args, "aun:m:")
+        opts, fileArgs = getopt.getopt(args, "auin:m:")
     except getopt.GetoptError:
         print __doc__
         return
     except getopt.GetoptError:
         print __doc__
         return
@@ -110,6 +114,8 @@ def main(args):
             imgName = val
         elif opt == "-m":
             maskClr = val
             imgName = val
         elif opt == "-m":
             maskClr = val
+        elif opt == "-i":
+            icon = 1
 
     if len(fileArgs) != 2:
         print __doc__
 
     if len(fileArgs) != 2:
         print __doc__
@@ -119,12 +125,12 @@ def main(args):
 
     # convert the image file to a temporary file
     tfname = tempfile.mktemp()
 
     # convert the image file to a temporary file
     tfname = tempfile.mktemp()
-    ok, msg = img2xpm.convert(image_file, maskClr, None, tfname)
+    ok, msg = img2img.convert(image_file, maskClr, None, tfname, wx.wxBITMAP_TYPE_PNG, ".png")
     if not ok:
         print msg
         return
 
     if not ok:
         print msg
         return
 
-    data = open(tfname, "r").readlines()
+    data = open(tfname, "rb").read()
     data = crunch_data(data, compressed)
     os.unlink(tfname)
 
     data = crunch_data(data, compressed)
     os.unlink(tfname)
 
@@ -136,27 +142,37 @@ def main(args):
     out.write("#" + "-" * 70 + "\n")
     if not append:
         out.write("# This file was generated by %s\n#\n" % sys.argv[0])
     out.write("#" + "-" * 70 + "\n")
     if not append:
         out.write("# This file was generated by %s\n#\n" % sys.argv[0])
-        out.write("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
+        out.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
+        if icon:
+            out.write("from wxPython.wx import wxEmptyIcon\n")
         if compressed:
         if compressed:
-            out.write("import cPickle, zlib\n\n\n")
+            out.write("import cStringIO, zlib\n\n\n")
         else:
         else:
-            out.write("import cPickle\n\n\n")
+            out.write("import cStringIO\n\n\n")
 
     if compressed:
         out.write("def get%sData():\n"
 
     if compressed:
         out.write("def get%sData():\n"
-                  "    return cPickle.loads(zlib.decompress(\n%s))\n\n"
+                  "    return zlib.decompress(\n%s)\n\n"
                   % (imgName, data))
     else:
         out.write("def get%sData():\n"
                   % (imgName, data))
     else:
         out.write("def get%sData():\n"
-                  "    return cPickle.loads(\n%s)\n\n"
+                  "    return %s\n\n"
                   % (imgName, data))
 
 
     out.write("def get%sBitmap():\n"
                   % (imgName, data))
 
 
     out.write("def get%sBitmap():\n"
-              "    return wxBitmapFromXPMData(get%sData())\n\n"
+              "    return wxBitmapFromImage(get%sImage())\n\n"
               "def get%sImage():\n"
               "def get%sImage():\n"
-              "    return wxImageFromBitmap(get%sBitmap())\n\n"
+              "    stream = cStringIO.StringIO(get%sData())\n"
+              "    return wxImageFromStream(stream)\n\n"
               % tuple([imgName] * 4))
               % tuple([imgName] * 4))
+    if icon:
+        out.write("def get%sIcon():\n"
+                  "    icon = wxEmptyIcon()\n"
+                  "    icon.CopyFromBitmap(get%sBitmap())\n"
+                  "    return icon\n\n"
+                  % tuple([imgName] * 2))
+
 
     if imgName:
         n_msg = ' using "%s"' % imgName
 
     if imgName:
         n_msg = ' using "%s"' % imgName
@@ -172,7 +188,3 @@ def main(args):
 if __name__ == "__main__":
     main(sys.argv[1:])
 
 if __name__ == "__main__":
     main(sys.argv[1:])
 
-
-
-
-