Search code examples
pythonimage-processingcompressionjpegjpeg2000

How to find .jpg file size without headers?


I need to find size of a .jpg file without its header (or metadata). How can I do that?

I guess JPEG data contains two parts:

  1. Header or metadata which is not related to image and has information about subsampling values or quantization table
  2. Main data which is result of compressing the image and depends on image. For a reason, I just care about size of main data. How can I get the size (in bytes)?

Solution

  • Thanx for comments. As Anon Coward said I've found this useful image which shows different parts of a JPEG file: JPEG structure

    I wanted length of Image Data so I need to find 0xFF 0xDA and count bytes from there, then subtract 16 from it. Here is the code:

    def get_JPEG_size(file_path):
      total_size = os.path.getsize(file_path)
    
      with open(file_path, 'rb') as f:
        s = f.read()
      
      header_size = s.find(b'\xff\xda')
      if header_size == -1:
        print("`FF DA` not found!")
        return 0
      header_size += (2 + 12) + 2
      
      data_size = total_size - header_size
    
      return data_size