def get_jpeg_size(jpeg_bytes):
    """Get the size of a JPEG image without reading and decompressing the entire
  file.  Based upon:  
   * https://github.com/shibukawa/imagesize_py/blob/master/imagesize.py#L87
  """
    import struct
    from io import BytesIO
    buf = BytesIO(jpeg_bytes)
    head = buf.read(24)
    if not head.startswith(b'\xff\xd8'):
        raise ValueError('Invalid JPEG header')
    buf.seek(0)
    size = 2
    ftype = 0
    while not 192 <= ftype <= 207 or ftype in [196, 200, 204]:
        buf.seek(size, 1)
        byte = buf.read(1)
        while ord(byte) == 255:
            byte = buf.read(1)
        ftype = ord(byte)
        size = struct.unpack('>H', buf.read(2))[0] - 2
    buf.seek(1, 1)
    height, width = struct.unpack('>HH', buf.read(4))
    return width, height
