Advertisement
lewapkon

03.6.py

Mar 4th, 2014
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. #!/usr/bin/python2
  2. # coding: utf-8
  3.  
  4. from PIL import Image
  5. from urllib import urlopen
  6. from StringIO import StringIO
  7.  
  8. chars = r'#@%MI=-*:,.'
  9.  
  10. # opens the image from desired url (here - flag of Wales)
  11. img = Image.open(StringIO(urlopen("http://upload.wikimedia.org/wikipedia/commons/thumb/5/59/Flag_of_Wales_2.svg/1000px-Flag_of_Wales_2.svg.png").read()))
  12. w, h = img.size
  13.  
  14. # resizes the image to make it fit into the terminal
  15. newW = 80 # 80 is the default terminal's width; you can change that value to make the the result bigger and nicer
  16. newH = int(0.5 * (h * newW) / w) # the 0.5 is there because character in the terminal is higher than wider
  17. img = img.resize((newW, newH))
  18.  
  19. img = img.convert('L') # converts the image to grayscale
  20. text = ''.join( chars[pixel / 25] for pixel in img.getdata()) # turns the image into ascii characters depending on the level of luminosity of every pixel [0, 10]
  21.  
  22. # prints the text with correct line breaking
  23. for i in xrange(newH):
  24.     print text[i*newW : (i+1)*newW]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement