

This is where the most important work is done. for i in range(h):ĭraw.text((j*charWidth,i*charHeight),getSomeChar(grey), You can make your own char list if you want. This function getSomeChar will get pixel value as a parameter that ranges between 0–256 and it returns respective ASCII character from the chars list. After that, a canvas ‘outputImage’ of black color is created using Image function and ImageDraw object is created to embed ASCII characters on the canvas. Now the font for ASCII characters is decided. OutputImage = Image.new('RGB',(charWidth*w,charHeight*h),color=(0,0,0)) Doing so is very important otherwise more computational power will be used and the output images will be too big. Here Firstly the image is loaded then the scaling factor is chosen (0.1–1) and the size of each ASCII character in the final output image is decided then the width and height of the image are taken and the image is resized. Image = image.resize((int(scaleFac*w),int(scaleFac*h*(charWidth/charHeight))),Image.NEAREST) Import these libraries first image = Image.open("InputImage.jpg") Save the newly generated image and you are done!!!Ĭode: from PIL import Image,ImageDraw,ImageFont.Now that character is printed on the new canvas of given size with ImageDraw function(in the function itself you can choose the colors to be RGB or B&W).Get relevant character for each pixel from the character list(according to the pixel value, the character from the list is chosen i.e.Convert the image into a greyscale image.Scale the images otherwise very big output image will be generated.
Image to ascii art code#
Firstly I will explain all the steps then we will proceed to the code snippets.
Image to ascii art install#
You can simply install pillow with pip install pillow. You must have python installed on your system along with pillow python package. This project will give you a solid foundation in understanding the structure of images and playing around it and that is in a fun way!! So, Let's get started. In this article, we will use Python to convert any given image to an artistic ASCII colored image in less than 35 lines of code.
