/Image-to-ASCII-art-converter

Converts an image of any type to an ASCII art representation of the same image.

Primary LanguagePythonApache License 2.0Apache-2.0

What is 2ASCIIpy?

2ASCIIpy is a python script made by yours truly that converts an image of any type (.png, .jpg, .gif, etc) to an ASCII representation of that image. It works with both grayscale, and colored images (Although, the output will allways be black and white. Duh.).

For example, if we input the following image:

Pikachu Image

We get:


...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
.......................:%%;...........................,$%%*................
.......................+#%£*:.......................;**%##$................
.......................+#$%#%:.....................:£#%$##$................
.......................+#$$%##,...................,$%$$$##£................
.......................+#$$$%#$;.................,$:*$$##*,................
.......................+##%*:+$;................;£:;+%###*.................
.......................;*#$+;;*+;..............;+*;;:£##£;.................
........................,%*;;;;£+..............+£;;;,+##;..................
........................,%:;;;;£+,............,+*;;;;+#%;..................
........................,%+;;;;:**............*+;;;;;*#;...................
........;££,.............;$*;;;;**.*££££££:...£*;;;;*$;....................
.......;++++;.............$£;;;+££;++++++*::+++:;;;*£+.....................
......;++,;*+;............*£+;:+*+:,,,,,,,:+*£;;;;+£:......................
......£+,;;,+*,............+£:£;,,,,,,,,,;;;;:;;;:$;.......................
.....,£+;;;;;:+,...........,:£:;,,,,,,,;;;;;;;;;+$,........................
.....*:;;;;;;;:+,..........:**#+;;;;;;;;:%#$;;;+$%,........................
...;++;;;;;;;;;::,.........+$$*$+;;;;;;;**%#*;;:£%,........................
...:$;;;;;;;;;;;::;.......;+$@+#£,;;;;;;%+£@#;;;*$,........................
..,:£;;;;;;;;;;;;+*,......£++#@#*;;;;;;;#@#%%;;;+£,........................
..:*;;;;;;;;;;;;;;+£,.....$*;+%*;+%+;;;;:%#$:;::+*$;.......................
..+*;;;;;;;;;;;;;;:$,.....%£:;:;;;:;;;;;;:::;+£*:+@:.......................
..*$,,;;;;;;;;;;;;:*+,....%$:;;:+***++:;;;;;+*$£+:@:.......................
..+$**;;;;;;;;;;;;::£+,...%$+;;:*$##$$+;;;;:£*$%£:@:.......................
...+@@+;;;;;;;;;;;:::£+,..$$+;;;;*#$$*;;;;;:*£$$£:@:.......................
...,,,#@$;;;;;;;::::::**..,*$;;;;+$£$*;;;;;:£$$$£:@:.......................
......;:*##:,;;::::::;$%...*$,;;;;:*+:;;;;;;+$%£+:%;.......................
........;££££*;::::::££+...:**;;;;;::;;;;;;::**+::£*;......................
...........$%$**::::*@£.....:#;;;;;;;;;;;;::::::::+%*......................
.............+@@+:::%@*.....:%;;;;;;;;;;:::::::::::#£......................
.............,,,£*::%:,.....:$;;;::::::::::::::::::#£......................
..............,£+:*#:,.....:*:;;;;::::::::::::+:;::#£......................
..............,%+:*%£,.....+£;;;;;;;:::::::::+:;;;:#£......................
.............,+£+::+#£;....+$;;;;;;;:+:::::::*;;;;:#£......................
.............+#+::::+#£,..,+£;;;;;;;;:+:::::+£:;;;:#£......................
.............+@%*::++*££..$*;;:;;;;;;+£::::+£:;;;:+@£......................
.............,::$%£+++$$..$*,;+:;;;;;:£#::*£:;;;::£@£......................
................++£$*£*:.:*:;;:*+;;;;;:££:*£+:;;+£*%+......................
..................+@£#+,+*;;;;;+*+:;;;;+£++£%:**££:*+;.....................
.................,*@$@*:%;;;;;;,+$£+;;*@:::*@+#@$::+££.....................
.................*#$$$$$£;;;;;;;;;:£$#%::::*@@*:::::$%.....................
.................£@£$$%£;;;;;;;;;;;::++:::::++::::::$%.....................
.................:$##$%$,;;;;;;;;;;;;;:::::::::::::;$%.....................
..................:%%%#%;;;;;;;;;;;::::::::::::::::*%%.....................
.....................%@$;;;;;;;;;;;::::::::::::::::#@$.....................
.....................,*$:;;;;;;;;;:::::::::::::::::#£,.....................
......................,+$:;;;;;:::::+++++++:::::::#*;......................
.......................+@+;;;;;:::::+++++++++:::*$£,.......................
.......................:%$+::::::+*£££££****+++*%#,........................
........................,#£+:::::*#@@@#$£$$£++£%#$,........................
.........................,#%*++££$%;,,,,,#%+++:::+$;.......................
........................,£*+;;:+$$;......;£##%*:+£+$+......................
.......................;+*:;;;£$£:........:£**£$%#$*;......................
.......................+#::::£@#+.............*$%%%;.......................
.......................+@*$££@@:...........................................
.......................,;#@@%,,............................................
.........................:::;..............................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................
...........................................................................



How to use 2ASCIIpy

To use 2ASCIIpy, you will need to use command line arguments, as follow:


python main.py --file <PATH TO THE IMAGE> --scale <SCALE TO WHICH RESIZE THE IMAGE BY> --output <NAME/PATH TO THE OUTPUT FILE>

Example:

Example image


  • --file = The path to the desired image

  • --scale = the scale to which divide the width and the height for when resizing

  • --output = Name and path to the text file containing the output of the program

  • --print (true / false) = Optional parameter to decide wether to print the output to the console or not


Yeah, but how does it work?

  • Resizes your image, by dividing the width and the height by the given scale

  • The script first creates a list of characters to represent the brightness of the pixel, going from darkest to brightest ( ['@', '#', '%', '$', '£', '*', '+', ':', ';', ',', '.'] ).

  • Converts the image to grayscale, so all pixels turn into a number from 0 to 255.

  • Then loops trough all pixels of the image, divides the pixel number by 25, thus converting the value from 0 to 255 to (approximately) 0 to 10.

  • Them it uses that number to index the characters list, and picks a character for the output.


Future plans

  • Add an optional GUI

Support

You can support this project by reporting bugs 🐞, suggesting fixes 🔧 and new features ⚙, and ways to optimize the code 💻. Feel free to use the code in your projects, as long as propper credit is given.

And of course, if you liked this project, please consider giving it a star ⭐.