0

When you open an image in a text editor you get some characters which don't really makes sense (at least not to me). Is there a way to add comments to that text, so the file would not apear damaged when opened with an image viewer.

So, something like this: enter image description here

Would turn into this: enter image description here

Cristian Marian
  • 750
  • 1
  • 8
  • 18
  • Please be more specific. I don't understand the problem. – Altay Mazlum Jul 02 '15 at 11:29
  • @AltayMazlum, it's not a problem, but i will edit my question to better reflect what I am after – Cristian Marian Jul 02 '15 at 12:33
  • I've done some research and did not come up to an answer for your problem. I'm sorry. – Altay Mazlum Jul 02 '15 at 13:05
  • 1
    @AltayMazlum,I also spent like two hours on this thing, but it seems that if there is a solution, it is buried deep. Thanks for trying! – Cristian Marian Jul 02 '15 at 13:23
  • Exactly. You're welcome, I' like to help people. – Altay Mazlum Jul 02 '15 at 13:24
  • Why do you want to do this? if you just want to tag the data than aergistal has the correct way of doing it. If you are looking to embed data in the binary of the image (plain test or encryoted) however, then there is a different approach that would work better. can you clarify your end desire with this? – Semicolons and Duct Tape Jul 22 '15 at 20:40
  • @SemicolonsandDuctTape, I'd like to see that different aproach. Aergistal's way ends up doing what I am after, but not in the way I intended. – Cristian Marian Jul 23 '15 at 08:30
  • You can conceal a hidden message in a file using steganography. Simply putting text inside the file won't work since the majority use a binary format. The characters that don't make sense are the actual bytes. – aergistal Jul 23 '15 at 08:48

2 Answers2

1

The way to go is setting metadata fields if your image format supports any.

For example, for a PNG you can set a comment field when exporting the file or with a separate tool like exiftool:

exiftool -comment="One does not simply put text into the image data" test.png

If the purpose of the text is to ensure ownership then take a look at digital watermarking.

aergistal
  • 29,947
  • 5
  • 70
  • 92
1

If you are looking to actually encode information in your image you should use steganography ( https://en.wikipedia.org/wiki/Steganography )

The wiki article runs you through the basics and shows and example of a picture of a cat hidden in a picture of trees as an example of how you can hide information. In the case of hiding text you can do the following:

Encoding

  • Come up with your phase: For argument's sake I'll use the word Hidden
  • Convert that text to a numeric representation - for simplicity I'll assume ASCII conversion of characters, but you don't have to

    "Hidden" = 72 105 100 100 101 110

  • Convert the numeric representation to Binary

    72 = 01001000 / 105 = 01101001 / 100 = 01100100 / 101=01100100 / 110=01101110

  • For each letter convert the 8 bit binary representations into four 2 bit binary representations that we shall call mA,mR,mG,mB for reasons that will become clear shortly

    72 = 01 00 10 00 => 1 0 2 0 = mA mR mG mB

  • Open an image file for editing: I would suggest using C# to load the image and then use Get/Set Pixels to edit them (How to manipulate images at the pixel level in C# )

  • use the last 2 bits of each color channel for each pixel to encode your message. For example to encode H in the first pixel of an image you can use the C# code at the end of the instructions

  • Once all letters of the Word - one per pixel - have been encoded in the image you are done.

Decoding

Use the same basic process in reverse.

  • You walk through the image one pixel at a time
  • You take the 2 least significant bits of each color channel in the pixel
  • You concatenate the LSB together in alpha,red,green,blue order.
  • You convert the concatenated bits into an 8 bit representation and then convert that binary form to base 10. Finally, you perform a look up on the base 10 number in an ASCII chart, or just cast the number to a char.
  • You repeat for the next pixel

The thing to remember is that the technique I described will allow you to encode information in the image without a human observer noticing because it only manipulates the image on the last 2 bits of each color channel in a single pixel, and human eyes cannot really tell the difference between the colors in the range of [(252,252,252,252) => (255,255,255,255)].

But as food for thought, I will mention that a computer can with the right algorithms, and there is active research into bettering the ability of a computer to be able to pick this sort of thing out.

So if you only want to put in a watermark then this should work, but if you want to actually hide something you have to encrypt the message and then perform the Steganography on the encrypted binary. Since encrypted data is MUCH larger than plain text data it will require an image with far more pixels.

Here is the code to encode H into the first pixel of your image in C#.

//H=72 and needs the following message Alpha, message Red, message Green, message Blue components
mA = 1;
mR = 0;
mG = 2;
mB = 0;
Bitmap myBitmap = new Bitmap("YourImage.bmp");
//pixel 0,0 is the first pixel
Color pixelColor = myBitmap.GetPixel(0, 0);
//the 252 places 1's in the 6 bits that we aren't manipulating so that ANDing with the message bits works
pixelColor = Color.FromArgb(c.A & (252 + mA), c.R & (252 + mR), c.G & (252 + mG), c.B & (252 + mB));
myBitmap.SetPixel(0, 0, pixelColor);
Community
  • 1
  • 1
Semicolons and Duct Tape
  • 2,927
  • 4
  • 20
  • 35