memegasil.blogg.se

Python ascii art
Python ascii art











python ascii art
  1. #PYTHON ASCII ART INSTALL#
  2. #PYTHON ASCII ART CODE#
python ascii art

Now, in a draw.text it takes the position of embedding character as the first argument and character itself that is being chosen with getSomeChar function as the second argument, a font that we have chosen earlier as the third and fill that is color as the fourth argument. We loop through each pixel and gathered RGB components and the mean of them gives us the grey pixel value. This is where the most important work is done. for i in range(h): for j in range(w): r,g,b = pixels grey = int((r/3+g/3+b/3)) pixels = (grey,grey,grey) draw.text((j*charWidth,i*charHeight),getSomeChar(grey), font=font,fill = (r,g,b)) You can make your own char list if you want.

python ascii art

This function getSomeChar will get pixel value as a parameter that ranges between 0–256 and it returns the respective ASCII character from the chars list. " charArr = list(chars) l = len(charArr) mul = l/256 return charArr 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. font = uetype( 'C:\\Windows\\Fonts\\lucon.ttf',15) outputImage = Image.new( 'RGB',(charWidth*w,charHeight*h),color=(0,0,0)) draw = ImageDraw.Draw(outputImage) 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. Import these libraries first image = Image.open("InputImage.jpg") scaleFac = 0.8 charWidth = 10 charHeight = 18 w,h = image.size image = image.resize((int(scaleFac*w),int(scaleFac*h*(charWidth/charHeight))),Image.NEAREST) w,h = image.size pixels = image.load() Save the newly generated image and you are done!!!Ĭode: from PIL import Image,ImageDraw,ImageFont import math.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.

#PYTHON ASCII ART CODE#

Firstly I will explain all the steps then we will proceed to the code snippets.

#PYTHON 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.













Python ascii art