Sure, I can help you with this. We can create a simple web application using Python and Flask. You'll also need to have the following libraries installed: Flask, Flask-Upload, OpenCV (cv2), and Numpy.
Here's the complete code:
import os
import cv2
import numpy as np
from flask import Flask, render_template, request, send_from_directory, url_for
from flask_uploads import UploadSet, configure_uploads, IMAGES, patch_request_class
app = Flask(__name__)
app.config['SECRET_KEY'] = 'supersecretkey'
# Flask-Uploads settings
app.config['UPLOADED_PHOTOS_DEST'] = os.path.join('static', 'uploads')
photos = UploadSet('photos', IMAGES)
configure_uploads(app, photos)
patch_request_class(app)
def ascii_generator(image_path, charset, scale):
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
height, width = img.shape
new_width = int(scale * width)
new_height = int(new_width * 0.5)
resized_img = cv2.resize(img, (new_width, new_height))
ascii_img = []
for row in resized_img:
ascii_row = [charset[int(pixel / 255 * (len(charset) - 1))] for pixel in row]
row_str = "".join(ascii_row)
ascii_img.append(row_str)
return "\n".join(ascii_img)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload():
if 'photo' not in request.files:
return 'No file uploaded', 400
photo = request.files['photo']
if photo.filename == '':
return 'No file selected', 400
filename = photos.save(request.files['photo'])
photo_url = photos.url(filename)
image_path = os.path.join(app.config['UPLOADED_PHOTOS_DEST'], filename)
charset = request.form.get('charset', '#@%S?|+;-:. ')
scale = float(request.form.get('scale', 0.1))
ascii_art = ascii_generator(image_path, charset, scale)
return render_template('ascii_art.html', photo_url=photo_url, ascii_art=ascii_art)
if __name__ == '__main__':
app.run(debug=True)
And now, create templates folder in your project folder and inside it, create two files: index.html and ascii_art.html.
Paste the following code into index.html:
<!doctype html>
<html>
<head>
<title>Video to ASCII</title>
</head>
<body>
<h1>Upload video to convert to ASCII art</h1>
<form action="{{ url_for('upload') }}" method="post" enctype="multipart/form-data">
{{ form.csrf_token }}
<input type="file" name="photo"><br>
<label>Charset: </label><input type="text" name="charset" value="!-~" required><br>
<label>Scale: </label><input type="number" step="0.01" min="0.01" max="1" name="scale" value="0.1" required><br>
<input type="submit" value="Upload and Convert">
</form>
</body>
</html>
And paste the following code into ascii_art.html:
<!doctype html>
<html>
<head>
<title>ASCII Art</title>
<style>
pre {
font-size: 4px;
line-height: 4px;
background-color: black;
color: white;
}
</style>
</head>
<body>
<h1>Original Image</h1>
<img src="{{ photo_url }}" alt="Uploaded Image">
<h1>ASCII Art</h1>
<pre>{{ ascii_art }}</pre>
</body>
</html>
To run the application, make sure that your virtual environment has Flask and the mentioned libraries installed, and run:
python app.py
Now you should be able to access the application locally at http://localhost:5000. Upload a video, choose the charset and scale, and the web app will convert the video to ASCII art.