texttopng 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/usr/bin/env python
  2. # $URL: http://pypng.googlecode.com/svn/trunk/code/texttopng $
  3. # $Rev: 132 $
  4. # Script to renders text as a PNG image.
  5. from array import array
  6. import itertools
  7. font = {
  8. 32: '0000000000000000',
  9. 33: '0010101010001000',
  10. 34: '0028280000000000',
  11. 35: '0000287c287c2800',
  12. 36: '00103c5038147810',
  13. 37: '0000644810244c00',
  14. 38: '0020502054483400',
  15. 39: '0010100000000000',
  16. 40: '0008101010101008',
  17. 41: '0020101010101020',
  18. 42: '0010543838541000',
  19. 43: '000010107c101000',
  20. 44: '0000000000301020',
  21. 45: '000000007c000000',
  22. 46: '0000000000303000',
  23. 47: '0000040810204000',
  24. 48: '0038445454443800',
  25. 49: '0008180808080800',
  26. 50: '0038043840407c00',
  27. 51: '003c041804043800',
  28. 52: '00081828487c0800',
  29. 53: '0078407804047800',
  30. 54: '0038407844443800',
  31. 55: '007c040810101000',
  32. 56: '0038443844443800',
  33. 57: '0038443c04040400',
  34. 58: '0000303000303000',
  35. 59: '0000303000301020',
  36. 60: '0004081020100804',
  37. 61: '0000007c007c0000',
  38. 62: '0040201008102040',
  39. 63: '0038440810001000',
  40. 64: '00384c545c403800',
  41. 65: '0038447c44444400',
  42. 66: '0078447844447800',
  43. 67: '0038444040443800',
  44. 68: '0070484444487000',
  45. 69: '007c407840407c00',
  46. 70: '007c407840404000',
  47. 71: '003844405c443c00',
  48. 72: '0044447c44444400',
  49. 73: '0038101010103800',
  50. 74: '003c040404443800',
  51. 75: '0044487048444400',
  52. 76: '0040404040407c00',
  53. 77: '006c545444444400',
  54. 78: '004464544c444400',
  55. 79: '0038444444443800',
  56. 80: '0078447840404000',
  57. 81: '0038444444443c02',
  58. 82: '0078447844444400',
  59. 83: '0038403804047800',
  60. 84: '007c101010101000',
  61. 85: '0044444444443c00',
  62. 86: '0044444444281000',
  63. 87: '0044445454543800',
  64. 88: '0042241818244200',
  65. 89: '0044443810101000',
  66. 90: '007c081020407c00',
  67. 91: '0038202020202038',
  68. 92: '0000402010080400',
  69. 93: '0038080808080838',
  70. 94: '0010284400000000',
  71. 95: '000000000000fe00',
  72. 96: '0040200000000000',
  73. 97: '000038043c443c00',
  74. 98: '0040784444447800',
  75. 99: '0000384040403800',
  76. 100: '00043c4444443c00',
  77. 101: '000038447c403c00',
  78. 102: '0018203820202000',
  79. 103: '00003c44443c0438',
  80. 104: '0040784444444400',
  81. 105: '0010003010101000',
  82. 106: '0010003010101020',
  83. 107: '0040404870484400',
  84. 108: '0030101010101000',
  85. 109: '0000385454444400',
  86. 110: '0000784444444400',
  87. 111: '0000384444443800',
  88. 112: '0000784444784040',
  89. 113: '00003c44443c0406',
  90. 114: '00001c2020202000',
  91. 115: '00003c4038047800',
  92. 116: '0020203820201800',
  93. 117: '0000444444443c00',
  94. 118: '0000444444281000',
  95. 119: '0000444454543800',
  96. 120: '0000442810284400',
  97. 121: '00004444443c0438',
  98. 122: '00007c0810207c00',
  99. 123: '0018202060202018',
  100. 124: '0010101000101010',
  101. 125: '003008080c080830',
  102. 126: '0020540800000000',
  103. }
  104. def char(i):
  105. """Get image data for the character `i` (a one character string).
  106. Returned as a list of rows. Each row is a tuple containing the
  107. packed pixels.
  108. """
  109. i = ord(i)
  110. if i not in font:
  111. return [(0,)]*8
  112. return map(lambda row: (ord(row),), font[i].decode('hex'))
  113. def texttoraster(m):
  114. """Convert string *m* to a raster image (by rendering it using the
  115. font in *font*). A triple of (*width*, *height*, *pixels*) is
  116. returned; *pixels* is in boxed row packed pixel format.
  117. """
  118. # Assumes monospaced font.
  119. x = 8*len(m)
  120. y = 8
  121. return x,y,itertools.imap(lambda row: itertools.chain(*row),
  122. zip(*map(char, m)))
  123. def render(message, out):
  124. import png
  125. x,y,pixels = texttoraster(message)
  126. w = png.Writer(x, y, greyscale=True, bitdepth=1)
  127. w.write_packed(out, pixels)
  128. out.flush()
  129. def main(argv=None):
  130. import sys
  131. if argv is None:
  132. argv = sys.argv
  133. if len(argv) > 1:
  134. message = argv[1]
  135. else:
  136. message = sys.stdin.read()
  137. render(message, sys.stdout)
  138. if __name__ == '__main__':
  139. main()