This is one of the projects for ACM IITR's open projects for summer 2022.
ASCII(American Standard Code for Information Interchange) is a common encoding format used for representing strings and text data in computers.
But…what if we use this for something other than text?
How about images? Here images can be turned into ASCII encoded strings that look like the image. Not just images, how about videos that are asciified??
For this some basic image processing techniques are used to convert an image or video(.mp4) into an ASCII art. We can also convert images or videos into asciified sketches using the same.
Video Demo click
To run this on your local machine, clone the repo:
git clone https://github.com/st3rl4nce/ascii-art-generator.git
cd ascii-art-generator
run:
pip install -r requirements.txt
cd src
run:
python ascii.py "../data/inp_vs.mp4" "../data/out_vsnew.mp4"
Flags:
Different optional flags can be used to change the way the output is rendered:
-bg : To specify the background color of the output ascii video =>
-bg=0:black, -bg=1:white(default).
-s : To specify whether the video should be skechified before asciifying =>
-s=1:skechify+asciify, -s=0:just asciify(default).
-gs : To specify(if skechified)whether the skechified video should be grayscale
(pencil sketch like) or colored=>
-gs=1:grayscale, -gs=0:color(default).
-rs : To resize the output to match the resolution of the input=>
-rs=1:resize to match source, -rs=0 no resize(default).
Here "../data/inp_v1.mp4", "../data/out.mp4" are just the relative paths of the input and output files.
Any video input and output can be specified as long as the output is of mp4 format.
run:
python sketchify.py "../data/inp_im1.jpeg" "../data/out_im1.png"
flags:
Different optional flags can be used to change the way the output is rendered:
-a : To specify whether the skechified image should be asciified or not =>
-a=0: Only Skechify, -a=1: skechify+asciify(default).
-gs : To specify(if skechified)whether the skechified video should be grayscale
(pencil sketch like) or colored=>
-gs=1:grayscale, -gs=0:color(default).
-bg : To specify the background color of the output asciified sketch =>
-bg=0:black, -bg=1:white(default).
Here "../data/inp_im1.jpeg" "../data/out_im1.png" are just the relative paths of the input and output files.
Any file can be specified as input and output as long as it is an image file.
run:
python asciifyImg.py "../data/inp_im1.jpeg" "../data/out_im1.png"
flags:
We can use different optional flags to change the way the output is rendered:
-bg : To specify the background color of the output ascii image =>
-bg=0:black, -bg=1:white(default).
-rs : To resize the output to match the resolution of the input=>
-rs=1:resize to match source, -rs=0 no resize(default).
Here "../data/inp_im1.jpeg" "../data/out_im1.png" are just the relative paths of the input and output files.
Any file can be specified as input and output as long as it is an image file.
-
We know that images are stored as pixels, with each pixel containing some data depending on the image scheme(like RGB, HSV).
-
Now to convert our input into an ascii art we have to map each of those pixels with some ASCII characters without losing the color data of the image.
-
Since the ASCII characters are not square like(and pixels are), we will be upscaling them to get more square like to serve our needs.
-
We will limit the width of our image by specifying the number of columns as a constant.
-
Now, we claculate the dimensions(height, width) of each pixel using scale value and the specified number of columns.
-
Then, the dimensions of the ouptput image are calculated using no. of columns, rows and size of each ASCII char after upscaling.
-
We'll create a new RGB output using PIL, and write(using
ImageDraw
) to it from our input image. (Note: OpenCV uses BGR whereas PIL uses RGB, so take care). -
Now, find out the no.of pixels of input image contribute to one pixel of output image, and use the average(can also use weighted means) value of those pixels to give color to the output ASCIIfied pixel.
-
As a final step we'd be fixing the overflows in borders(using
getbox()
) after inverting it and finally save it to the specified location.
-
To skechify an image, we first invert it, and then blur it(using
GaussianBlur
from OpenCV), and reinvert it. -
Now, we merge the original image and the blurred image using bitwise division(
divide
from OpenCV), and write it to a file and save it. -
To make the sketch similar to a pencil sketch we have to first grayscale(using
cvtColor
of OpenCV) the image and then execute the steps 1, 2.
-
We know that videos are nothing but a stream of images.
-
Owing to this fact, if we were to modify each image of the stream we would have modified the video as a whole.
-
So, we need a way to extract these frames from the video, which can be done pretty easily by using
VideoCapture
package of OpenCV -
Using
VideoCapture
package from OpenCV we will access each frame and save it to our temporary folder. -
Now, we will extract the frames asciify/skechify/both them and save them in a
temp
folder. -
Then, we will start reading the images from the
temp
folder and write them to the specified output video using theVideoWriter
package.
- Basic idea of how images are stored.
- Extracting pixels from images and manipulating them to edit the images.
- Extracting frames from videos and manipulating them to edit the videos as a whole.
- An intro on basic features of
openCV
for image and video processing.