Calculate image height using file size and width

249 Views Asked by At

I'm not very hopeful that this is actually even possible because JPG is a lossy compression format, but I'll ask anyway.

I have some incomplete metadata about an image, and need to figure out the dimensions without loading the file into memory, or using command-line tools like imagemagick.

This is what I know:

  • file format is JPG (JPG is an 8-bit image format).
  • total file size is 96284 bytes.
  • image width is 600 pixels.

Assuming the height could be variable, is there a mathematical equation I can use to calculate the height predictably?

1

There are 1 best solutions below

0
On BEST ANSWER

No, there is no relation between the number of pixels and the compressed size.

You can try your luck by analyzing the file header to get the width/height tags. A library like jpeglib allows you to read just the header, but it takes some effort to integrate it.