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

조회 수: 17 (최근 30일)
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
Emi Va
Emi Va 2019년 7월 14일
편집: Emi Va 2019년 7월 14일
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
Guillaume
Guillaume 2019년 7월 14일
" 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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2019년 7월 14일
편집: Guillaume 2019년 7월 14일
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
Guillaume
Guillaume 2019년 7월 19일
Can you point to that other conversation as this would avoid duplication of efforts?
Emi Va
Emi Va 2019년 7월 19일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by