How to convert RGBA pixels to characters?

Asked

Viewed 602 times

0

having access to the pixels of any image through the SFML library, my goal is captured and convert them to characters by placing in a new matrix (image) and then display it. For that I checked the size of the image:

sf::Vector2u tam = imagem.getSize();
int largura = tam.x;
int altura  = tam.y;

To capture the RGB format, use the following code:

// O ponteiro dos pixel originais em RGBA.
const sf::Uint8* pixels = imagem.getPixelsPtr();

Pixel* rgba = (Pixel*) pixels;
// Vetor que armazenará os pixels.
sf::Uint8* saida = new sf::Uint8[4 * largura * altura];

int tamanho = 4 * largura * altura;

for (int i = 0; i < tamanho; i++)
{
saida[i] = pixels[i];
}

What I’m doing is creating a vector that is receiving the RGBA, but still no conversion to characters, and that’s one of my doubts as is a vector as I will control when it must break the line to form the correct image?
Wouldn’t it be better to do it with a matrix?
My other question is how do I convert these pixels into characters?

I tried that way, but without success:

for (int i = 0; i < tamanho; i++)
{
if (atriz[i][j]>=9,4*8)
saida[i] = '@';
}
  • Hi! Still not able to solve that question? Did you consult the links about ASCII art? They were useful? Link 1, Link 2 and Link 3

  • You can even use an array, but it is not necessary. You can use the hint given in a reply to your other question: pixels[4 * (y * largura + x) + 0] returns the pixel R component in the coordinate (x, y). + 1 gives you back G, + 2 to B, and + 3 component A.

  • There is an error in your second code. Despite the code if (atriz[i][j]>=9,4*8) compile correctly in C/C++, I don’t believe it is producing the result you expect. What you meant by 9,4*8?

  • Hello @carlosrafaelgn all right?! I finally managed to do what I wanted to hehe :D, I was doing the calculations of wrong conversions, when getting ready put the open source code. att

  • 1

    Cool! It’ll get interesting! :)

1 answer

1

If you want a ratio of one character per pixel, you must break the line each x characters, where x is the width of the image. Consider inserting a line break in the multiple indexes of x. Or, as you say yourself, use a matrix.

To convert a pixel directly into a character - in this case, you can end up getting bizarre images that look nothing like the original, depending on the details. As far as I know, programs that transform an animated video or GIF into an animated ASCII art use form analysis to transform image regions into specific sets of characters, i.e.: a black pixel surrounded on the left and on top by three white pixels (using 0 pair span and 1 to black because I couldn’t draw with HTML):

00
01

It can turn into something like:

|---|
|-||||

Or

/--|
|-|||

Depending on the filter used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.