code: def split_into_segments(data):
      
      if data[0:2] != b"\xff\xd8":
          raise InvalidImageDataError("Given data isn't JPEG.")
  
      head = 2
      segments = [b"\xff\xd8"]
      while 1:
          if data[head: head + 2] == b"\xff\xda":
              segments.append(data[head:])
              break
          else:
              length = struct.unpack(">H", data[head + 2: head + 4])[0]
              endPoint = head + length + 2
              seg = data[head: endPoint]
              segments.append(seg)
              head = endPoint
  
          if (head >= len(data)):
              raise InvalidImageDataError("Wrong JPEG data.")
      return segments
docs: Slices JPEG meta data into a list from JPEG binary data.