home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pypil112.zip / Scripts / image2py.py < prev    next >
Text File  |  2001-05-03  |  3KB  |  126 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # convert an image to a Python module
  6. #
  7. # to use the module, import it and access the "IMAGE" variable
  8. #
  9. #       import img1
  10. #       im = img1.IMAGE
  11. #
  12. # the variable name can be changed with the -n option
  13. #
  14. # note that the application using this module must include JPEG
  15. # and/or ZIP decoders, unless the -u option is used.
  16. #
  17. # Copyright (c) Secret Labs AB 1997.  All rights reserved.
  18. # Copyright (c) Fredrik Lundh 1997.
  19. #
  20. # See the README file for information on usage and redistribution.
  21. #
  22.  
  23. import Image
  24. import getopt, string, StringIO, sys
  25.  
  26. octdigits = "01234567"
  27.  
  28. def usage():
  29.     print "image2py 0.1/97-01-03 -- convert image to python module"
  30.     print
  31.     print "Usage: image2py [options] imagefile pyfile"
  32.     print
  33.     print "Options:"
  34.     print "  -n <name>    set variable name (default is 'IMAGE')"
  35.     print "  -l           use lossy compression (JPEG) if suitable"
  36.     print "  -u           disable compression"
  37.     print
  38.     print "Provided you use distinct variable names, the output"
  39.     print "files can be concatenated into one large module file."
  40.     sys.exit(1)
  41.  
  42. try:
  43.     opt, argv = getopt.getopt(sys.argv[1:], "n:lu")
  44. except getopt.error, v:
  45.     usage()
  46.  
  47. name = "IMAGE"
  48. lossy = 0
  49. compress = 1
  50.  
  51. for o, a in opt:
  52.     if o == "-n":
  53.         name = a
  54.     elif o == "-l":
  55.         lossy = 1
  56.     elif o == "-u":
  57.         compress = 0
  58.  
  59. if len(argv) != 2:
  60.     usage()
  61.  
  62. # --------------------------------------------------------------------
  63. # convert image to string
  64.  
  65. im = Image.open(argv[0])
  66.  
  67. if im.format == "JPEG" and compress:
  68.  
  69.     # store as is
  70.     data = open(argv[0], "rb").read()
  71.  
  72. else:
  73.  
  74.     # load and store as PNG
  75.     fp = StringIO.StringIO()
  76.  
  77.     if compress:
  78.         if lossy and im.mode in ["L", "RGB"]:
  79.             im.save(fp, "JPEG")
  80.         else:
  81.             im.convert("RGB").save(fp, "PNG")
  82.     else:
  83.         im.save(fp, "PPM") # FIXME: won't work with "P" images
  84.  
  85.     data = fp.getvalue()
  86.  
  87. # --------------------------------------------------------------------
  88. # convert string to python module (this is not very fast...)
  89.  
  90. fp = open(argv[1], "w")
  91.  
  92. data = repr(data)
  93.  
  94. fp.write("# generated by image2py %s\n" % argv[0])
  95. fp.write("import Image, StringIO\n")
  96.  
  97. word = "%s = Image.open(StringIO.StringIO('" % name
  98.  
  99. fp.write(word)
  100. c = len(word)
  101.  
  102. i = 1
  103. while i < len(data)-1:
  104.     if data[i] != "\\":
  105.         word = data[i]
  106.         i = i + 1
  107.     else:
  108.         if data[i+1] in octdigits:
  109.             for n in range(2, 5):
  110.                 if data[i+n] not in octdigits:
  111.                     break
  112.             word = data[i:i+n]
  113.             i = i + n
  114.         else:
  115.             word = data[i:i+2]
  116.             i = i + 2
  117.     l = len(word)
  118.     if c + l >= 78-1:
  119.         # fp.write("'\n'")
  120.         fp.write("\\\n")
  121.         c = 0
  122.     fp.write(word)
  123.     c = c + l
  124.  
  125. fp.write("'))\n")
  126.