How can I create a (printable) image (jpeg, bitmap, tiff...any format is ok) starting from a binary code?

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

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 Az!.png, 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.
Hi Guillame, thank you, this makes sense and this sort of trivial conversion is exactly what I would like to achieve. Unfortunately, the binary code needs to be formatted so that the computer 'reads' it as a certain file rather than just a sequence of 1s and 0s. So, let me rephrase my question a little better:
1) How can I format the binary code so that it becomes readable as a an image?
More specifically, I would like to format my binary code in ways that will correspond to as many image formats as possible (BMP, PNG, JPG, TIFF, etc...) Maybe most of these conversion won't yeld interesting results, but it is also possible that some of them will!
Thanks again,
E
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.
Hi Guillame,
First of all sorry for the remuneration offer, I didn't know it was against MATLAB's rules, I just wanted to be respectful of people's time.
What I call 'binary code' is a series of '0' and '1' characters stored as a long string of charactes inside a text file (.rtf). I have attached a fragment of this file (just a couple MB) for you to see here: https://drive.google.com/file/d/1xju0o2osHEzXmqxnzrr2gAWDMVq-HxpJ/view?usp=sharing
It would be amazing to learn how to convert it into pixels, or in other words, to learn how to format these characters so that they can be interpreted as if they were an image format. Especially if as you say it is something trivial and potentially easy to do even for a beginner like me!
One thing though, although it may 'seem' merely a trivial creative thing, this is actually a fundamental step for a larger and complex artistic research project I am conducting, and it is something I have been working on for over a year already (that's why I was willing to hire someone to help me with it). I just wanted to say this to make sure it's clear how much I care about figuring this out.
A BIT MORE INFO ON MY BINARY CODE: The complete string of binary code from which I extracted the fragment linked above weights 73.1MB, and I have been able to open it only with the TextEdit app. It was created by converting in binary code another (human readable) document, using an online 'text to binary' converter. Maybe this is important.
Thanks again and looking forward to your reply,
E
" 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.

Sign in to comment.

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

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
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.
Hi Guillame, thanks again for your help. I also found someone else online who is providing a few concrete options to convert my file in image. Interestingly, they do not look like your images, so it really makes sense to me to wait for all the input and help you can give me and my project. I am dying to see what can come out of that data! To answer your question:
"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."
Each line corresponds to a single SNP. Each SNP is described by the 'identifier #' (an 'rsid' or an internal id), its location on the reference human genome, and the genotype call oriented with respect to the plus strand on the human reference sequence. This is how each line would be understood if you're looking for its actual meaning:
# rsid chromosome position genotype
rs548049170 1 69869 TT
I hope this helps, as I am very curious to see how you imagine converting the actual 'meaning' of these characters, rather than just converting single characters!
Can you point to that other conversation as this would avoid duplication of efforts?
The other conversation was via email (I can't link to it directly). What has come out of it so far is a software that: "takes a string of text (which u paste into that text field) && when the button is pressed that text is converted to byte data, then image dimensions are calculated form that byte data (assuming a PNG image, we need 4 bytes per pixel, RGBA, so depending on how much data there is will determine the size of the image), then the bytes are converted into pixels and piped into an image element. from there u could right-mouse click and save the image."
Since all our email exchanges were confidential I won't link to this person's work, but the output of a string text, processed by his software, looks like this (of course depending on the kind of text the image will be slightly different):
upload for matlab.png
Also, I shared with this person your answers and tests, precisely to avoid duplication of efforts! So far it seems to me that the chances of getting the same kind of image, with being so many ways of converting the data, are very limited. I will keep you posted if and when I get other concrete converting systems, just in case.
Thanks!

Sign in to comment.

Asked:

on 13 Jul 2019

Commented:

on 19 Jul 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!