2

I am working on transforming an image into a set of emojis, depending on how many colors are there. The Maths part is done. I have the matrix of numbers from 0 to 30, but I specifically need to convert the numbers into symbols and I was thinking about emojis since they are so used nowadays.

My question is how am I supposed to read a matrix of integers from a file, transform the matrix of integers into a matrix with different emojis (eventually, from a list of my choice) and put the output in another text file, this time containing the emojis? Is that possible? I guess it should be, but how do I do that? Does anyone have any suggestions?

The problem that I face is actually with the emojis unicode, I don't seem to have success when it comes to receiving messages on the console in their case. I just get "? ?" instead of a smiley face. But that thing happens only for them, the ASCII characters seem to work a bit better. The problem with ASCII characters is that I need, again, expressive images instead of numbers or random pipe shapes.

There is the code:

%make sure you have the "1234567.jpg" in the same location as the .m file
imdata = imread('1234567.jpg');
[X_no_dither,map] = rgb2ind(imdata,30,'nodither');
imshow(X_no_dither,map)

% and there I try to put the output in a text file
dlmwrite('result.txt',X_no_dither,'delimiter',"\t");

Ok, and the output in the text file is:

0 0 0 0 26 26 ... etc.

And I wonder how am I supposed to write the code in such a way that I will get emojis instead of numbers.

... etc.

That's how I'd want the output to be like. But, from what I tried yesterday, I cannot print them without getting warnings/errors.

RidiX
  • 819
  • 6
  • 9

1 Answers1

1

What you need to do is create a table with your 30 emojis (this documentation page might be helpful), then index into that table. I'm using the compose function as indicated in the page above, it should also be possible to copy-paste emojis into your M-file. If you don't see the emojis in MATLAB's console, change the font you're using.

For example:

table = [compose("\xD83D\xDE0E"),
         "B",  % find the utf16 encoding for your emojis, or copy-paste them in
         "C",
         "D",
         ...
        ];
output = table(X_no_dither + 1);
f = fopen('result.txt', 'wt');
for ii = 1:size(output, 1)
    fprintf(f, '%s', output(ii, :));
    fprintf(f, '\n');
end
fclose(f);

This will write the file out in UTF16 format, which is what MATLAB uses. If you're on Windows this might work well for you. On other platforms you might want to save as UTF8 instead, which can be accomplished by opening the file in UTF8 mode:

f = fopen('result.txt', 'wt', 'native', 'UTF-8');

Note that, even if you don't manage to get the emojis shown in the MATLAB command window, opening the text file in an editor will show the emojis correctly.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120