convert-icon 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #!/usr/bin/python
  2. #
  3. # $Id: convert-icon 704 2006-10-18 00:51:11Z fraggle $
  4. #
  5. # Copyright(C) 2005 Simon Howard
  6. #
  7. # This program is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License
  9. # as published by the Free Software Foundation; either version 2
  10. # of the License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. # 02111-1307, USA.
  21. #
  22. # Converts images into C structures to be inserted in programs
  23. #
  24. import sys
  25. import os
  26. import re
  27. try:
  28. import Image
  29. except ImportError:
  30. print "WARNING: Could not update %s. Please install the Python Imaging library." % sys.argv[2]
  31. sys.exit(0)
  32. def convert_image(filename, output_filename):
  33. im = Image.open(filename).convert("RGB")
  34. outfile = open(output_filename, "w")
  35. size = im.size
  36. struct_name = os.path.basename(output_filename)
  37. struct_name = re.sub(re.compile("\\..*$"), "", struct_name)
  38. struct_name = re.sub(re.compile("\W"), "_", struct_name)
  39. outfile.write("static int %s_w = %i;\n" % (struct_name, size[0]))
  40. outfile.write("static int %s_h = %i;\n" % (struct_name, size[1]))
  41. outfile.write("\n")
  42. outfile.write("static unsigned char %s_data[] = {\n" % (struct_name))
  43. elements_on_line = 0
  44. outfile.write(" ")
  45. for y in range(size[1]):
  46. for x in range(size[0]):
  47. val = im.getpixel((x, y))
  48. outfile.write("0x%02x,0x%02x,0x%02x, " % val)
  49. elements_on_line += 1
  50. if elements_on_line >= 4:
  51. elements_on_line = 0
  52. outfile.write("\n")
  53. outfile.write(" ")
  54. outfile.write("\n")
  55. outfile.write("};\n")
  56. convert_image(sys.argv[1], sys.argv[2])