Dabs commited on
Commit
33e5333
1 Parent(s): 93db1fe

first commit

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +121 -0
  3. requirements.txt +2 -0
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ venv/
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Python code to convert an image to ASCII image.
2
+ import sys, random, argparse
3
+ import numpy as np
4
+ import math
5
+ from PIL import Image, ImageFont, ImageDraw
6
+ import gradio as gr
7
+
8
+ # 70 levels of gray
9
+ gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
10
+
11
+ # 10 levels of gray
12
+ gscale2 = '@%#*+=-:. '
13
+
14
+ def getAverageL(image):
15
+
16
+ """
17
+ Given PIL Image, return average value of grayscale value
18
+ """
19
+ # get image as numpy array
20
+ im = np.array(image)
21
+ # get shape
22
+ w,h = im.shape
23
+
24
+ # get average
25
+ return np.average(im.reshape(w*h))
26
+
27
+ def covertImageToAscii(input_img, cols, scale, moreLevels):
28
+ """
29
+ Given Image and dims (rows, cols) returns an m*n list of Images
30
+ """
31
+ # declare globals
32
+ global gscale1, gscale2
33
+
34
+ # open image and convert to grayscale
35
+ image = Image.fromarray(input_img).convert('L')
36
+
37
+ # store dimensions
38
+ W, H = image.size[0], image.size[1]
39
+ print("input image dims: %d x %d" % (W, H))
40
+
41
+ # compute width of tile
42
+ w = W/cols
43
+
44
+ # compute tile height based on aspect ratio and scale
45
+ h = w/scale
46
+
47
+ # compute number of rows
48
+ rows = int(H/h)
49
+
50
+ print("cols: %d, rows: %d" % (cols, rows))
51
+ print("tile dims: %d x %d" % (w, h))
52
+
53
+ # check if image size is too small
54
+ if cols > W or rows > H:
55
+ print("Image too small for specified cols!")
56
+ exit(0)
57
+
58
+ # ascii image is a list of character strings
59
+ aimg = []
60
+ # generate list of dimensions
61
+ for j in range(rows):
62
+ y1 = int(j*h)
63
+ y2 = int((j+1)*h)
64
+
65
+ # correct last tile
66
+ if j == rows-1:
67
+ y2 = H
68
+
69
+ # append an empty string
70
+ aimg.append("")
71
+
72
+ for i in range(cols):
73
+
74
+ # crop image to tile
75
+ x1 = int(i*w)
76
+ x2 = int((i+1)*w)
77
+
78
+ # correct last tile
79
+ if i == cols-1:
80
+ x2 = W
81
+
82
+ # crop image to extract tile
83
+ img = image.crop((x1, y1, x2, y2))
84
+
85
+ # get average luminance
86
+ avg = int(getAverageL(img))
87
+
88
+ # look up ascii char
89
+ if moreLevels:
90
+ gsval = gscale1[int((avg*69)/255)]
91
+ else:
92
+ gsval = gscale2[int((avg*9)/255)]
93
+
94
+ # append ascii char to string
95
+ aimg[j] += gsval
96
+
97
+ # return txt image
98
+ return aimg
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+ def sepia(input_img):
109
+ aimg = covertImageToAscii(input_img, 200, 0.43, False)
110
+ blank_image = Image.new(mode="RGB", size=(2000, 2000))
111
+ blank_image.save("blank_image.png")
112
+
113
+ my_image = Image.open("blank_image.png")
114
+ image_editable = ImageDraw.Draw(my_image)
115
+ image_editable.text((400,400), "\n".join(aimg), (237, 230, 211))
116
+ return my_image
117
+
118
+
119
+ iface = gr.Interface(sepia, gr.inputs.Image(shape=(200, 200)), "image")
120
+
121
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ Pillow
2
+ gradio