






'''simple docstring'''






from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    get_resize_output_image_size,
    normalize,
    pad,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    get_image_size,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging
from ...utils.import_utils import is_vision_available


__A     						= logging.get_logger(__name__)


if is_vision_available():
      import PIL




class a_     (     UpperCamelCase_				):
   _snake_case	   =				["""pixel_values"""]







   def __init__(self       , __a = True       , __a = None       , __a = PILImageResampling.BILINEAR       , __a = True       , __a = False       , __a = True       , __a = True       , __a = 1 / 2_5_5       , __a = True       , __a = None       , __a = None       , **__a       , )      ->   Tuple:
      """simple docstring"""






      super().__init__(**_lowerCAmelCase)

      __snake_case					:   Tuple  				=			size if size is not None else {"height": 2_5_6_0, "width": 1_9_2_0}
      if isinstance(_lowerCAmelCase       , (tuple, list)):
         # The previous feature extractor size parameter was in (width, height) format
         __snake_case					:   Union[str, Any]  				=			size[::-1]
      __snake_case					:   int  				=			get_size_dict(_lowerCAmelCase)

      __snake_case					:   Optional[Any]  				=			do_resize
      __snake_case					:   int  				=			size
      __snake_case					:   List[str]  				=			resample
      __snake_case					:   Any  				=			do_thumbnail
      __snake_case					:   Optional[int]  				=			do_align_long_axis
      __snake_case					:   Tuple  				=			do_pad
      __snake_case					:   Union[str, Any]  				=			do_rescale
      __snake_case					:   Union[str, Any]  				=			rescale_factor
      __snake_case					:   Optional[int]  				=			do_normalize
      __snake_case					:   Dict  				=			image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
      __snake_case					:   Union[str, Any]  				=			image_std if image_std is not None else IMAGENET_STANDARD_STD







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a = None)      ->   List[str]:
      """simple docstring"""






      __snake_case					:   int  				=			get_image_size(_lowerCAmelCase)
      __snake_case					:   str  				=			size["height"], size["width"]

      if (output_width < output_height and input_width > input_height) or (
          output_width > output_height and input_width < input_height
      ):
         __snake_case					:   Any  				=			np.rotaa(_lowerCAmelCase       , 3)

      if data_format is not None:
         __snake_case					:   Optional[int]  				=			to_channel_dimension_format(_lowerCAmelCase       , _lowerCAmelCase)

      return image







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a = False       , __a = None       , )      ->   Union[str, Any]:
      """simple docstring"""






      __snake_case					:   Tuple  				=			size["height"], size["width"]
      __snake_case					:   Optional[int]  				=			get_image_size(_lowerCAmelCase)

      __snake_case					:   List[Any]  				=			output_width - input_width
      __snake_case					:   Dict  				=			output_height - input_height

      if random_padding:
         __snake_case					:   List[str]  				=			np.random.randint(low=0       , high=delta_height + 1)
         __snake_case					:   List[Any]  				=			np.random.randint(low=0       , high=delta_width + 1)
      else:
         __snake_case					:   Dict  				=			delta_height // 2
         __snake_case					:   int  				=			delta_width // 2

      __snake_case					:   Optional[int]  				=			delta_height - pad_top
      __snake_case					:   Dict  				=			delta_width - pad_left

      __snake_case					:   Optional[Any]  				=			((pad_top, pad_bottom), (pad_left, pad_right))
      return pad(_lowerCAmelCase       , _lowerCAmelCase       , data_format=_lowerCAmelCase)







   def      SCREAMING_SNAKE_CASE__       (self       , *__a       , **__a)      ->   Optional[Any]:
      """simple docstring"""






      logger.info('pad is deprecated and will be removed in version 4.27. Please use pad_image instead.')
      return self.pad_image(*_lowerCAmelCase       , **_lowerCAmelCase)







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a = PILImageResampling.BICUBIC       , __a = None       , **__a       , )      ->   List[Any]:
      """simple docstring"""






      __snake_case					:   Any  				=			get_image_size(_lowerCAmelCase)
      __snake_case					:   List[str]  				=			size["height"], size["width"]

      # We always resize to the smallest of either the input or output size.
      __snake_case					:   Union[str, Any]  				=			min(_lowerCAmelCase       , _lowerCAmelCase)
      __snake_case					:   Any  				=			min(_lowerCAmelCase       , _lowerCAmelCase)

      if height == input_height and width == input_width:
         return image

      if input_height > input_width:
         __snake_case					:   Union[str, Any]  				=			int(input_width * height / input_height)
      elif input_width > input_height:
         __snake_case					:   int  				=			int(input_height * width / input_width)

      return resize(
          _lowerCAmelCase       , size=(height, width)       , resample=_lowerCAmelCase       , reducing_gap=2.0       , data_format=_lowerCAmelCase       , **_lowerCAmelCase)







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a = PILImageResampling.BICUBIC       , __a = None       , **__a       , )      ->   int:
      """simple docstring"""






      __snake_case					:   Optional[Any]  				=			get_size_dict(_lowerCAmelCase)
      __snake_case					:   Optional[Any]  				=			min(size['height']       , size['width'])
      __snake_case					:   Tuple  				=			get_resize_output_image_size(_lowerCAmelCase       , size=_lowerCAmelCase       , default_to_square=_lowerCAmelCase)
      __snake_case					:   Optional[int]  				=			resize(_lowerCAmelCase       , size=_lowerCAmelCase       , resample=_lowerCAmelCase       , data_format=_lowerCAmelCase       , **_lowerCAmelCase)
      return resized_image







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a = None       , **__a       , )      ->   int:
      """simple docstring"""






      return rescale(_lowerCAmelCase       , scale=_lowerCAmelCase       , data_format=_lowerCAmelCase       , **_lowerCAmelCase)







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a       , __a       , __a = None       , **__a       , )      ->   Any:
      """simple docstring"""






      return normalize(_lowerCAmelCase       , mean=_lowerCAmelCase       , std=_lowerCAmelCase       , data_format=_lowerCAmelCase       , **_lowerCAmelCase)







   def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a = None       , __a = None       , __a = None       , __a = None       , __a = None       , __a = None       , __a = False       , __a = None       , __a = None       , __a = None       , __a = None       , __a = None       , __a = None       , __a = ChannelDimension.FIRST       , **__a       , )      ->   Any:
      """simple docstring"""






      __snake_case					:   Union[str, Any]  				=			do_resize if do_resize is not None else self.do_resize
      __snake_case					:   Optional[int]  				=			size if size is not None else self.size
      if isinstance(_lowerCAmelCase       , (tuple, list)):
         # Previous feature extractor had size in (width, height) format
         __snake_case					:   List[str]  				=			size[::-1]
      __snake_case					:   List[str]  				=			get_size_dict(_lowerCAmelCase)
      __snake_case					:   Optional[int]  				=			resample if resample is not None else self.resample
      __snake_case					:   Union[str, Any]  				=			do_thumbnail if do_thumbnail is not None else self.do_thumbnail
      __snake_case					:   str  				=			do_align_long_axis if do_align_long_axis is not None else self.do_align_long_axis
      __snake_case					:   int  				=			do_pad if do_pad is not None else self.do_pad
      __snake_case					:   Optional[Any]  				=			do_rescale if do_rescale is not None else self.do_rescale
      __snake_case					:   str  				=			rescale_factor if rescale_factor is not None else self.rescale_factor
      __snake_case					:   int  				=			do_normalize if do_normalize is not None else self.do_normalize
      __snake_case					:   Dict  				=			image_mean if image_mean is not None else self.image_mean
      __snake_case					:   str  				=			image_std if image_std is not None else self.image_std

      __snake_case					:   Any  				=			make_list_of_images(_lowerCAmelCase)

      if not valid_images(_lowerCAmelCase):
         raise ValueError(
             'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
             'torch.Tensor, tf.Tensor or jax.ndarray.')

      if do_resize and size is None:
         raise ValueError('Size must be specified if do_resize is True.')

      if do_rescale and rescale_factor is None:
         raise ValueError('Rescale factor must be specified if do_rescale is True.')

      if do_pad and size is None:
         raise ValueError('Size must be specified if do_pad is True.')

      if do_normalize and (image_mean is None or image_std is None):
         raise ValueError('Image mean and std must be specified if do_normalize is True.')

      # All transformations expect numpy arrays.
      __snake_case					:   Union[str, Any]  				=			[to_numpy_array(_lowerCAmelCase) for image in images]

      if do_align_long_axis:
         __snake_case					:   Union[str, Any]  				=			[self.align_long_axis(_lowerCAmelCase       , size=_lowerCAmelCase) for image in images]

      if do_resize:
         __snake_case					:   Optional[Any]  				=			[self.resize(image=_lowerCAmelCase       , size=_lowerCAmelCase       , resample=_lowerCAmelCase) for image in images]

      if do_thumbnail:
         __snake_case					:   str  				=			[self.thumbnail(image=_lowerCAmelCase       , size=_lowerCAmelCase) for image in images]

      if do_pad:
         __snake_case					:   List[Any]  				=			[self.pad_image(image=_lowerCAmelCase       , size=_lowerCAmelCase       , random_padding=_lowerCAmelCase) for image in images]

      if do_rescale:
         __snake_case					:   Optional[Any]  				=			[self.rescale(image=_lowerCAmelCase       , scale=_lowerCAmelCase) for image in images]

      if do_normalize:
         __snake_case					:   Tuple  				=			[self.normalize(image=_lowerCAmelCase       , mean=_lowerCAmelCase       , std=_lowerCAmelCase) for image in images]

      __snake_case					:   List[str]  				=			[to_channel_dimension_format(_lowerCAmelCase       , _lowerCAmelCase) for image in images]

      __snake_case					:   List[str]  				=			{"pixel_values": images}
      return BatchFeature(data=_lowerCAmelCase       , tensor_type=_lowerCAmelCase)





from unittest.mock import patch

import datasets
from datasets import Dataset



def    UpperCAmelCase__	(  ):
  lowercase     :Any   						=    Dataset.from_dict({"col_1": [3, 2, 0, 1]} )

  with patch("tqdm.auto.tqdm" ) as mock_tqdm:
    datasets.disable_progress_bar()
    dset.map(lambda lowerCamelCase : {"col_2": x["col_1"] + 1} )
    mock_tqdm.assert_not_called()

    mock_tqdm.reset_mock()

    datasets.enable_progress_bar()
    dset.map(lambda lowerCamelCase : {"col_2": x["col_1"] + 1} )
    mock_tqdm.assert_called()



