else/Ascii/ascii.py

162 lines
3.8 KiB
Python
Raw Normal View History

2014-12-01 10:19:13 +00:00
# Gray = 0.21 R + 0.72 G + 0.07 B
from PIL import Image
import json
import time
import sys
2014-12-02 00:37:23 +00:00
import urllib.request
import binascii
import io
2014-12-01 10:19:13 +00:00
2014-12-04 08:34:01 +00:00
def readtable(filename):
try:
asciifile = open('%s.json' % filename)
except FileNotFoundError:
asciifile = open('bin/%s.json' % filename)
asciitable = json.loads(asciifile.read())
asciifile.close()
for key in asciitable:
asciitable[key] = float(asciitable[key]) + 1
return asciitable
asciitable = readtable('asciitable')
asciitable_min = readtable('asciitable_min')
2014-12-01 10:19:13 +00:00
def average(inx):
r, g, b = 0, 0, 0
width = inx.size[0]
height = inx.size[1]
area = width * height
transparent = False
for y in range(height):
for x in range(width):
pixel = inx.getpixel((x, y))
if "RGB" in inx.mode:
try:
if pixel[3] == 0:
transparent = True
except IndexError:
pass
r += pixel[0]
g += pixel[1]
b += pixel[2]
else:
r += pixel
g += pixel
b += pixel
if not transparent:
r = int(r/ area)
g = int(g/ area)
b = int(b/ area)
i = int((0.21 * r) + (0.72 * g) + (0.07 * b))
return [r, g, b, i]
return [255, 255, 255, 255]
def boot():
global asciikeys
global asciivals
try:
2014-12-02 00:37:23 +00:00
FILENAME = input('File or URL: ')
2014-12-01 10:19:13 +00:00
if '.' not in FILENAME:
FILENAME += '.png'
resolution = input('x y (leave blank for standard): ')
if resolution != "":
XVALUE = int(resolution.split()[0])
YVALUE = int(resolution.split()[1])
else:
XVALUE = 8
2014-12-04 08:34:01 +00:00
YVALUE = 17
2014-12-01 10:19:13 +00:00
CONTRAST = input('Smooth 1-~ (blank for standard): ')
try:
CONTRAST = int(CONTRAST)
if CONTRAST < 1:
CONTRAST = 1
except ValueError:
CONTRAST = 8
2014-12-04 08:34:01 +00:00
table = input('Table, Full or Min (blank default): ')
if table.lower() == 'min':
table = asciitable_min
else:
table = asciitable
2014-12-01 10:19:13 +00:00
except EOFError:
2014-12-04 08:34:01 +00:00
FILENAME = "examples/Raspberry.png"
2014-12-01 10:19:13 +00:00
XVALUE = 8
2014-12-04 08:34:01 +00:00
YVALUE = 17
2014-12-01 10:19:13 +00:00
CONTRAST = 8
2014-12-04 08:34:01 +00:00
table = asciitable
2014-12-02 01:55:36 +00:00
# Optimal resolutions ratios:
# 1 :: 2
# 8 :: 17
# 3 :: 5
# Different images look better in certain resolutions
2014-12-04 08:34:01 +00:00
ascii(FILENAME, XVALUE, YVALUE, CONTRAST, ticker, table)
2014-12-01 10:19:13 +00:00
2014-12-02 00:37:23 +00:00
def ticker(status):
print('\r%s' % status, end="")
2014-12-04 08:34:01 +00:00
def ascii(FILENAME, XVALUE, YVALUE, CONTRAST, TICKERFUNCTION, table):
asciikeys = list(table.keys())
asciivals = list(table.values())
asciikeys.sort(key=table.get)
each = 256 / len(asciivals)
asciivals = [((x+1) * each) for x in range(len(asciivals))]
asciikeys_x = asciikeys[::CONTRAST]
asciivals_x = asciivals[::CONTRAST]
asciivals_x[-1] = 256
asciikeys_x[-1] = ' '
2014-12-02 00:37:23 +00:00
if 'http' in FILENAME and '/' in FILENAME:
2014-12-04 08:34:01 +00:00
TICKERFUNCTION("Downloading")
2014-12-02 00:37:23 +00:00
rpi = urllib.request.urlopen(FILENAME).read()
FILENAME = FILENAME.split('/')[-1]
#rpi = binascii.unhexlify(rpi)
rfile = open(FILENAME, 'wb')
rfile.write(rpi)
rfile.close()
rpi = io.BytesIO(rpi)
rpi = Image.open(rpi)
else:
rpi = Image.open(FILENAME)
2014-12-01 10:19:13 +00:00
width = rpi.size[0]
height = rpi.size[1]
charspanx = int(width / XVALUE)
charspany = int(height / YVALUE)
print("%d x %d characters" % (charspanx, charspany))
output = ""
print("Working...")
for yline in range(charspany):
2014-12-02 00:37:23 +00:00
status = "%0.2f" % (yline / charspany)
TICKERFUNCTION(status)
2014-12-02 01:09:13 +00:00
try:
sys.stdout.flush()
except:
pass
2014-12-01 10:19:13 +00:00
for xline in range(charspanx):
xcoord = xline*XVALUE
ycoord = yline*YVALUE
region = (xcoord, ycoord, xcoord+XVALUE, ycoord+YVALUE)
region = rpi.crop(region)
av = average(region)
i = av[3]
c = 0
2014-12-02 00:37:23 +00:00
for value in asciivals_x:
2014-12-01 10:19:13 +00:00
if i == 255:
output += ' '
break
if i <= value:
2014-12-02 00:37:23 +00:00
output += asciikeys_x[c]
2014-12-01 10:19:13 +00:00
break
c += 1
output += "\n"
2014-12-04 08:34:01 +00:00
tabletype = "Full" if table == asciitable else "Min"
output += '\n%s\n%dx%d (%dx%d)\n%d\n%s' % (FILENAME, charspanx, charspany, XVALUE, YVALUE, CONTRAST, tabletype)
2014-12-01 10:19:13 +00:00
outfile = FILENAME.split('.')[0] + '.txt'
outfile = open(outfile, 'w')
print(output, file=outfile)
outfile.close()
2014-12-02 00:37:23 +00:00
TICKERFUNCTION("1.00")
print()
2014-12-01 10:19:13 +00:00
if __name__ == "__main__":
boot()