Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

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

asked 2017-08-10 00:57:27 -0500

Yves gravatar image

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;
    image = imread("test.jpg", 0);  
    for ( int i=0; i<; i++ )
        cout << cv::format("0x%x ",<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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-08-10 01:06:18 -0500

berak gravatar image

updated 2017-08-10 01:28:06 -0500

imread() will decode the image, meaning: read & discard the jpeg header, then de-compress it.

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

to see the image headers, instead of imread(), you will have to use low-level io:

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.

edit flag offensive delete link more



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...

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

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

berak gravatar imageberak ( 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...

Yves gravatar imageYves ( 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.

StevenPuttemans gravatar imageStevenPuttemans ( 2017-08-10 07:07:20 -0500 )edit

@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 ;)

berak gravatar imageberak ( 2017-08-10 07:28:12 -0500 )edit

You could also consider steganography. Choose a solution capable to survive JPEG compression.

Formicant gravatar imageFormicant ( 2017-08-10 23:50:47 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-08-10 00:57:27 -0500

Seen: 582 times

Last updated: Aug 10 '17