dom111 / image-to-ansi

Convert an image to ANSI colour codes using JavaScript.
https://dom111.github.io/image-to-ansi
MIT License
68 stars 17 forks source link

Option to output to binary file? #4

Open SpinyUK opened 1 year ago

SpinyUK commented 1 year ago

Hi, I dabble in ANSI art using PabloDraw - is there a way to convert the escape code output to something I can save as a *.ans file in windows (or linux etc) that I can open in PabloDraw? It would save me many hours in image prep :)

I can 'print' in terminal and the output looks great, but if I pipe it to a file, it's not something PabloDraw can parse.

cheers, Phil.

dom111 commented 1 year ago

Hey @SpinyUK,

Thanks for your interest in this! I wonder if this is because of the unicode characters and ANSI art typically being in CP437? Do you see strings like: Γûä in PabloDraw?

If that's the case, replacing the unicode lower block character with \xDC (as per this page: http://www.unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT) might work. Something like:

ansi-image.sh|perl -mutf8 -0pe 's/▄/\xDC/g'

If this works (and is useful!), I'd be happy to look at making it an option for output, although I can't promise when I'd get around to it!

Thanks,

Dom

SpinyUK commented 1 year ago

Well, it's kind of working :)

yes, I see a lot of 'Γûä' when there are half blocks, converting them didn't seem to help, it just replaced them with the text DC, so I think pablodraw is reading them as text and not escaped.

but with normal full height blcks, I can see that pablodraw can show blacks with colour #0 but not any others , eg the screenshot below is three screenshots together, the top being Notepad++ showing the escaped codes - I'm guessing here (i'm a gfx guy not a coder :) ) the 0m is referring to col#0 and 15 to col#15 (white in this case) but the other colours on the subsequent lines aren't showing in Pablodraw, or aren't showing correctly at least :) https://i.imgur.com/r2NyNqx.png

If I work backwards and save an example ANSI then view it, I can see that only the colours seem to get escaped, and the blocks are extended ASCII, maybe:

https://i.imgur.com/ovBnJyy.png

So I reckon Pablodraw (and Moebius, another editior) are using a different way to encode.

I'll see if I can find out which escape codes are equivalent to which characters - eg the second imgur link shows a full height block is an odd looking 'pi' character, if I make a list I could rexex the output perhaps - an exercise for me, the reader :)

dom111 commented 1 year ago

Interesting... Of note here is that both those apps appear to use only 16 colours which makes sense, this app uses the extended ANSI colouring (in 16 colour mode the colours 30-37 and 90-97 are the standard and bright colours shown, in my output 38 enables 256 colour mode and the other parameters pick the colour from the palette, I also make use of the background colours to get two vertical pixels in one character space in the terminal).

I think I did make it possible to scale the colours down to 16 originally, but I've since removed (or at least hidden) that functionality, so I'm not sure this would end up being much help at all for you in its current state :(

If you're a programmer yourself you might find my code a useful example to help convert images to your preferred ANSI format, but also feel free to keep this open and if I have the time and inclination I might have a go myself!

SpinyUK commented 1 year ago

cheers for the help, I'll see if I can work out a way to convert your output to something PabloDraw likes, now I know how you're 'rendering' the images. It makes sense to just change the BG Colour for 'pixels' but when I draw, thats the opposite of how my workflow goes :D The fact it renders perfectly in a terminal ( I tested in WSL Ubuntu on win10) shows the data is there, just need to tweak it :) This is the ort of thing I normaly do - all done with the arrow keys and F1-F4 in Pablodraw :D so being able to pre draw some of this and export it, would save tons of time :) https://www.spiny.org/torment/ansi.html cheers, Phil.