dump_char_my.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. #!/usr/bin/env python3
  2. from PIL import Image
  3. from pprint import pprint
  4. import argparse, sys, time, pickle
  5. ON = 'X'
  6. OFF = '.'
  7. EMPTY = False
  8. hexbug = True
  9. # 128
  10. INTENSITY = 64
  11. parser = argparse.ArgumentParser( description='Display Graphical Image.')
  12. parser.add_argument( 'FILENAME', help='Image file to process' )
  13. parser.add_argument( '--hex', help='Display output in hexadecimal', action="store_true" )
  14. parser.add_argument( '--all', help='Display all of the image.', action="store_true" )
  15. parser.add_argument( '-d', help='Display image.', action="store_true" )
  16. args = parser.parse_args()
  17. filename = args.FILENAME
  18. print("Loading", filename)
  19. im = Image.open(filename)
  20. # pix = im.convert('RGB') # this is even worse. No!
  21. pix = im.load()
  22. size = im.size
  23. print("size:",size[0],"x",size[1])
  24. # print im.format, im.size, im.mode, im.info
  25. if im.mode == 'P':
  26. pal = im.getpalette()
  27. # print "Palette is:", pal
  28. def img_point(x,y):
  29. global pix
  30. return pix[x,y]
  31. # return pix.getpixel((x,y))
  32. def img_average(x,y):
  33. global im, pal
  34. rgb = img_point(x,y)
  35. # Image is in palette mode, convert from palette
  36. # index to RGB value.
  37. if im.mode == 'P':
  38. # print "rgb=",rgb
  39. rgb = pal[rgb*3:(rgb+1)*3]
  40. # print "rgb now:",rgb
  41. if im.mode == 'I':
  42. return rgb >> 8
  43. return int( ( rgb[0] + rgb[1] + rgb[2] ) / 3 )
  44. def is_set(x,y):
  45. avg = img_average(x,y)
  46. return (avg < INTENSITY )
  47. # This has start and end ranges to their max values.
  48. sx = 0
  49. ex = size[0]
  50. sy = 0
  51. ey = size[1]
  52. total = 0
  53. def scan_image():
  54. global sx, ex, sy, ey, total
  55. total = 0
  56. # Max out the start and end points. We adjust
  57. # as we find points within the image.
  58. sx = size[0]; ex = 0
  59. sy = size[1]; ey = 0
  60. for y in range( 0,size[1] ):
  61. for x in range( 0,size[0] ):
  62. # c = img_point(x,y)
  63. pnt_is = is_set(x,y)
  64. if (pnt_is):
  65. total += 1
  66. # print (x,y), (sx,ex,sy,ey)
  67. if x < sx:
  68. sx = x
  69. if x > ex:
  70. ex = x
  71. if y < sy:
  72. sy = y
  73. if y > ey:
  74. ey = y
  75. #print (sx,ex,sy,ey)
  76. # give us a little border to work with
  77. if sx > 0:
  78. sx -= 1
  79. if ex < size[0]:
  80. ex += 1
  81. if sy > 0:
  82. sy -= 1
  83. if ey < size[1]:
  84. ey += 1
  85. #print (sx,ex,sy,ey)
  86. return(sx,sy,ex,ey)
  87. scan_image()
  88. print("Character within ({0},{1}) - ({2},{3}) total {4} pixels...".format( sx, sy, ex, ey, total ))
  89. def display_image():
  90. global args
  91. if args.all:
  92. for y in range(0,size[1]):
  93. s = ''
  94. for x in range(0,size[0]):
  95. if args.hex:
  96. s += '{0:02X}'.format( img_average(x,y))
  97. else:
  98. if is_set(x,y):
  99. s += ON
  100. else:
  101. s += OFF
  102. print(s)
  103. else:
  104. for y in range(sy,ey):
  105. s = ''
  106. for x in range(sx,ex):
  107. if args.hex:
  108. s += '{0:02X}'.format( img_average(x,y) )
  109. else:
  110. if is_set(x,y):
  111. s += ON
  112. else:
  113. s += OFF
  114. print(s)
  115. print(" --")
  116. def save_image():
  117. global args
  118. result = []
  119. if args.all:
  120. for y in range(0,size[1]):
  121. s = ''
  122. for x in range(0,size[0]):
  123. if args.hex:
  124. s += '{0:02X}'.format( img_average(x,y))
  125. else:
  126. if is_set(x,y):
  127. s += ON
  128. else:
  129. s += OFF
  130. result.append(s)
  131. else:
  132. for y in range(sy,ey):
  133. s = ''
  134. for x in range(sx,ex):
  135. if args.hex:
  136. s += '{0:02X}'.format( img_average(x,y) )
  137. else:
  138. if is_set(x,y):
  139. s += ON
  140. else:
  141. s += OFF
  142. result.append(s)
  143. return result
  144. display_image()
  145. img_s = save_image()
  146. img_s.append('net')
  147. #img_s1 = filter_image(img_s)
  148. # print "border:",border
  149. with open('out.txt', 'w') as f:
  150. for e in img_s
  151. f.write(e)