I enjoy competitive programming so I spend a lot of time on Hackerrank solving various problems. One day I tried to solve this problem and it got me thinking about text formatting when running scripts on terminal.
With that problem in my mind and thinking that back-end development didn’t need to be visually uninteresting I created this script. It receives as input the path to a black and white icon on your computer, image width, image height, and it outputs that icon on the terminal using ‘#’ and whitespaces.
How it Works
To load the image I used the PIL library. Then, since I needed to work with the colors of each pixel, I created a 2D numpy array of integers (each number representing the color of that specific pixel). 0 means white and 255 means black.
Then, I iterated through the array to create a new one. If the number on arr[i][j] was 0, the new array would have a whitespace on the ith row and jth column. If the number was not zero, I’d fill the new array with a ‘#’ symbol.
After that, I printed the array generated.
Since this was just a test to learn a bit about image processing I didn’t plan a lot to write this script, but in the future I intend to make a lib out of this to make it useful for everyone and every project, and I’d love some help!
Let’s talk on LinkedIn.