How can I create a (printable) image (jpeg, bitmap, tiff...any format is ok) starting from a binary code?
Show older comments
My goal is to translate a (relatively long) text file into an image file, by converting the text into a binary code (first step) and then using the binary code to create an image (second step). I have completed the first step and I have a big string of binary code, now, which I want to translate in an image. I am ok with the process being messy and unpredictable, as my goal is artistic and conceptual. I am extremely curious to see what image would result from this, and I would be curious to see if there are ways to format the binary code so that it creates a colorful unpredictable image.
I am new to Matlab, but it looks like people have been able to make images from binary code using this software. I have tried to understand how, but with no luck.
I hope someone will be able to help and thanks a lot in advance.
E.
5 Comments
Guillaume
on 13 Jul 2019
Images, text, anything on a computer is just numbers. It's just how you tell the computer to interpret these numbers that make it display as text, image or anything else.
For example the sequence of numbers [65, 122, 33] is simply 'Az!' if interpreted as (ASCII or UTF8) text. It's a greenish kind of colour
, if interpreted as a (8-bit RGB) pixel colour.
, if interpreted as a (8-bit RGB) pixel colour.So, you can trivially convert a stream of numbers, be they represented in binary ot decimal, into an image. The result is not likely to be very interesting if the input is fairly random, unless you come up with a clever algorithm to generate something interesting out of a random input.
You'll have to be clearer as to what you call binary code. Ideally, provide an example or attach your file. Often when people say they've converted something to binary code, they're storing the something as a sequence of of either '0' and '1' characters, or 0 and 1 integers (or worse double), which basically is an inefficient way of storing the same information (using more memory). It's just as easy to convert the text directly into pixels.
It is against the TOS of matlab central to offer anything for money. So, I'm editing your post to remove mention of remuneration. You'll be wasting your money for something as trivial as this anyway.
Guillaume
on 14 Jul 2019
" I just wanted to be respectful of people's time."
It's fine. We're all volunteers here. We do this because we enjoy it. Your question is interesting anyway. It's a change from students asking us to do their homework for them.
Answers (1)
I understood that it's an art project but I'm worried that you're going to be disappointed with the results if you don't come up with a better concept of converting your text into an image.
Typically, text consist of around 26 different characters, 52 different glyphs when you add upper and lower case, plus a few punctuation characters. If you use non-english characters you may have a few more symbols unless you went for something like chinese. On the other hand images typically have at least 256 level of intensity for each colour channel. So already, if you just use map characters to colour, you're missing on a lot of colour. Characters are typically in the range 32-122, so you'll be missing on all the colours < 32 (very dark colour) and > 122 (saturated colours).
Furthermore, just converting numbers into an image, you typically don't get any pattern. Contiguous pixels don't have the same colour and it just look ugly. For example, I pasted the text of your original question in a text file and converted that into an image. This is what the image looks like:

and the same blown up so you can see each pixel:

The code I've used for the conversion:
%I copied the text of the question into the text file: text2image.txt. File attached.
text = fileread('text2image.txt'); %read the whole text
text = tex(1:end-mod(end, 3)-3); %clip to a multiple of 3. Then clip to a size that can be reshaped into a rectangle
img = uint8(reshape(text, 12, 23, 3)); %convert text into image
figure; imshow(img);
figure; imshow(imresize(img, 10, 'nearest')); %blown up version of the image
As you can see, there's nothing vivid since bright colours have intensity near 255 and we never go above 122, there's a lot of grey because if the 3 colour channels have more or less the same value, it looks grey, and the pattern looks completely random.
Now, with your bit stream you've attached if we use a basic approach to the conversion it looks a bit better in that it uses brighter colours. I'm not sure how you initially converted your text to bits.
One issue: don't use rtf, or word, or any rich text format to save your bit stream. They're not easy to read in matlab. Plain text file, as I've done above, works better.
First, let's import the bit stream. I'm hacking it here. The process works for your particular file, I'm looking into the rtf file and extracting the part that looks like the bit stream. This may not work correctly for other rtf files:
rawrtf = fileread('2 2.rtf'); %read raw rtf
bitstream = regexp(rawrtf, '(?<=\\cf0 )[01]+', 'match', 'once'); %get continuous stream of 01 after the \cf0 tag
As I said in my comment, storing numbers as '0' and '1' character is extremely inefficient. In matlab, you use 16 times more space. So let's go back to storing that as (uint8) numbers. First we need to crop the stream to a multiple of 8.
bitstream = bitstream(1:end-mod(end, 8));
bytestream = uint8(sum(reshape(bitstream - '0', 8, []) .* 2.^(7:-1:0)')); %convert consecutive 8 characters into 8-bit number
Finally, we can convert that into a colour image, by interpreted the bytes as RGB intensities. Since an image has 3 colour channels, we need to make sure we have a multiple of 3 bytes.
bytestream = bytestream(1:end-mod(end, 3));
Then we can reshape that into a MxNx3 image. We just have to find good M and N. We can do that by looking at the factors of the number of bytes:
>> factor(numel(bytestream) / 3)
ans =
3 11 73 79
Let's have a (79*3) x (73*11) image:
img = reshape(bytestream, 79*3, 73*11, 3);
imshow(img);
This is the result:

There's a pattern to your stream, which is a bit odd. But still, it's not very exciting.
As I said initially, you need to come up with an algorithm a bit more complex than just convert bits/bytes into intensity directly, in order to produce something more interesting.
5 Comments
Guillaume
on 18 Jul 2019
Emi Va's comment mistakenly posted as an answer moved here:
Hi Guillame,
First of all, I really like the pattern-style image quite a bit, it is the most interesting image by far, up to this point! Unfortunately I wasn't able to replicate your instructions and run the code to translate the entire code I have(sig!). Furthermore, someone else just told me that I may get more interesting results by converting the original genome file I had rather than the 'binary version' of it, because my binary is just adding to it and making the file heavier without really helping with the actual conversion. You also told me to stop using .rtf so from now I will be using just .txt files.
What I would like to do is to try your conversion codes on the text contained in these two files, to see how the results may differ:
If this is not something you can do (and/or if it falls a bit too close to 'students' homework') would you be so kind to give me instructions on how to do the conversion myself, starting from right after lunching the MatLab software? I will do my best to follow along, one step at a time!
A thousand thanks,
E
Guillaume
on 18 Jul 2019
because my binary is just adding to it and making the file heavier without really helping with the actual conversion
That's what I said as well. You're storing the same information, just using more space to store it.
As I said, it's an interesting project, so I don't mind helping you. Unfortunately, I don't have the time right now to go into much details, so you'll have to wait a bit. While it's very easy to generate an image from any data, there are many ways this could be done which would produce completely different results. The way I chose above is the most straightforward.
It makes a lot more sense to start from your raw data. However, I'm not sure how to understand your raw file (the 2nd one you link to). Can you explain how to interpret that file. I don't think you just want to interpret it as text. You'll probably get better result if we use the meaning of the text to generate the image rather than just the characters.
Emi Va
on 19 Jul 2019
Guillaume
on 19 Jul 2019
Can you point to that other conversation as this would avoid duplication of efforts?
Emi Va
on 19 Jul 2019
Categories
Find more on Green in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!