How can I create a (printable) image (jpeg, bitmap, tiff...any format is ok) starting from a binary code?
14 ビュー (過去 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 件のコメント
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
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
2019 年 7 月 19 日
Can you point to that other conversation as this would avoid duplication of efforts?
参考
カテゴリ
Help Center および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!