Created
October 1, 2012 14:03
-
-
Save zokis/3811976 to your computer and use it in GitHub Desktop.
Ascii Art em escala de cinza
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.jpg | |
*.png |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Author: Marcelo Fonseca Tambalo | |
# Date: October 1, 2012 | |
from PIL import Image | |
from bisect import bisect_left | |
import sys | |
import optparse | |
def main(argv=None): | |
opar = optparse.OptionParser() | |
opar.add_option("-i", "--imagem", dest="imagem", | |
help="imagem", type="str", default=None) | |
opar.add_option("-m", "--html", help="saida em html", action="store_true", dest="html") | |
options, args = opar.parse_args(argv) | |
img_path = options.imagem | |
if not options.imagem: | |
img_path = raw_input('digite o caminho para a imagem: ') | |
art = [] | |
if options.html: | |
art = ['<pre style="font-size:10px">\n'] | |
#escala_de_cinza = [' ', '.', '-', ':', '+', '*', '=', '#', '@'] | |
escala_de_cinza = [' ', '.', ':', '=', '#', '@'] | |
#escala_de_cinza = [' ', '.', ':', '+', '*', '=', '#', '@'] | |
t_escala = len(escala_de_cinza) | |
d = 255 // t_escala | |
variacao = [(i + d) for i in range(0, 256, d - 1)] | |
if len(variacao) > t_escala: | |
escala_de_cinza.append('@') | |
image = Image.open(img_path) | |
image = image.resize((image.size[1], int(image.size[0] * 0.405)), Image.ANTIALIAS) | |
image = image.convert('L') | |
for coluna in range(0, image.size[1]): | |
for linha in range(0, image.size[0]): | |
luminosidade = 255 - image.getpixel((linha, coluna)) | |
art.append(escala_de_cinza[bisect_left(variacao, luminosidade)]) | |
art.append('\n') | |
if options.html: | |
art.append('</pre>') | |
return ''.join(art) | |
if __name__ == '__main__': | |
print main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment