
    cCi;$                         S r SSKJrJrJr  SSKJr  SSKJrJ	r	  SSK
JrJr  SSKJrJrJrJrJrJr  SSKJrJr  / S	Qr/ S
QrS r " S S\5      rS/rg)z"Image processor class for Idefics.    )CallableOptionalUnion)Image   )BaseImageProcessorBatchFeature)resizeto_channel_dimension_format)ChannelDimension
ImageInputPILImageResamplingmake_flat_list_of_imagesto_numpy_arrayvalid_images)
TensorTypeis_torch_available)g3<4'?gwgM?gy{ ?)gB91?gwt.?g	U?c                     U R                   S:X  a  U $ U R                  S5      n[        R                  " SUR                  S5      n[        R
                  " X!5      nUR                  S5      nU$ )NRGBRGBA)   r   r   )modeconvertr   newsizealpha_composite)image
image_rgba
backgroundr   s       n/home/james-whalen/.local/lib/python3.13/site-packages/transformers/models/idefics/image_processing_idefics.pyconvert_to_rgbr!   &   s_     zzUv&J6:??ODJ++JCO%--e4O    c                   \  ^  \ rS rSrSrS/r      SS\S\\\	\
\	   4      S\\\	\
\	   4      S	\\   S
\S\\\	4   SS4U 4S jjjrSSSSSSS\R                  4S\S	\\   S\\\\4      S\\\	\
\	   4      S\\\	\
\	   4      S\\   S
\\   S\\	   S\\\\4      S\4S jjrSrU =r$ )IdeficsImageProcessor3   a  
Constructs a Idefics image processor.

Args:
    image_size (`int`, *optional*, defaults to 224):
        Resize to image size
    image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`):
        Mean to use if normalizing the image. This is a float or list of floats the length of the number of
        channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can be
        overridden by the `image_mean` parameter in the `preprocess` method.
    image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`):
        Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
        number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method.
        Can be overridden by the `image_std` parameter in the `preprocess` method.
    image_num_channels (`int`, *optional*, defaults to 3):
        Number of image channels.
    do_rescale (`bool`, *optional*, defaults to `True`):
        Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
        the `preprocess` method.
    rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
        Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
        method.
pixel_valuesNr   
image_size
image_mean	image_stdimage_num_channels
do_rescalerescale_factorreturnc                    > [         TU ]  " S0 UD6  Xl        X@l        Ub  UO[        U l        Ub  UO[        U l        XPl        X`l	        g )N )
super__init__r'   r*   IDEFICS_STANDARD_MEANr(   IDEFICS_STANDARD_STDr)   r+   r,   )	selfr'   r(   r)   r*   r+   r,   kwargs	__class__s	           r    r1   IdeficsImageProcessor.__init__N   sI     	"6"$"4(2(>*DY&/&;AU$,r"   images	transformreturn_tensorsc
           	         Ub  UOU R                   nUb  UOU R                  nUb  UOU R                  nUb  UOU R                  nUb  UOU R                  nUb  UOU R
                  nX34n[        U[        5      (       a  [        U5      S:X  a  / $ U R                  U5      n[        U5      n[        U5      (       d  [        S5      eUbF  [        5       (       d  [        S5      eSSKnU Vs/ s H
  o" U5      PM     nnUR!                  U5      $ U Vs/ s H  n[#        U5      PM     nnU Vs/ s H  n[%        U5      PM     nnU Vs/ s H  n['        X[(        R*                  S9PM     nnU Vs/ s H  oR-                  XS9PM     nnU Vs/ s H  oR/                  XUS9PM     nnU Vs/ s H  n[1        U[2        R4                  5      PM     nn[7        SU0U	S	9S   nU$ s  snf s  snf s  snf s  snf s  snf s  snf s  snf )
a  
Preprocess a batch of images.

Args:
    images (`ImageInput`):
        A list of images to preprocess.
    image_size (`int`, *optional*, defaults to `self.image_size`):
        Resize to image size
    image_num_channels (`int`, *optional*, defaults to `self.image_num_channels`):
        Number of image channels.
    image_mean (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_MEAN`):
        Mean to use if normalizing the image. This is a float or list of floats the length of the number of
        channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. Can
        be overridden by the `image_mean` parameter in the `preprocess` method.
    image_std (`float` or `list[float]`, *optional*, defaults to `IDEFICS_STANDARD_STD`):
        Standard deviation to use if normalizing the image. This is a float or list of floats the length of the
        number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess`
        method. Can be overridden by the `image_std` parameter in the `preprocess` method.
    transform (`Callable`, *optional*, defaults to `None`):
        A custom transform function that accepts a single image can be passed for training. For example,
        `torchvision.Compose` can be used to compose multiple transforms. If `None` - an inference mode is
        assumed - and then a preset of inference-specific transforms will be applied to the images
    do_rescale (`bool`, *optional*, defaults to `True`):
        Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
        the `preprocess` method.
    rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
        Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
        method.

Returns:
    a PyTorch tensor of the processed images

Nr   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.z.To pass in `transform` torch must be installed)resample)r   scale)meanstdr&   )datatensor_type)r'   r*   r(   r)   r+   r,   
isinstancelistlenfetch_imagesr   r   
ValueErrorr   ImportErrortorchstackr!   r   r
   r   BICUBICrescale	normalizer   r   FIRSTr	   )r4   r8   r*   r'   r(   r)   r9   r+   r,   r:   r5   r   rH   xr   s                  r    
preprocess IdeficsImageProcessor.preprocessa   s   \ $.#9Zt
3E3Q/W[WnWn#-#9Zt
!*!6IDNN	#-#9Zt
+9+E4K^K^'fd##Fq(8I""6*)&1F##:   %''!"RSS,23FqilFF3;;v&& .44V.#V4-34V.#V4PVWPV1&+=+E+EFPVWOUVve,,U,AvVMSTV...CVTRXYRXQ-a1A1G1GHRXYNF#;XYgh 4 54WVTYs*   ,G&G+/G0
"G52G:G?/$H)r+   r(   r*   r'   r)   r,   )   NNr   Tgp?)__name__
__module____qualname____firstlineno____doc__model_input_namesintr   r   floatrC   boolr1   r   PYTORCHr   dictstrr   rO   __static_attributes____classcell__)r6   s   @r    r$   r$   3   s   0 (( :>9=,-,3-- U5$u+#567- E%e"456	-
 %SM- - c5j)- 
- -, -./3:>9=(,%)*.;E;M;M\\ %SM\ T#s(^,	\
 U5$u+#567\ E%e"456\ H%\ TN\ !\ !sJ!78\ 
\ \r"   r$   N)rV   typingr   r   r   PILr   image_processing_utilsr   r	   image_transformsr
   r   image_utilsr   r   r   r   r   r   utilsr   r   r2   r3   r!   r$   __all__r/   r"   r    <module>rg      sU    ) , ,  F C  4 < ; 
J. JZ #
#r"   