Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,86 +1,4 @@
|
|
| 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 |
-
inputs="image",
|
| 73 |
-
["image", "text"],
|
| 74 |
-
title="ASCII Art Generator",
|
| 75 |
-
description="Upload an image, and this app will turn it into ASCII art!",
|
| 76 |
-
live=True
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 82 |
-
|
| 83 |
-
'''
|
| 84 |
import gradio as gr
|
| 85 |
import numpy as np
|
| 86 |
from PIL import Image
|
|
@@ -97,7 +15,8 @@ def generate_ascii_art(image):
|
|
| 97 |
img = img.convert("L")
|
| 98 |
|
| 99 |
# Define ASCII characters to represent different intensity levels
|
| 100 |
-
ascii_chars = "@%#*+=-:. "
|
|
|
|
| 101 |
|
| 102 |
# Convert each pixel to ASCII character based on intensity
|
| 103 |
ascii_image = ""
|
|
@@ -121,7 +40,7 @@ iface = gr.Interface(
|
|
| 121 |
)
|
| 122 |
|
| 123 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 124 |
-
|
| 125 |
|
| 126 |
'''
|
| 127 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 15 |
img = img.convert("L")
|
| 16 |
|
| 17 |
# Define ASCII characters to represent different intensity levels
|
| 18 |
+
#ascii_chars = "@%#*+=-:. "
|
| 19 |
+
ascii_chars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/|()1{}[]?-_+~<>i!lI;:,\\^`'. "
|
| 20 |
|
| 21 |
# Convert each pixel to ASCII character based on intensity
|
| 22 |
ascii_image = ""
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
| 43 |
+
|
| 44 |
|
| 45 |
'''
|
| 46 |
import gradio as gr
|