Advertisement
Guest User

bitmapToASCII.py

a guest
Apr 24th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. #!/usr/local/bin/python2
  2. # encoding: UTF-8
  3.  
  4. import sys
  5. import argparse
  6.  
  7. from PIL import Image
  8.  
  9. class conf(object):
  10.     pad  = True
  11.     bw   = False
  12.     file = None
  13.  
  14. class Color(object):
  15.     CLEAR = "\033[0m"
  16.     ALPHA_LIMIT = 1
  17.  
  18.     GREYSCALE_BEGIN = 232
  19.     COLOR_BEGIN     = 16
  20.  
  21.     def __init__(self, rgba):
  22.         r, g, b, a = rgba
  23.         self.r = r
  24.         self.g = g
  25.         self.b = b
  26.         self.a = a
  27.  
  28.     def escape(self, value):
  29.         return "\033[48;5;%dm" % value
  30.  
  31.     def code(self):
  32.         is_clear = self.a < self.ALPHA_LIMIT
  33.         if   is_clear: return self.CLEAR
  34.         elif conf.bw:  return self.bw_code()
  35.         else:          return self.color_code()
  36.  
  37.     def color_code(self):
  38.         ratio = 5.0 / 255.0
  39.         r = int(round(ratio * self.r))
  40.         g = int(round(ratio * self.g))
  41.         b = int(round(ratio * self.b))
  42.         return self.escape(
  43.             r * 6 * 6 +
  44.             g * 6 +
  45.             b +
  46.             self.COLOR_BEGIN
  47.         )
  48.  
  49.     def bw_code(self):
  50.         ratio = 24.0 / 255.0
  51.         avg   = (self.r + self.g + self.b)/3.0
  52.         shade = int(round(ratio * avg))
  53.         return self.escape(shade + self.GREYSCALE_BEGIN)
  54.  
  55. def calc_size(img):
  56.     iw, ih = img.size
  57.     aspect = float(iw) / float(ih) # image aspect
  58.     font   = 2.0 # font aspect
  59.     h = ih
  60.     w = h * aspect * font
  61.     w = int(round(w))
  62.  
  63.     return (w, h)
  64.  
  65. def main(**kwargs):
  66.     img  = Image.open(conf.file)
  67.     w, h = calc_size(img)
  68.     img  = img.resize(calc_size(img))
  69.     img  = img.convert("RGBA")
  70.     pix  = img.load()
  71.  
  72.     write = sys.stdout.write
  73.  
  74.     if conf.pad:
  75.         write("\n")
  76.     for y in xrange(h):
  77.         sys.stdout.write(Color.CLEAR)
  78.         if conf.pad:
  79.             write(" ")
  80.             write(" ")
  81.         for x in xrange(w):
  82.             rgba = pix[x, y]
  83.             code = Color(rgba).code()
  84.             write(code)
  85.             write(" ")
  86.         write(Color.CLEAR)
  87.         write("\n")
  88.  
  89. parser = argparse.ArgumentParser(
  90.     description="Print ANSI art for an image to standard output."
  91. )
  92. parser.add_argument(
  93.     "file",
  94.     type=str,
  95.     help="File to convert"
  96. )
  97. parser.add_argument(
  98.     "--bw",
  99.     action="store_const",
  100.     const=True,
  101.     default=False,
  102.     help="Enable greyscale mode"
  103. )
  104. parser.add_argument(
  105.     "--no-pad",
  106.     action="store_const",
  107.     const=True,
  108.     default=False,
  109.     help="Disable padding"
  110. )
  111. args = parser.parse_args()
  112. conf.bw   = args.bw
  113. conf.pad  = not args.no_pad
  114. conf.file = args.file
  115. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement