Python to Convert Image to ASCII Symbols with Source Code

In this article, you’ll learn how to convert image to an ASCII symbols.

Prerequisites:

  1. Python Basics
  2. pywhatkit module

What is ASCII?

ASCII, acronym of American Standard Code for Information Interchange.

ASCII is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices.

Install Necessary Modules:

Open your Anaconda Prompt and type and run the following command (individually):

pip install pywhatkit
  • pywhatkit is a Python library with various helpful features. It is an easy-to-use library that does not require you to do some additional setup.
  • This module has lots of other cool features as well. Feel free and go-ahead to explore them or if you wish I can write an article about them.

Once Installed now we can import it inside our python code.

Source Code:

'''
Python Program to Convert Image to ASCII
'''

# Import the necessary module!
import pywhatkit as kt

###############################################
# Method 1:

# Display welcome msg
print("Let's convert image to ASCII symbols!")

# Capture source and target path
source_path = "rose.png"    # source location
target_path = "ascii_img"   # store location

# Call the 'image_to_ascii_art' method
kt.image_to_ascii_art(source_path, target_path)

###############################################
# Method 2:

# Import pywhatkit as kt
# kt.image_to_ascii_art("rose.png", "ascii_img")

Output:

Let's convert image to ASCII symbols! '................................................................................\n..........................................::!!!!::::............................\n.......................................:*%@@@&&&&&&@$!:.........................\n..............................:::::!*%$@&@@@@&&&#####&@*........................\n...........................:*$@&#####&&@@@@@@&&&#####&&@%:......................\n.........................:$&###SSS#&&&&&@&#&&&###&&###&&&%:.....................\n.........................!####SS#&&####@&#S#&&####&&&&&&&&$*!:..................\n....................::!%$#S#@#SS#&#SSS&&SSSSS##SS#&#&&&&&&@&&@*:................\n.................:*@#SS***SS&#SSS&#SS#&SSSSSSSSSS#&##&&&&&@&&&&@*:..............\n.................!S*******SS#&SSS#&#S#SS#@##SSSSS#S###@&&&&&&&&&@:..............\n.................!SSSS*****SS##SS###&#*S#&###S#SSS#$##&&&&&&&&&@$...............\n.................!#SSSS****SS##&#S####SS###@@@##&@@&S#&&@&&&&&&@*...............\n..................@S#SSS****SS##&@#####&&&&&&&&@&&#S##&&&&&&&&@*................\n..................!#####SS**SSS##&@&#S######&&&##SS#&&&#&&&&@%!.................\n...................!&S####S**SSSSS#&@&#S######&&@$$$@&##&&@$!...................\n.....................*&######S*SSSSSS#&&&##&@@$%%%$@&&&&@$*..........::.........\n.......................!*%*@&&#SSSSSSSS#&&&&&@$%$@&&&@$%***:.......:!:..........\n...........................@#&&&#SSSSSSSS#&@@@$@&&&@%******:.....:!!:...........\n...........................*SS&&&&#SSSSSS###&@@&&&$********:..:!**:.............\n............................&*S##&&&#SSSS#####&&&%***********%$%!...............\n............................!#*SSS#&@&#SS#####&&&@%*******$@@%!.................\n.............................!#SSSSS#&&&######&&&&@%*****$$*:...................\n.......................::::...:@SSSSSS##&###S#$$$@@%*%%%%*:.....................\n.......................:::!!!*%$&#SS######&##S@$&@%%%*!:........................\n...........................:!*%$@&&#&&&&@$&##SS&@*$*:...........................\n...............................::*%%%%*!:.$###@%!:..............................\n..........................................!!&$:.................................\n............................................$@!.................................\n............................................*&!.................................\n................:*%%%*!!:...................*&*:................................\n................::*@@&@@$%**::..............*&!.................................\n....................:!%$$$$@$%*!!::.........*&!.................................\n........................::$###&@$%%%*:......*&!.................................\n.........................!&####&&@%%*!:.....*@:.................................\n...........................:!*%$%%!::**:....%@:.................................\n...........................!*%%%%%***!!!::::$$..................................\n.........................*@&&&&@@@$%!:.::!::@$!::::!:::.........................\n........................*S##&&&@$*:.......::&$:......:!:........................\n........................%$%%%*!:...........:&%........:!:.......................\n...........................................:@*..........*!......................\n...........................................!@:.........:$$:.....................\n...........................................*$..........:!:......................\n.......................................::::$%::!%%*!!::.........................\n...............................::!!!****%$$@@$%%$$%%%%**:.......................\n..............................:!!::::::$@@@@@@@%.....:::!.......................\n...........::::::::...................@S##&@&&&@!...............................\n...........*&&&&&&&@$%*::.............&###&&#&&&%...............................\n...........:$&&&&#######&%:...........!###&###&&$:..............................\n.............*@&&&&&&###SS%............!&######@!...............................\n..........:!%@&&&&#&&&&&@&*!:::::.......:$###&%:................................\n.........!&####&@&&##@%%%$@&&@$$$$%*!....%@!!:..................................\n........:#S#####&###$**%$%$@@@$%$$%$$...:@*.....................................\n........*SS##S#S##@!.*@$%**%%%%*%%%$%!:.*&:.....................................\n........:SSSSS#&$!...:$&@%%***!!**$$%*!*@%......................................\n.........*@$%*!........*@&@$*:..:!%%*:.*&!......................................\n........................:!::.......::..:&!......................................\n........................................@*......................................\n........................................%$......................................'
Code language: PHP (php)
pip install opencv

