]>
git.saurik.com Git - wxWidgets.git/blob - misc/scripts/png2c.py
3 # This script is a slightly modified version of the original found at
5 # http://wiki.wxwidgets.org/Embedding_PNG_Images-Bin2c_In_Python
7 # without any copyright attribution so it is assumed it can be used under
8 # wxWindows licence as the rest of the wiki material.
16 USAGE
= """png2c - Embed a PNG in a C header file (like XPM)
17 Usage: png2c [file ..] Output input PNG files as C structures on stdout"""
23 r
= re
.compile("^([a-zA-Z._][a-zA-Z._0-9]*)[.][pP][nN][gG]$")
25 for path
in sys
.argv
[1:]:
26 filename
= os
.path
.basename(path
)
28 # Allow only filenames that make sense
31 print "Skipped file (unsuitable filename): " + filename
34 # Read PNG file as character array
35 bytes = array
.array('B', open(path
, "rb").read())
39 text
= "/* %s - %d bytes */\n" \
40 "static const unsigned char %s_png[] = {\n" % (filename
, count
, m
.group(1))
42 # Iterate the characters, we want
44 # 0x01, 0x02, .... (8 values per line maximum)
48 # Every new line starts with two whitespaces
51 # Then the hex data (up to 8 values per line)
52 text
+= "0x%02x" % (byte
)
53 # Separate all but the last values
60 # Now conclude the C source