def generate(self, x: np.ndarray, y: np.ndarray | None = None, **kwargs) -> np.ndarray:
        """
        Generate adversarial samples and return them in an array.

        :param x: An array with the original inputs.
        :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
                  (nb_samples,). Only provide this parameter if you'd like to use true labels when crafting adversarial
                  samples. Otherwise, model predictions are used as labels to avoid the "label leaking" effect
                  (explained in this paper: https://arxiv.org/abs/1611.01236). Default is `None`.
        :param max_iter: Maximum number of optimisation iterations.
        :return: An array holding the adversarial examples.
        """
        if y is not None:
            y = check_and_transform_label_format(y, nb_classes=self.estimator.nb_classes, return_one_hot=False)

        if y is None:
            if self.targeted:
                raise ValueError("Target labels `y` need to be provided for a targeted attack.")
            y = np.argmax(self.estimator.predict(x), axis=1)
        else:
            if self.estimator.nb_classes == 2 and y.shape[1] == 1:
                raise ValueError(
                    "This attack has not yet been tested for binary classification with a single output classifier."
                )
            if y.ndim > 1 and y.shape[1] > 1:
                y = np.argmax(y, axis=1)

        y = y.flatten()  # type: ignore

        if self.th is None:
            logger.info(
                "Performing minimal perturbation Attack. \
                This could take long time to process. \
                For sanity check, pass th=10 to the Attack instance."
            )

        # NOTE: Pixel and Threshold Attacks are well-defined for unprocessed images where the pixel values are,
        #       8-Bit color i.e., the pixel values are np.uint8 in range [0, 255].

        # TO-DO: Better checking of input image.
        #        All other cases not tested needs the images to be rescaled to [0, 255].
        if self.estimator.clip_values[1] != 255.0:
            self.rescale = True
            x = (x - self.estimator.clip_values[0]) / (self.estimator.clip_values[1] - self.estimator.clip_values[0])
            x = x * 255.0

        x = x.astype(ART_NUMPY_DTYPE)

        adv_x_best = []
        self.adv_th = []
        for image, target_class in tqdm(zip(x, y), desc="Pixel threshold", disable=not self.verbose):  # type: ignore

            if self.th is None:

                min_th = -1
                start, end = 1, 127

                image_result = image

                while True:  # pragma: no cover

                    threshold = (start + end) // 2
                    success, trial_image_result = self._attack(image, target_class, threshold)

                    if success:
                        image_result = trial_image_result
                        end = threshold - 1
                        min_th = threshold
                    else:
                        start = threshold + 1

                    if end < start:
                        break

                self.adv_th = [min_th]

            else:

                success, image_result = self._attack(image, target_class, self.th)

                if not success:
                    image_result = image

            adv_x_best += [image_result]

        adv_x_best_array = np.array(adv_x_best)

        if self.rescale:
            adv_x_best_array = self.rescale_input(adv_x_best_array)

        return adv_x_best_array

        Generate adversarial samples and return them in an array.

        :param x: An array with the original inputs.
        :param y: Target values (class labels) one-hot-encoded of shape (nb_samples, nb_classes) or indices of shape
                  (nb_samples,). Only provide this parameter if you'd like to use true labels when crafting adversarial
                  samples. Otherwise, model predictions are used as labels to avoid the "label leaking" effect
                  (explained in this paper: https://arxiv.org/abs/1611.01236). Default is `None`.
        :param max_iter: Maximum number of optimisation iterations.
        :return: An array holding the adversarial examples.
        
art/attacks/evasion/pixel_threshold.py