ruslanmv commited on
Commit
fbf5e14
1 Parent(s): 41e193d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +80 -2
main.py CHANGED
@@ -1,3 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  from PIL import Image
@@ -38,8 +117,7 @@ iface = gr.Interface(
38
  )
39
 
40
  iface.launch(server_name="0.0.0.0", server_port=7860)
41
-
42
-
43
 
44
  '''
45
  import gradio as gr
 
1
+ import sys, random, argparse
2
+ import numpy as np
3
+ import math
4
+ from PIL import Image, ImageFont, ImageDraw
5
+ import gradio as gr
6
+
7
+ # 70 levels of gray
8
+ gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,\\"^`'. "
9
+
10
+ # 10 levels of gray
11
+ gscale2 = '@%#*+=-:. '
12
+
13
+ def getAverageL(image):
14
+ im = np.array(image)
15
+ w, h = im.shape
16
+ return np.average(im.reshape(w*h))
17
+
18
+ def covertImageToAscii(input_img, cols, scale, moreLevels):
19
+ global gscale1, gscale2
20
+
21
+ image = Image.fromarray(input_img).convert('L')
22
+ W, H = image.size[0], image.size[1]
23
+
24
+ w = W/cols
25
+ h = w/scale
26
+ rows = int(H/h)
27
+
28
+ if cols > W or rows > H:
29
+ print("Image too small for specified cols!")
30
+ exit(0)
31
+
32
+ aimg = []
33
+ for j in range(rows):
34
+ y1 = int(j*h)
35
+ y2 = int((j+1)*h)
36
+
37
+ if j == rows-1:
38
+ y2 = H
39
+
40
+ aimg.append("")
41
+
42
+ for i in range(cols):
43
+ x1 = int(i*w)
44
+ x2 = int((i+1)*w)
45
+
46
+ if i == cols-1:
47
+ x2 = W
48
+
49
+ img = image.crop((x1, y1, x2, y2))
50
+ avg = int(getAverageL(img))
51
+
52
+ if moreLevels:
53
+ gsval = gscale1[int((avg*69)/255)]
54
+ else:
55
+ gsval = gscale2[int((avg*9)/255)]
56
+
57
+ aimg[j] += gsval
58
+
59
+ return aimg
60
+
61
+ def sepia(input_img):
62
+ aimg = covertImageToAscii(input_img, 200, 0.43, False)
63
+
64
+ my_image = Image.new(mode="RGB", size=(2000, 2000), color=(0, 0, 0))
65
+ image_editable = ImageDraw.Draw(my_image)
66
+ image_editable.text((10, 10), "\n".join(aimg), (237, 230, 211))
67
+
68
+ return [my_image, "\n".join(aimg)]
69
+
70
+ iface = gr.Interface(sepia,
71
+ gr.inputs.Image(shape=(200, 200)),
72
+ ["image", "text"],
73
+ title = "ASCII Art",
74
+ description = "Convert an image to ASCII art based on ascii character density. Copy and paste the text to a notepad to see it correctly")
75
+
76
+
77
+ iface.launch(server_name="0.0.0.0", server_port=7860)
78
+
79
+ '''
80
  import gradio as gr
81
  import numpy as np
82
  from PIL import Image
 
117
  )
118
 
119
  iface.launch(server_name="0.0.0.0", server_port=7860)
120
+ '''
 
121
 
122
  '''
123
  import gradio as gr