Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Unlock the full potential of AI with Building LLMs for Productionβ€”our 470+ page guide to mastering LLMs with practical projects and expert insights!

Publication

Convert Images to ASCII Art Images Using Python
Latest   Machine Learning

Convert Images to ASCII Art Images Using Python

Last Updated on July 24, 2023 by Editorial Team

Author(s): Pushkara Sharma

Originally published on Towards AI.

Programming

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.

Original Image on the left and Final ASCII Image on right

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.

Also check this article out on Automatic Movie Review System:-

Automatic Movie Review System Using Sentimental Analysis For Positive Or Negative Review

In this article, we will use machine learning to perform sentimental analysis of reviews available on the IMDB website…

towardsdatascience.com

Dependencies Required:

You must have python installed on your system along with pillow python package. You can simply install pillow with pip install pillow. Firstly I will explain all the steps then we will proceed to the code snippets.

Steps:-

  • Open the image from the provided path.
  • Scale the images otherwise very big output image will be generated.
  • Convert the image into a greyscale image.
  • Get relevant character for each pixel from the character list(according to the pixel value, the character from the list is chosen i.e. β€œ_” represents low density).
  • 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).
  • Save the newly generated image and you are done!!!

Code:

from PIL import Image,ImageDraw,ImageFont
import math

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()

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. Doing so is very important otherwise more computational power will be used and the output images will be too big.

font = ImageFont.truetype('C:\\Windows\\Fonts\\lucon.ttf',15)
outputImage = Image.new('RGB',(charWidth*w,charHeight*h),color=(0,0,0))
draw = ImageDraw.Draw(outputImage)

Now the font for ASCII characters is decided. I have used inbuilt fonts of windows. 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.

def getSomeChar(h):
chars = "$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\U+007C()1{}[]?- _+~<>i!lI;:,\"^`'. "[::-1]
charArr = list(chars)
l = len(charArr)
mul = l/256
return charArr[math.floor(h*mul)]

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. You can make your own char list if you want.

for i in range(h):
for j in range(w):
r,g,b = pixels[j,i]
grey = int((r/3+g/3+b/3))
pixels[j,i] = (grey,grey,grey)
draw.text((j*charWidth,i*charHeight),getSomeChar(grey),
font=font,fill = (r,g,b))

This is where the most important work is done. We loop through each pixel and gathered RGB components and the mean of them gives us the grey pixel value. 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.

outputImage.save("name_for_output.png")

Then just save the image…..and share the generated artistic image.

You can play around with font size, font style, scaling factor, canvas background, and characters list.

This is my first blog … so please let me know if you guys like this or not. Suggestions are most welcome.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