2
$\begingroup$

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)?

$\endgroup$
5
  • 1
    $\begingroup$ 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. $\endgroup$
    – knaccc
    Mar 30, 2022 at 13:42
  • 1
    $\begingroup$ 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. $\endgroup$
    – DannyNiu
    Mar 30, 2022 at 14:50
  • 1
    $\begingroup$ You mention encryption in your question, but it seems like this is really a question about image formats. $\endgroup$
    – bmm6o
    Mar 30, 2022 at 18:59
  • 1
    $\begingroup$ 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. $\endgroup$
    – bmm6o
    Mar 30, 2022 at 19:01
  • 5
    $\begingroup$ I’m voting to close this question because it's about image formats and compression. No security is involved. $\endgroup$ Mar 30, 2022 at 20:30

1 Answer 1

1
$\begingroup$

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.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.