V
V
Vlad_Ilnitskiy2014-02-03 22:45:31
C++ / C#
Vlad_Ilnitskiy, 2014-02-03 22:45:31

How to convert an image to text format?

It is necessary to write a program that receives an image file of the BMP type as input and converts it to a text format. In text format, display white as a space and black as an '8'. How can this be implemented? Convert to black and white first, then render it as a matrix? I would like to see at least small pieces of code, because I don’t understand how it all will look like.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
J
JastAir, 2014-02-04
@Vlad_Ilnitskiy

The very idea of ​​converting an image to monochrome (as I understand it, you initially need it), it's something like this:
I'm writing in c # Since I'm not very familiar with ++.

Color _pixelColor = img.GetPixel(x, y);
int _MonoNum = (_pixelColor.R + _pixelColor.G + _pixelColor.B)/3;
if (MonoNum<128) //if black color
{
matrix[x, y] = Convert.ToInt32("1"); 
}
else //white color
{
matrix[x, y] = Convert.ToInt32("0");
}

And don't forget that it's all in cycles:
for (int x = 0; x < GlobalConst.width; x++)
for (int y = 0; y < GlobalConst.height; y++)

Approximately such an essence, and then you already write the matrix to a file, the work is essentially for 15 minutes. I also advise you to look at the GetPixel method on MSDN, everything is written about it, and you need to use it, unless of course you have to work with a high resolution.

A
avalak, 2014-02-03
@avalak

Judging by the description, you need a library for ascii graphics.
aalib
writing html5 generator ascii art

S
Sergey, 2014-02-04
Protko @Fesor

Everything is pretty simple. We take a picture, convert it to grayscale , split it into a grid (say 10*10 pixels), calculate the average brightness value for this piece and determine if the brightness is above the threshold value (for example 128) then this is a gap, if it is lower or equal, then our symbol.

A
Alexey, 2014-02-03
@ScorpLeX

Did you copy part of the text of the order?
In fact, the image is a matrix of pixels.
You can convert to black and white by determining the color of the pixel, if more than 8000000 (not exactly) it means white, if less then black.
We pass through the matrix, determining the color and writing along the way, that's all.
Google for the phrase ascii art generator

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question