Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python2
- # coding: utf-8
- from PIL import Image
- from urllib import urlopen
- from StringIO import StringIO
- chars = r'#@%MI=-*:,.'
- # opens the image from desired url (here - flag of Wales)
- 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()))
- w, h = img.size
- # resizes the image to make it fit into the terminal
- newW = 80 # 80 is the default terminal's width; you can change that value to make the the result bigger and nicer
- newH = int(0.5 * (h * newW) / w) # the 0.5 is there because character in the terminal is higher than wider
- img = img.resize((newW, newH))
- img = img.convert('L') # converts the image to grayscale
- 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]
- # prints the text with correct line breaking
- for i in xrange(newH):
- print text[i*newW : (i+1)*newW]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement