Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # extracted from celery
  3.  
  4. from __future__ import absolute_import, unicode_literals
  5. import os
  6. import sys
  7.  
  8. TERM = os.environ.get('TERM')
  9.  
  10. TERM_IS_SCREEN = TERM and TERM.startswith('screen')
  11. _IMG_PRE = '\033Ptmux;\033\033]' if TERM_IS_SCREEN else '\033]'
  12. _IMG_POST = '\a\033\\' if TERM_IS_SCREEN else '\a'
  13.  
  14.  
  15.  
  16. def isatty(fh):
  17. try:
  18. return fh.isatty()
  19. except AttributeError:
  20. pass
  21.  
  22. def supports_images():
  23. return isatty(sys.stdin) and os.environ.get('ITERM_PROFILE')
  24.  
  25.  
  26. def _read_as_base64(path):
  27. import codecs
  28. import base64
  29. with codecs.open(path, mode='rb') as fh:
  30. encoded = base64.b64encode(fh.read())
  31. return encoded if type(encoded) == 'str' else encoded.decode('ascii')
  32.  
  33. def imgcat(path, inline=1, preserve_aspect_ratio=0, **kwargs):
  34. return '\n%s1337;File=inline=%d;preserveAspectRatio=%d:%s%s' % (
  35. _IMG_PRE, inline, preserve_aspect_ratio,
  36. _read_as_base64(path), _IMG_POST)
  37.  
  38.  
  39. if __name__ == '__main__':
  40. if len(sys.argv) > 1:
  41. for fn in sys.argv[1:]:
  42. print(imgcat(fn))
  43. else:
  44. print '\n'.join([i.strip() for i in """A script for displaying image(s) on terminal.
  45. Usage: python {} <file1.png> [file2.png ...]
  46. """.splitlines()])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement