Score:2

Get Pixel Data from JPG/JPEG but still bring the integrity?

in flag

As far as I know, JPG is a lossy image compression format

Meanwhile, BMP is a generally uncompressed image format, whose file structure consists of bitmapFileHeader (14 bytes) and bitmapInfoHeader (40 bytes), the rest of which is pixel data.

So I just need to pass the first 54 bytes and store it as a HEADER variable, and store the rest in the PIXELDATA variable.

In this state I can freely encrypt and decrypt PIXELDATA, and can recombine it with HEADER. So that the integrity of the file is maintained.

But in the case of JPG it is different. I'd like to try reading the pixels manually, but I don't know what position the index bytes are in (unlike BMP which skips 54 bytes).

I use Python with various libraries like PIL or Numpy. Both managed to read Pixels, but when saved as a new JPG file, it differed from the original, in that:

  1. Comparison of SHA-256 checksums.
  2. Different file sizes.
  3. Some of the pixel colors change from the original if you look at it in more detail.

My question is, is there another way to read the pixel data of a JPG file but still provide the integrity of the original JPG file when saved in a new JPG file (Same checksum, file sizes, and offcourse the pixel color)?

knaccc avatar
es flag
I think your question can be simplified as: Is there way to create a JPG file which can be repeatedly compressed and decompressed without the pixels changing? Or maybe you just want to know how to put a different header onto an existing JPG file without having to decompress and recompress the pixels.
DannyNiu avatar
vu flag
J2K can be used losslessly. If you can find a lib that does deterministic JPEG2000 encoding, and that the format is interoperable with your application(s), then I think the problem is solved.
ph flag
You mention encryption in your question, but it seems like this is really a question about image formats.
ph flag
And there's the obvious comment that the easiest way to not save a different version is to not save it at all. Can you track if you have any changes you want to save? Otherwise lossy compression gonna compress lossy.
Gilles 'SO- stop being evil' avatar
cn flag
I’m voting to close this question because it's about image formats and compression. No security is involved.
Score:1
jp flag

In JPG pixels are not stored. They are derived in runtime, as user reads the file. What is stored is some factors of how much a color change in a given block, for example 8 by 8 pixels.

The data that is stored, these factors, contain significantly less information that pixels would.

Therefore you cant create a JPG with perfect pixel data. Even if you try, these factors that actually store the data, dont contain enough bits to represent your pixels.

This is for most JPG usage. You could try some excessive formats, that store more data per pixel, comparable to BMP. Search for lossless JPG then. Keep in mind, a lot of online services will re-compress the image. Ruining your data in the process.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.