Attention! This forum will be made read-only by Dec-20. Please migrate to https://forum.opencv.org. Most of existing active users should've received invitation by e-mail.

# Is it possible to edit an image in hex format and then to open it as normal

I know that an image file contains not only pixels but also some other information about the file.

For example, a jpeg file always starts with FFDB FFE0.

I'm thinking if I can change a few of information in a jpeg file but it can be still opened as normal.

For example, imagine that there is a jpeg image file as below:

0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
0x00, 0x01, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x84, 0x00, 0x09, 0x06, 0x07, 0x13, 0x13, 0x12, 0x13,
0x13, 0x13, 0x13, 0x16, 0x15, 0x15, 0x17, 0x18, 0x18, 0x18, 0x18, 0x18, 0x17, 0x17, 0x18, 0x17,
0x1A, 0x16, 0x1F, 0x18, 0x1D, 0x17, 0x18, 0x17, 0x1A, 0x18, 0x18, 0x18, 0x1D, 0x28, 0x20, 0x1D,
0x1A, 0x25, 0x1D, 0x17, 0x1A, 0x21, 0x31, 0x21, 0x25, 0x29, 0x2D, 0x2E, 0x2E, 0x2E, 0x17, 0x1F


If the first 0xFF is a thing that I can change, meaning that no matter what it is, this image file can be printed as normal, I may want to change it with another value.

So my question is: Is there something I can change in a jpeg file?

Also, here is my program about converting a jpeg to hex file:

int main(int argc, char **argv)
{
Mat image;
for ( int i=0; i<image.total(); i++ )
{
cout << cv::format("0x%x ", image.at<uchar>(i)); // this prints hex numbers ( 0x40 for 64, etc)
}

return 0;
}


However, when I execute it, I get a file starting with 0x2f 0x30 0x30 0x32, how comes? I think all of jpg files should be started with 0xFF, 0xD8, 0xFF, 0xE0.

edit retag close merge delete

Sort by » oldest newest most voted

what you receive, is the raw image pixels, all header information (and file magic) is gone

ifstream in(argv[1],ios_base::binary);
while (in.good()) {
uchar byte;
in >> byte;
printf("%x ",byte);
}


but honestly, i think, you should forget about editing anything on this level immediately, e.g. you're simply not allowed to change the jpg file magic, as some programs will just refuse to read it.

more

1

I'm totally a newbie on things about image. My Boss wants to find a way to make an invisible mark on our images because our images are too important and expensive. My thought is to change something in headers... Well, if it doesn't work for jpg, obviously I got a wrong idea...

( 2017-08-10 01:17:03 -0500 )edit

can it be , you're looking for "watermarks" ? or "exif" headers ?

( 2017-08-10 01:26:57 -0500 )edit

Yes, watermarks, exactly :D in fact we have used watermark. But My Boss thought it was not enough...

( 2017-08-10 01:30:14 -0500 )edit

In my opinion, a watermark all over the image is better than something encoded. How are you ever going to prove that the image used actually contains that hex code? If I open it in an editor (most editors can handle slight data corruption) and resave it to disk, than your precaution is gone. Removing a watermark without leaving artefacts is impossible for now.

( 2017-08-10 07:07:20 -0500 )edit
1

@Yves , iirc, other libs, like ImageMagic will let you add "hidden things" to the exif headers in a "more orderly" fashion. just don't try to "hack it blindly", as you did before ;)

( 2017-08-10 07:28:12 -0500 )edit
2

You could also consider steganography. https://en.wikipedia.org/wiki/Stegano... Choose a solution capable to survive JPEG compression.

( 2017-08-10 23:50:47 -0500 )edit

Official site

GitHub

Wiki

Documentation