def _size_from_jpeg(img_data):
  # JPEG format:
  # 0xff 0xd8 (Start of Image)
  # repeated
  #   block marker (0xff 0x??)
  #   length (?? ??) of block
  #   length - 2 bytes data
  #
  # the image data block has marker 0xff 0xc0
  # the image data format is:
  #   ?? ?? length
  #   1 byte precision
  #   ?? ?? height
  #   ?? ?? width
  pos = 0
  # SOI 0xff 0xd8
  if (ord(img_data[pos]) != 0xff or ord(img_data[pos + 1]) != 0xd8):
    _debug('SOI not found %u %u' % (ord(img_data[pos]), ord(img_data[pos + 1])))
    return None
  pos += 2
  # start of stream
  sos = False
  while not sos:
    # marker
    if (ord(img_data[pos]) != 0xff):
      _debug('marker not found at pos %d' % (pos))
      return None
    if (ord(img_data[pos + 1]) == 0xc0):
      sos = True
      pos += 2
    else:
      pos += 2
      len = _read_2byte(img_data, pos)
      _debug('len %d' % len)
      pos += len
  pos += 3
  _debug('h %x %x' % (ord(img_data[pos]), ord(img_data[pos+1])))
  height = _read_2byte(img_data, pos)
  pos += 2
  width = _read_2byte(img_data, pos)
  return (width, height)
A very crude JPEG reader so that we can get the image size
dataset/ETHPy150Open CollabQ/CollabQ/common/imageutil.py/_size_from_jpeg