Changed the img2py tool to use PNG instead of XPM for embedding image
data in Python source code, and the generated code now uses streams to convert the image data to wxImage, wxBitmap, or wxIcon. A few other changes to match recent additions to CVS. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@14416 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
76
wxPython/tools/img2img.py
Normal file
76
wxPython/tools/img2img.py
Normal file
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
Common routines for the image converter utilities.
|
||||
"""
|
||||
import sys, os, glob, getopt, string
|
||||
from wxPython.wx import *
|
||||
|
||||
wxInitAllImageHandlers()
|
||||
|
||||
def convert(file, maskClr, outputDir, outputName, outType, outExt):
|
||||
if string.lower(os.path.splitext(file)[1]) == ".ico":
|
||||
icon = wxIcon(file, wxBITMAP_TYPE_ICO)
|
||||
img = wxBitmapFromIcon(icon)
|
||||
else:
|
||||
img = wxBitmap(file, wxBITMAP_TYPE_ANY)
|
||||
|
||||
if not img.Ok():
|
||||
return 0, file + " failed to load!"
|
||||
else:
|
||||
if maskClr:
|
||||
om = img.GetMask()
|
||||
mask = wxMaskColour(img, maskClr)
|
||||
img.SetMask(mask)
|
||||
if om is not None:
|
||||
om.Destroy()
|
||||
if outputName:
|
||||
newname = outputName
|
||||
else:
|
||||
newname = os.path.join(outputDir,
|
||||
os.path.basename(os.path.splitext(file)[0]) + outExt)
|
||||
if img.SaveFile(newname, outType):
|
||||
return 1, file + " converted to " + newname
|
||||
else:
|
||||
img = wxImageFromBitmap(img)
|
||||
if img.SaveFile(newname, outType):
|
||||
return 1, "ok"
|
||||
else:
|
||||
return 0, file + " failed to save!"
|
||||
|
||||
|
||||
|
||||
|
||||
def main(args, outType, outExt, doc):
|
||||
if not args or ("-h" in args):
|
||||
print doc
|
||||
return
|
||||
|
||||
outputDir = ""
|
||||
maskClr = None
|
||||
outputName = None
|
||||
|
||||
try:
|
||||
opts, fileArgs = getopt.getopt(args, "m:n:o:")
|
||||
except getopt.GetoptError:
|
||||
print __doc__
|
||||
return
|
||||
|
||||
for opt, val in opts:
|
||||
if opt == "-m":
|
||||
maskClr = val
|
||||
elif opt == "-n":
|
||||
outputName = val
|
||||
elif opt == "-o":
|
||||
outputDir = val
|
||||
|
||||
if not fileArgs:
|
||||
print doc
|
||||
return
|
||||
|
||||
for arg in fileArgs:
|
||||
for file in glob.glob(arg):
|
||||
if not os.path.isfile(file):
|
||||
continue
|
||||
ok, msg = convert(file, maskClr, outputDir, outputName,
|
||||
outType, outExt)
|
||||
print msg
|
||||
|
31
wxPython/tools/img2png.py
Normal file
31
wxPython/tools/img2png.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/usr/bin/env python
|
||||
"""
|
||||
img2png.py -- convert several image formats to PNG format
|
||||
|
||||
Usage:
|
||||
|
||||
img2png.py [options] image_files...
|
||||
|
||||
Options:
|
||||
|
||||
-o <dir> The directory to place the .png file(s), defaults to
|
||||
the current directory.
|
||||
|
||||
-m <#rrggbb> If the original image has a mask or transparency defined
|
||||
it will be used by default. You can use this option to
|
||||
override the default or provide a new mask by specifying
|
||||
a colour in the image to mark as transparent.
|
||||
|
||||
-n <name> A filename to write the .png data to. Defaults to the
|
||||
basename of the image file + '.png' This option overrides
|
||||
the -o option.
|
||||
"""
|
||||
|
||||
|
||||
import sys
|
||||
import img2img
|
||||
from wxPython import wx
|
||||
|
||||
img2img.main(sys.argv[1:], wx.wxBITMAP_TYPE_PNG, ".png", __doc__)
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
#!/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
|
||||
@@ -36,33 +36,17 @@ Options:
|
||||
|
||||
import sys, os, glob, getopt, tempfile, string
|
||||
import cPickle, cStringIO, zlib
|
||||
import img2xpm
|
||||
import img2img
|
||||
from wxPython import wx
|
||||
|
||||
|
||||
def crunch_data(data, compressed):
|
||||
# convert the lines to a Python list, pickle it and optionally compress the result.
|
||||
lines = []
|
||||
for line in data:
|
||||
if line[0] == "\"":
|
||||
# the line is typically (but not always):
|
||||
# [quote] <data> [quote][comma][newline]
|
||||
|
||||
# chop one char from the front
|
||||
line = line[1:]
|
||||
|
||||
# now find the final quote and truncate there
|
||||
quote = string.rfind(line, "\"")
|
||||
|
||||
# and append the remaining data to our list
|
||||
lines.append(line[:quote])
|
||||
|
||||
|
||||
# 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)
|
||||
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()
|
||||
@@ -141,12 +125,12 @@ def main(args):
|
||||
|
||||
# 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
|
||||
|
||||
data = open(tfname, "r").readlines()
|
||||
data = open(tfname, "rb").read()
|
||||
data = crunch_data(data, compressed)
|
||||
os.unlink(tfname)
|
||||
|
||||
@@ -158,32 +142,35 @@ 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("from wxPython.wx import wxBitmapFromXPMData, wxImageFromBitmap\n")
|
||||
out.write("from wxPython.wx import wxImageFromStream, wxBitmapFromImage\n")
|
||||
if icon:
|
||||
out.write("from wxPython.wx import wxIconFromXPMData\n")
|
||||
out.write("from wxPython.wx import wxEmptyIcon\n")
|
||||
if compressed:
|
||||
out.write("import cPickle, zlib\n\n\n")
|
||||
out.write("import cStringIO, zlib\n\n\n")
|
||||
else:
|
||||
out.write("import cPickle\n\n\n")
|
||||
out.write("import cStringIO\n\n\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"
|
||||
" return cPickle.loads(\n%s)\n\n"
|
||||
" return %s\n\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"
|
||||
" return wxImageFromBitmap(get%sBitmap())\n\n"
|
||||
" stream = cStringIO.StringIO(get%sData())\n"
|
||||
" return wxImageFromStream(stream)\n\n"
|
||||
% tuple([imgName] * 4))
|
||||
if icon:
|
||||
out.write("def get%sIcon():\n"
|
||||
" return wxIconFromXPMData(get%sData())\n\n"
|
||||
" icon = wxEmptyIcon()\n"
|
||||
" icon.CopyFromBitmap(get%sBitmap())\n"
|
||||
" return icon\n\n"
|
||||
% tuple([imgName] * 2))
|
||||
|
||||
|
||||
@@ -201,7 +188,3 @@ def main(args):
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -22,80 +22,10 @@ Options:
|
||||
"""
|
||||
|
||||
|
||||
import sys, os, glob, getopt, string
|
||||
from wxPython.wx import *
|
||||
import sys
|
||||
import img2img
|
||||
from wxPython import wx
|
||||
|
||||
wxInitAllImageHandlers()
|
||||
|
||||
|
||||
def convert(file, maskClr, outputDir, outputName):
|
||||
if string.lower(os.path.splitext(file)[1]) == ".ico":
|
||||
icon = wxIcon(file, wxBITMAP_TYPE_ICO)
|
||||
img = wxBitmapFromIcon(icon)
|
||||
else:
|
||||
img = wxBitmap(file, wxBITMAP_TYPE_ANY)
|
||||
|
||||
if not img.Ok():
|
||||
return 0, file + " failed to load!"
|
||||
else:
|
||||
if maskClr:
|
||||
om = img.GetMask()
|
||||
mask = wxMaskColour(img, maskClr)
|
||||
img.SetMask(mask)
|
||||
if om is not None:
|
||||
om.Destroy()
|
||||
if outputName:
|
||||
newname = outputName
|
||||
else:
|
||||
newname = os.path.join(outputDir, os.path.basename(os.path.splitext(file)[0]) + ".xpm")
|
||||
if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
|
||||
return 1, file + " converted to " + newname
|
||||
else:
|
||||
img = wxImageFromBitmap(img)
|
||||
if img.SaveFile(newname, wxBITMAP_TYPE_XPM):
|
||||
return 1, "ok"
|
||||
else:
|
||||
return 0, file + " failed to save!"
|
||||
|
||||
|
||||
|
||||
def main(args):
|
||||
if not args or ("-h" in args):
|
||||
print __doc__
|
||||
return
|
||||
|
||||
outputDir = ""
|
||||
maskClr = None
|
||||
outputName = None
|
||||
|
||||
try:
|
||||
opts, fileArgs = getopt.getopt(args, "m:n:o:")
|
||||
except getopt.GetoptError:
|
||||
print __doc__
|
||||
return
|
||||
|
||||
for opt, val in opts:
|
||||
if opt == "-m":
|
||||
maskClr = val
|
||||
elif opt == "-n":
|
||||
outputName = val
|
||||
elif opt == "-o":
|
||||
outputDir = val
|
||||
|
||||
if not fileArgs:
|
||||
print __doc__
|
||||
return
|
||||
|
||||
for arg in fileArgs:
|
||||
for file in glob.glob(arg):
|
||||
if not os.path.isfile(file):
|
||||
continue
|
||||
ok, msg = convert(file, maskClr, outputDir, outputName)
|
||||
print msg
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
img2img.main(sys.argv[1:], wx.wxBITMAP_TYPE_XPM, ".xpm", __doc__)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user