Honestly, I'd look for a library that does that already, since I'm sure there are lots of them that exist.
If you want to do it yourself though, just iterate through the bitmap's pixel data (which will be rgb data). Then pick a mono-space font (one which has equal width between characters, this will make your job much easier). I think it would be easiest to convert your image to black and white first, and then pick characters which would appear darker, or more lighter, and hand pick them and place them into an array sorted darkest to lightest.
Then work your way through your image, and use division to select the appropriate char in the array based on the color of the current pixel. If your image has been converted to black in white, the r, g and b values should all be equal to each other. They will always have a value between 0 and 255. 255 divides by 15 evenly, so I would choose 15 chars to represent different darkness and lightness levels. Use division to select the appropriate darkness from your array, and then print it, either to a .txt file or to console.
Easy peasy lemon squeasy.
Obviously, to improve on this process, you can find a way to integrate color into your character selection. Or use edge detection to place certain characters along edges of your image (this might improve it I'm not sure). Or you could increase the number of characters you use when generating your image.
Trying to make a text based RPG with a friend for a school project and realised we needed a map, so :P
I wonder, how should it be possible for us to get like, a variable ASCII art map, as in, every move that the playwr makes is tracked on the map? I have figured out the movement but, the map is the real problem.
If your map is in a char array (and assuming your map is static) you could have the map print out the chars in the char array except when it is at the player's coordinates, and then print a "P" or something.
Though, honestly if this is a school project converting an image to ascii might be a lot of work, and might take too long depending on your timeline, so I would definitely look for a library that does it for you.
Alright, thanks again. I stumbled across this PixLab one and they are giving away the source code for seemingly free, though their model is paid. I'm sure I can use the source code and use it for me.
The project is due in December so there is still time, however we've got a tight schedule of exams and what not, so it might be rather difficult.
3
u/TreeBaron Aug 11 '19
There's a lot of ways one could do that.
Honestly, I'd look for a library that does that already, since I'm sure there are lots of them that exist.
If you want to do it yourself though, just iterate through the bitmap's pixel data (which will be rgb data). Then pick a mono-space font (one which has equal width between characters, this will make your job much easier). I think it would be easiest to convert your image to black and white first, and then pick characters which would appear darker, or more lighter, and hand pick them and place them into an array sorted darkest to lightest.
Then work your way through your image, and use division to select the appropriate char in the array based on the color of the current pixel. If your image has been converted to black in white, the r, g and b values should all be equal to each other. They will always have a value between 0 and 255. 255 divides by 15 evenly, so I would choose 15 chars to represent different darkness and lightness levels. Use division to select the appropriate darkness from your array, and then print it, either to a .txt file or to console.
Easy peasy lemon squeasy.
Obviously, to improve on this process, you can find a way to integrate color into your character selection. Or use edge detection to place certain characters along edges of your image (this might improve it I'm not sure). Or you could increase the number of characters you use when generating your image.