HighCWu commited on
Commit
baff8b7
·
1 Parent(s): 6462a42

enable queue, add svg file output

Browse files
Files changed (1) hide show
  1. app.py +10 -15
app.py CHANGED
@@ -5,9 +5,8 @@ Refer to https://huggingface.co/spaces/dt/ascii-art/blob/main/app.py
5
  import sys, random, argparse
6
  import numpy as np
7
  import math
8
- import gradio as gr
9
-
10
  from PIL import Image, ImageFont, ImageDraw
 
11
 
12
  # 70 levels of gray
13
  gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
@@ -145,26 +144,20 @@ def colorizeTextImage(input_img, text_img):
145
 
146
  def convertTextToHTML(our_colors, aimg):
147
  bimg = r'''
148
- <!DOCTYPE html>
149
- <html>
150
- <body>
151
  <style>text{ font-size:8px; }</style>
152
- <svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="width: 100vw;height: 100vh;" viewBox="0, 0, 1200, 1200">
153
  '''
154
  for i in range(our_colors.shape[0]):
155
  our_colors2 = our_colors[i]
156
  aimg2 = aimg[i]
157
  for j in range(our_colors2.shape[0]):
158
  [r, g, b] = our_colors2[j]
159
- p = aimg2[j]
160
  if p == ' ': continue
161
- aimg3 = f'<text x="{j*6}" y="{i*11}" style="fill:rgb{int(r),int(g),int(b)};">{p}</text>\n'
162
  bimg += aimg3
163
  bimg += r'''
164
  </svg>
165
-
166
- </body>
167
- </html>
168
  '''
169
 
170
  return bimg
@@ -191,14 +184,16 @@ def sepia(input_img):
191
 
192
  my_image, my_colors = colorizeTextImage(input_img, my_image)
193
  my_html = convertTextToHTML(my_colors, aimg)
 
 
194
 
195
- return [my_image, my_html]
196
 
197
 
198
  iface = gr.Interface(sepia,
199
  gr.inputs.Image(),
200
- ["image", "html"],
201
  title = "Colorful ASCII Art",
202
- description = "Convert an image to colorful ASCII art based on ascii character density. It generates converted image and svg.")
203
 
204
- iface.launch()
 
5
  import sys, random, argparse
6
  import numpy as np
7
  import math
 
 
8
  from PIL import Image, ImageFont, ImageDraw
9
+ import gradio as gr
10
 
11
  # 70 levels of gray
12
  gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. "
 
144
 
145
  def convertTextToHTML(our_colors, aimg):
146
  bimg = r'''
147
+ <svg xmlns="http://www.w3.org/2000/svg" version="1.1" style="width: 92vw;" viewBox="-100, -100, 2000, 2000">
 
 
148
  <style>text{ font-size:8px; }</style>
 
149
  '''
150
  for i in range(our_colors.shape[0]):
151
  our_colors2 = our_colors[i]
152
  aimg2 = aimg[i]
153
  for j in range(our_colors2.shape[0]):
154
  [r, g, b] = our_colors2[j]
155
+ p = aimg2[j].replace('<', '&lt;').replace('>', '&gt;').replace('&', '&amp;')
156
  if p == ' ': continue
157
+ aimg3 = f'<text x="{j*6+450}" y="{i*11}" style="fill:rgb{int(r),int(g),int(b)};">{p}</text>\n'
158
  bimg += aimg3
159
  bimg += r'''
160
  </svg>
 
 
 
161
  '''
162
 
163
  return bimg
 
184
 
185
  my_image, my_colors = colorizeTextImage(input_img, my_image)
186
  my_html = convertTextToHTML(my_colors, aimg)
187
+ with open("result.svg", "w") as f:
188
+ f.write(my_html)
189
 
190
+ return [my_image, my_html, "result.svg"]
191
 
192
 
193
  iface = gr.Interface(sepia,
194
  gr.inputs.Image(),
195
+ ["image", "html", "file"],
196
  title = "Colorful ASCII Art",
197
+ description = "Convert an image to colorful ASCII art based on ascii character density. Copy and paste the text to a notepad to see it correctly")
198
 
199
+ iface.launch(enable_queue=True)