How Did the Transition From Ascii Art to Images Happen

Images To ASCII Art

In this tutorial, nosotros volition larn how to catechumen any image to ASCII art using the Python programming language. I'm sure you have heard of ASCII art which is a graphic blueprint technique that uses printable ASCII characters to display images. Await at the image beneath for an example.

ASCII Art Illustration
ASCII Art Analogy

Now that nosotros are clear with what we aim to practice by the end of this tutorial. Let'due south not waste any more time and begin the lawmaking implementation.

Creating ASCII Art From An Paradigm Using Python

In this department, you will learn how to generate ASCII art from an image using Python.

Loading an Prototype

The starting time and foremost step is to load the image into our program using the PIL library. We will make use of the exception handling to make certain nosotros handle errors beforehand. Nosotros will use a flag variable to know if the image is in the arrangement or not.

Recommended Read: Python Exception Handling – Python endeavour-except

import PIL.Epitome  img_flag = True path = input("Enter the path to the epitome field : \northward")  try:   img = PIL.Image.open(path)   img_flag = True except:   print(path, "Unable to observe image ")                

Resize The Image

Nosotros need to resize the image to a smaller width and height so that information technology doesn't end upwards having too large text and create a mess.

width, height = img.size aspect_ratio = height/width new_width = 120 new_height = aspect_ratio * new_width * 0.55 img = img.resize((new_width, int(new_height)))                

Convert Image to Grayscale

We tin utilise thecatechumen office and pass the selection asL for GreyScale image output.

Create an ASCII Character List

Remember that the ASCII characters are bundled from darkest to lightest, which means for the list shown below the darkest pixel will exist replaced with@ and lightest with.. You tin can modify the list according to your preference.

chars = ["@", "J", "D", "%", "*", "P", "+", "Y", "$", ",", "."]                

Convert to ASCI Fine art

To convert the image to ASCII character, we get the pixel value for each pixel in the paradigm and map the respective ASCII character together to class a new cord. Now, we useto_greyscale function to catechumen our image to aGreyScale image andpixel_to_ascii function to convert our image to ASCII art! We will also relieve the resulting text into a file.

pixels = img.getdata() new_pixels = [chars[pixel//25] for pixel in pixels] new_pixels = ''.join(new_pixels)  # split up cord of chars into multiple strings of length equal to new width and create a list new_pixels_count = len(new_pixels) ascii_image = [new_pixels[alphabetize:index + new_width] for index in range(0, new_pixels_count, new_width)] ascii_image = "\north".join(ascii_image) print(ascii_image)  # write to a text file. with open("sample_ascii_image.txt", "due west") as f:  f.write(ascii_image)                

The Complete Code

Allow'southward accept a look at the complete code that we just coded in the previous section.

import PIL.Image  img_flag = True path = input("Enter the path to the image field : \n")  try:   img = PIL.Image.open up(path)   img_flag = True except:   print(path, "Unable to discover image ");  width, height = img.size aspect_ratio = height/width new_width = 120 new_height = aspect_ratio * new_width * 0.55 img = img.resize((new_width, int(new_height)))  img = img.catechumen('L')  chars = ["@", "J", "D", "%", "*", "P", "+", "Y", "$", ",", "."]  pixels = img.getdata() new_pixels = [chars[pixel//25] for pixel in pixels] new_pixels = ''.join(new_pixels) new_pixels_count = len(new_pixels) ascii_image = [new_pixels[index:alphabetize + new_width] for index in range(0, new_pixels_count, new_width)] ascii_image = "\due north".bring together(ascii_image)  with open("ascii_image.txt", "w") every bit f:  f.write(ascii_image)                

Some Sample Outputs

ASCII Art Sample Output 1
ASCII Art Sample Output i
ASCII Art Output 2
ASCII Fine art Output 2

Decision

Go ahead and try this exercise out with a lot of dissimilar characters and see the results for yourself. You might notice some really interesting results as well! Let us know in the comments beneath for which one worked best for you.

chewancenum.blogspot.com

Source: https://www.journaldev.com/60400/image-to-ascii-art-using-python

0 Response to "How Did the Transition From Ascii Art to Images Happen"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel