ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

DonationCoder.com Software > N.A.N.Y. 2008

Tim - Image to Text Converter

(1/4) > >>

TPReal:
Tim - Image to Text Converter

The program converts an image (jpg or bmp) to a text, which looks like the image in grayscale. You can choose any monospaced font and any font size you want to format the text with, and also any set of allowed characters. The result can be saved as text and then you can format it correctly in a text editor, or you can save the image of the text already correctly formatted.

The program was written in C++Builder 2006 and might need some dll's and bpl's to run. They might be available for download here: http://etonne.110mb.com/files/libs.zip
Put them in the program directory, or in your system directory.

Tim - Image to Text Converter
Tim - Image to Text Converter

tomos:
wow, looks really beautiful that text  :up:

CodeTRUCKER:
Nice!  Would you mind explaining the algorithm used to determine the density of the characters?  That is if it is not proprietary. :-[

TPReal:
I'm glad you like it :)

Character density algorithmCalculating the density of characters was rather easy in Borland's IDE. Let im be an image, then:

--- Code: C++ ---im->Canvas->Font=font_you_use_for_conversion;int width=im->Canvas->TextWidth('x'),height=im->Canvas->TextHeight('x');Now we have the size of a character. The font is monospaced so I can use 'x' or any other character. Now for ch being each character in turn, I clear the image, then print the character on it:

--- Code: C++ ---im->Canvas->TextOutA(10,10,ch);int counter=0;Then in loop where i changes from, say, 0 to width+10 and j from 0 to height+10:

--- Code: C++ ---counter+=im->Canvas->Pixels[i][j]&0x0000FF;Finally, the density of ink for this character is:

--- Code: C++ ---1-(double)counter/255/width/heightNote that the area that you check inside the (i;j) loop must be greater than the character width*height rectangle because a lot of characters exceed this size. The size is used only to determine the horizontal and vertical distance between characters.

Note also that just counting black pixels in the loop is not a good idea, because some fonts, especially small sized (like 2 or 3) include also some gray pixels which also have to be counted in.

Finally I cut the input image into width*height rectangles and for each of them I calculate the density of ink in a similar way as I did it for characters (only now I have to take into account all colors, and also the color weights) and for each rectangle I use the character that fits best.

This is not the actual code of the program, but I think it is more or less correct and shows the main idea :)

CodeTRUCKER:
Thank you for the explanation.  I learned something! :Thmbsup:

Navigation

[0] Message Index

[#] Next page

Go to full version