-
Notifications
You must be signed in to change notification settings - Fork 0
/
image2ascii.py
149 lines (115 loc) · 4.12 KB
/
image2ascii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#HA i coded this in an hour and a half and it doesn't look too bad
#import libraries for reading/writing images
from PIL import Image, ImageFilter
import os
#import libraries for reading command line arguments
import sys, getopt
#import numpy for processing arrays
import numpy
def main(argv):
inputfile, outputfile, depth = processargs(argv)
#try opening the image
try:
myimg = Image.open(inputfile)
except IOError:
print "Error reading image file"
sys.exit(2)
#prepare the image
pixelatedimage = prepareimage(myimg, depth)
#turn it to an array
pixarray = makearray(pixelatedimage)
#process the array
procarray = converttochars(pixarray)
#write the output
fo = open(outputfile,'w')
for row in procarray:
#print row
for val in row:
fo.write(val)
fo.write('\n')
fo.close()
#convert to characters based on value
def converttochars(pixarray):
#array of chars in increasing darnkess
chars = [' ', '.','-','~','=','!',']','}','#','$','%','&','@',]
procarray = numpy.chararray(pixarray.shape)
k = 0
for row in pixarray:
j = 0
for val in row:
val = 255.0-val
i = ((len(chars)-1)*(val/255.0))
i = int(round(i))
procarray[k][j] = chars[i]
j+=1
k+=1
return procarray
#get array of pixel values
def makearray(pixelatedimage):
pixels = numpy.asarray(pixelatedimage)
return pixels
#turn image into a square, black and white, and pixelated
def prepareimage(myimg, depth):
#find dimensions for square image
width, height = myimg.size
if (width > height):
newsize = width
else:
newsize = height
if (newsize%2 == 1):
newsize += 1
#create white square, paste original image over it
sqimg = Image.new('RGBA', (newsize, newsize), (255, 255, 255, 255))
offset = ((newsize - width)/2, (newsize - height) / 2)
sqimg.paste(myimg, offset)
#turn to monochrome
sqimg = sqimg.convert("L")
#calculate pixelation depth
depth = depth / 100
pixfactor = newsize * depth
pixfactor = int(pixfactor)
#pixelate
pixelatedimage = sqimg.copy()
pixelatedimage.thumbnail((pixfactor,pixfactor))
pixelatedimage = pixelatedimage.resize((pixfactor*2,pixfactor))
#output progress image, for debugging
#progimg = pixelatedimage.resize((newsize,newsize))
#progimg.save("progress.png","png")
return pixelatedimage
# process command line arguments
def processargs(argv):
#set defaults
inputfile = ''
outputfile = 'out.txt'
depth = 8.0;
#try reading args using getopt
try:
opts, args = getopt.getopt(argv,"hi:o:d:",["ifile=","ofile=","depth="])
except getopt.GetoptError:
print 'usage: image2ascii.py -i <inputfile> -o <outputfile> -d <depth>\n<depth> should be between 0 (exclusive) and 100 (inclusive), default is 8'
sys.exit(2)
#process the args
for opt, arg in opts:
if opt == '-h':
print 'usage: image2ascii.py -i <inputfile> -o <outputfile> -d <depth>\n<depth> should be between 0 (exclusive) and 100 (inclusive), default is 8'
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
elif opt in ("-d", "--depth"):
depth = float(arg)
#make sure there is an input file
if inputfile == '':
print 'usage: image2ascii.py -i <inputfile> -o <outputfile> -d <depth>\n<depth> should be between 1 and 100 inclusive'
sys.exit(2)
#make sure depth is correct
if depth <= 0 or depth > 100:
print 'usage: image2ascii.py -i <inputfile> -o <outputfile> -d <depth>\n<depth> should be between 0 (exclusive) and 100 (inclusive)'
sys.exit(2)
#print 'Input file is "', inputfile
#print 'Output file is "', outputfile
#print 'Depth is ', depth
return inputfile, outputfile, depth
if __name__ == "__main__":
main(sys.argv[1:])