62 lines
1.4 KiB
Python
Executable File
62 lines
1.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
import scipy.misc
|
|
import numpy as n
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
def png2asc(inFile):
|
|
im = scipy.misc.imread(inFile)
|
|
|
|
for r in im:
|
|
for c in r:
|
|
if c>127:
|
|
char = "X"
|
|
if c<128:
|
|
char = "."
|
|
if c==0:
|
|
char = " "
|
|
sys.stdout.write(char)
|
|
sys.stdout.write("\n")
|
|
|
|
def asc2png(inFile, outFile):
|
|
reader = open(inFile,'r')
|
|
coords = []
|
|
max_c = 0
|
|
for r,row in enumerate(reader):
|
|
for c,col in enumerate(row):
|
|
if col not in (' ', '\n'):
|
|
if col == ".":
|
|
coords.append((r, c, 127))
|
|
else:
|
|
coords.append((r, c, 255))
|
|
max_c = max(max_c, c)
|
|
im=n.zeros( (r, max_c-1), dtype = n.uint8 )
|
|
for c in coords:
|
|
im[c[0], c[1]] = c[2]
|
|
|
|
scipy.misc.imsave(outFile, im)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = ArgumentParser(
|
|
description = "Convert image to ascii, or ascii to image. If only input is defined, it is assumed as image (PNG)."
|
|
)
|
|
parser.add_argument(
|
|
'input',
|
|
action = "store"
|
|
)
|
|
parser.add_argument(
|
|
'output',
|
|
action = "store",
|
|
nargs = '?'
|
|
)
|
|
opts = parser.parse_args()
|
|
|
|
if opts.output == None:
|
|
png2asc(opts.input)
|
|
else:
|
|
asc2png(opts.input, opts.output)
|
|
|
|
|