|
|
|
import sys, random, argparse |
|
import numpy as np |
|
import math |
|
from PIL import Image, ImageFont, ImageDraw |
|
import gradio as gr |
|
|
|
|
|
gscale1 = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. " |
|
|
|
|
|
gscale2 = '@%#*+=-:. ' |
|
|
|
def getAverageL(image): |
|
|
|
""" |
|
Given PIL Image, return average value of grayscale value |
|
""" |
|
|
|
im = np.array(image) |
|
|
|
w,h = im.shape |
|
|
|
|
|
return np.average(im.reshape(w*h)) |
|
|
|
def covertImageToAscii(input_img, cols, scale, moreLevels): |
|
""" |
|
Given Image and dims (rows, cols) returns an m*n list of Images |
|
""" |
|
|
|
global gscale1, gscale2 |
|
|
|
|
|
image = Image.fromarray(input_img).convert('L') |
|
|
|
|
|
W, H = image.size[0], image.size[1] |
|
print("input image dims: %d x %d" % (W, H)) |
|
|
|
|
|
w = W/cols |
|
|
|
|
|
h = w/scale |
|
|
|
|
|
rows = int(H/h) |
|
|
|
print("cols: %d, rows: %d" % (cols, rows)) |
|
print("tile dims: %d x %d" % (w, h)) |
|
|
|
|
|
if cols > W or rows > H: |
|
print("Image too small for specified cols!") |
|
exit(0) |
|
|
|
|
|
aimg = [] |
|
|
|
for j in range(rows): |
|
y1 = int(j*h) |
|
y2 = int((j+1)*h) |
|
|
|
|
|
if j == rows-1: |
|
y2 = H |
|
|
|
|
|
aimg.append("") |
|
|
|
for i in range(cols): |
|
|
|
|
|
x1 = int(i*w) |
|
x2 = int((i+1)*w) |
|
|
|
|
|
if i == cols-1: |
|
x2 = W |
|
|
|
|
|
img = image.crop((x1, y1, x2, y2)) |
|
|
|
|
|
avg = int(getAverageL(img)) |
|
|
|
|
|
if moreLevels: |
|
gsval = gscale1[int((avg*69)/255)] |
|
else: |
|
gsval = gscale2[int((avg*9)/255)] |
|
|
|
|
|
aimg[j] += gsval |
|
|
|
|
|
return aimg |
|
|
|
|
|
def sepia(input_img): |
|
aimg = covertImageToAscii(input_img, 200, 0.43, False) |
|
blank_image = Image.new(mode="RGB", size=(2000, 2000)) |
|
blank_image.save("blank_image.png") |
|
|
|
my_image = Image.open("blank_image.png") |
|
image_editable = ImageDraw.Draw(my_image) |
|
image_editable.text((400,400), "\n".join(aimg), (237, 230, 211)) |
|
return [my_image, "\n".join(aimg)] |
|
|
|
|
|
iface = gr.Interface(sepia, |
|
gr.inputs.Image(shape=(200, 200)), |
|
["image", "text"], |
|
title = "ASCII Art", |
|
description = "Convert an image to ASCII art based on ascii character density. Copy and paste the text to a notepad to see it correctly") |
|
|
|
iface.launch() |