Note: you may need to restart the kernel to use updated packages.
ERROR: Could not find a version that satisfies the requirement opencv
ERROR: No matching distribution found for opencv
# install
# pip install opencv-python

import cv2
image = cv2.imread("rose.png")
cv2.imshow("rose", image)
cv2.waitKey(0)
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow("rose", gray_image)
cv2.waitKey(0)
inverted_image = 255 - gray_image
cv2.imshow("rose Inverted", inverted_image)
cv2.waitKey()
blurred = cv2.GaussianBlur(inverted_image, (21, 21), 0)
inverted_blurred = 255 - blurred
pencil_sketch = cv2.divide(gray_image, inverted_blurred, scale=256.0)
cv2.imshow("rose Sketch", pencil_sketch)
cv2.waitKey(0)
cv2.imshow("rose original image", image)
cv2.imshow("rose pencil sketch", pencil_sketch)
cv2.waitKey(0)

Output:

................................................................................ ..........................................::!!!!::::............................ .......................................:*%@@@&&&&&&@$!:......................... ..............................:::::!*%$@&@@@@&&&#####&@*........................ ...........................:*$@&#####&&@@@@@@&&&#####&&@%:...................... .........................:$&###SSS#&&&&&@&#&&&###&&###&&&%:..................... .........................!####SS#&&####@&#S#&&####&&&&&&&&$*!:.................. ....................::!%$#S#@#SS#&#SSS&&SSSSS##SS#&#&&&&&&@&&@*:................ .................:*@#SS***SS&#SSS&#SS#&SSSSSSSSSS#&##&&&&&@&&&&@*:.............. .................!S*******SS#&SSS#&#S#SS#@##SSSSS#S###@&&&&&&&&&@:.............. .................!SSSS*****SS##SS###&#*S#&###S#SSS#$##&&&&&&&&&@$............... .................!#SSSS****SS##&#S####SS###@@@##&@@&S#&&@&&&&&&@*............... ..................@S#SSS****SS##&@#####&&&&&&&&@&&#S##&&&&&&&&@*................ ..................!#####SS**SSS##&@&#S######&&&##SS#&&&#&&&&@%!................. ...................!&S####S**SSSSS#&@&#S######&&@$$$@&##&&@$!................... .....................*&######S*SSSSSS#&&&##&@@$%%%$@&&&&@$*..........::......... .......................!*%*@&&#SSSSSSSS#&&&&&@$%$@&&&@$%***:.......:!:.......... ...........................@#&&&#SSSSSSSS#&@@@$@&&&@%******:.....:!!:........... ...........................*SS&&&&#SSSSSS###&@@&&&$********:..:!**:............. ............................&*S##&&&#SSSS#####&&&%***********%$%!............... ............................!#*SSS#&@&#SS#####&&&@%*******$@@%!................. .............................!#SSSSS#&&&######&&&&@%*****$$*:................... .......................::::...:@SSSSSS##&###S#$$$@@%*%%%%*:..................... .......................:::!!!*%$&#SS######&##S@$&@%%%*!:........................ ...........................:!*%$@&&#&&&&@$&##SS&@*$*:........................... ...............................::*%%%%*!:.$###@%!:.............................. ..........................................!!&$:................................. ............................................$@!................................. ............................................*&!................................. ................:*%%%*!!:...................*&*:................................ ................::*@@&@@$%**::..............*&!................................. ....................:!%$$$$@$%*!!::.........*&!................................. ........................::$###&@$%%%*:......*&!................................. .........................!&####&&@%%*!:.....*@:................................. ...........................:!*%$%%!::**:....%@:................................. ...........................!*%%%%%***!!!::::$$.................................. .........................*@&&&&@@@$%!:.::!::@$!::::!:::......................... ........................*S##&&&@$*:.......::&$:......:!:........................ ........................%$%%%*!:...........:&%........:!:....................... ...........................................:@*..........*!...................... ...........................................!@:.........:$$:..................... ...........................................*$..........:!:...................... .......................................::::$%::!%%*!!::......................... ...............................::!!!****%$$@@$%%$$%%%%**:....................... ..............................:!!::::::$@@@@@@@%.....:::!....................... ...........::::::::...................@S##&@&&&@!............................... ...........*&&&&&&&@$%*::.............&###&&#&&&%............................... ...........:$&&&&#######&%:...........!###&###&&$:.............................. .............*@&&&&&&###SS%............!&######@!............................... ..........:!%@&&&&#&&&&&@&*!:::::.......:$###&%:................................ .........!&####&@&&##@%%%$@&&@$$$$%*!....%@!!:.................................. ........:#S#####&###$**%$%$@@@$%$$%$$...:@*..................................... ........*SS##S#S##@!.*@$%**%%%%*%%%$%!:.*&:..................................... ........:SSSSS#&$!...:$&@%%***!!**$$%*!*@%...................................... .........*@$%*!........*@&@$*:..:!%%*:.*&!...................................... ........................:!::.......::..:&!...................................... ........................................@*...................................... ........................................%$......................................
Code language: PHP (php)

Leave a Comment