1

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?

JamesWilson
  • 3,833
  • 2
  • 30
  • 40
  • 2
    As Yves says, you don't need to load the entire file to get the dimensions, nor do you need to add any massive dependency like ImageMagick, you can fairly easily parse the dimensions out if that's all you want. May I ask what the objection is to loading it? And what language you use? – Mark Setchell Apr 09 '20 at 07:45
  • In this case, I cannot load the image *at all* because the file is hosted from a remote server. I'm consuming an API — Solr in this case — via Javascript React/Next.js app. The API has incomplete metadata (as mentioned), and I want to add the height and width dimensions to an `` tag in server-side render so that as the image loads on the client side, the browser reserves the correct amount of space based on the aspect ratio before the image finishes downloading. It's clear to me now that the API needs to be updated to include height and width calculations. Thanks to you both. – JamesWilson Apr 09 '20 at 15:45

1 Answers1

4

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.