DEV Community

hardik singh
hardik singh

Posted on

5 2

convert images to ASCII characters!

ever wanted to convert your image to Ascii characters? here is how you can achieve it!!

we will be converting this image of Mario, into ascii characters
Image description

step 1:- loading in the image and resizing

Image description
we resize the image since there are a LOT of pixels. so we resize it by 50%! this will make the final output smaller and we will not be compromising on the quality(since there are a lot of pixels to begin with!)

step 2:- converting the image to a pure black and white image

Image description
here we convert the image to a pure black and white image. we do this since text characters don't have color. now we can save this black and white data to a new image
Image description
here is what the image looks after the conversion!

step 3:- converting this black and white data to text characters

Image description
here we read in the data from the fresh black and white image which we saved in the last step. we also have a count variable which helps us to break the line for every time we finish a row section of the image
now we check the color of each of these pixels, if its black(0), we simply write in a "~". on the other hand if its white(255), we write in a "."
we now save this data into a text document!

here is how it looks like finally

Image description
I have resized my code editor so you all can see this masterpiece!

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay