def METHOD_NAME(self, f):
    """Extract the images into a 4D uint8 numpy array [index, y, x, depth].
    Args:
      f: A file object that can be passed into a gzip reader.
    Returns:
      data: A 4D uint8 numpy array [index, y, x, depth].
    Raises:
      ValueError: If the bytestream does not start with 2051.
    """
    logger.info('Extracting %s', f.name)
    with gzip.GzipFile(fileobj=f) as bytestream:
        magic = self._read32(bytestream)
        if magic != 2051:
            raise ValueError(f"Invalid magic number {magic} in MNIST image file: {f.name} (expected 2051")
        num_images = self._read32(bytestream)
        rows = self._read32(bytestream)
        cols = self._read32(bytestream)
        buf = bytestream.read(rows * cols * num_images)
        data = np.frombuffer(buf, dtype=np.uint8)
        data = data.reshape(num_images, rows, cols)
        return self.create_array([PIL.Image.fromarray(image) for image in data])