
    
3j>                     B    S SK r S SKrSSKJr  SSKJr   " S S\5      rg)    N   )register_to_config)VaeImageProcessorc                   z  ^  \ rS rSrSr\       SS\S\S\S\S\S\S	\4U 4S
 jjj5       r\	 SS\
R                  R                  S\S\S\S\
R                  R                  4
S jj5       r\	SS\
R                  R                  S\S\
R                  R                  4S jj5       r\	SS\
R                  R                  4S jj5       rS\
R                  R                  S\S\S\
R                  R                  4S jr\	S\\
R                  R                     S\
R                  R                  4S j5       rSrU =r$ )Flux2ImageProcessor   aV  
Image processor to preprocess the reference (character) image for the Flux2 model.

Args:
    do_resize (`bool`, *optional*, defaults to `True`):
        Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept
        `height` and `width` arguments from [`image_processor.VaeImageProcessor.preprocess`] method.
    vae_scale_factor (`int`, *optional*, defaults to `16`):
        VAE (spatial) scale factor. If `do_resize` is `True`, the image is automatically resized to multiples of
        this factor.
    vae_latent_channels (`int`, *optional*, defaults to `32`):
        VAE latent channels.
    do_normalize (`bool`, *optional*, defaults to `True`):
        Whether to normalize the image to [-1,1].
    do_binarize (`bool`, *optional*, defaults to `False`):
        Whether to binarize the image to 0/1.
    do_convert_rgb (`bool`, *optional*, defaults to be `True`):
        Whether to convert the images to RGB format.
    do_convert_grayscale (`bool`, *optional*, defaults to be `False`):
        Whether to convert the images to grayscale format.
	do_resizevae_scale_factorvae_latent_channelsdo_normalizedo_binarizedo_convert_rgbdo_convert_grayscalec           
      ,   > [         TU ]  UUUUUUUS9  g )N)r	   r
   r   r   r   r   r   )super__init__)	selfr	   r
   r   r   r   r   r   	__class__s	           c/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/flux2/image_processor.pyr   Flux2ImageProcessor.__init__.   s,     	- 3%#)!5 	 	
    imagemax_aspect_ratiomin_side_lengthmax_areareturnc                 <   [        U [        R                  R                  5      (       d  [        S[	        U 5       35      eU R
                  u  pEXB:  d  XR:  a  [        SU SU SU S35      e[        XE-  XT-  5      nXa:  a  [        SU SU SUS S	U S
3	5      eU $ )u  
Check if image meets minimum size and aspect ratio requirements.

Args:
    image: PIL Image to validate
    max_aspect_ratio: Maximum allowed aspect ratio (width/height or height/width)
    min_side_length: Minimum pixels required for width and height
    max_area: Maximum allowed area in pixels²

Returns:
    The input image if valid

Raises:
    ValueError: If image is too small or aspect ratio is too extreme
z%Image must be a PIL.Image.Image, got zImage too small:    ×z#. Both dimensions must be at least pxzAspect ratio too extreme: z	 (ratio: z.1fz:1). Maximum allowed ratio is z:1)
isinstancePILImage
ValueErrortypesizemax)r   r   r   r   widthheightaspect_ratios          r   check_image_input%Flux2ImageProcessor.check_image_inputC   s    & %11DT%[MRSS

 "f&>#E7"VH4WXgWhhjk 
 5>6>:*,UG2fXY|TWFX Y,,<+=RA 
 r   target_areac                     U R                   u  p#[        R                  " XU-  -  5      n[        X$-  5      n[        X4-  5      nU R	                  XV4[
        R                  R                  R                  5      $ N)	r%   mathsqrtintresizer!   r"   
ResamplingLANCZOS)r   r,   image_widthimage_heightscaler'   r(   s          r   _resize_to_target_area*Flux2ImageProcessor._resize_to_target_areak   sa    $)JJ!		+|)CDEK'(\)*||UOSYY-A-A-I-IJJr   c                 ^    U R                   u  p#X#-  nXA::  a  U $ [        R                  X5      $ r.   )r%   r   r8   )r   r,   r5   r6   pixel_counts        r   _resize_if_exceeds_area+Flux2ImageProcessor._resize_if_exceeds_areau   s2    $)JJ!!0%L"99%MMr   r'   r(   c                 p    UR                   u  pEXB-
  S-  nXS-
  S-  nXb-   nXs-   n	UR                  XgX45      $ )aJ  
center crop the image to the specified width and height.

Args:
    image (`PIL.Image.Image`):
        The image to resize and crop.
    width (`int`):
        The width to resize the image to.
    height (`int`):
        The height to resize the image to.

Returns:
    `PIL.Image.Image`:
        The resized and cropped image.
   )r%   crop)
r   r   r'   r(   r5   r6   lefttoprightbottoms
             r   _resize_and_crop$Flux2ImageProcessor._resize_and_crop}   sL    * %*JJ!#)$*zz4e455r   imagesc                    [        U 5      S:X  a  U S   R                  5       $ U  Vs/ s H%  oR                  S:w  a  UR                  S5      OUPM'     n n[	        S U  5       5      n[        S U  5       5      nSn[        R                  R                  SX#4U5      nSnU  H5  nX1R                  -
  S-  nUR                  XU45        XaR                  -  nM7     U$ s  snf )z[
Concatenate a list of PIL images horizontally with center alignment and white background.
   r   RGBc              3   8   #    U  H  oR                   v   M     g 7fr.   )r'   .0imgs     r   	<genexpr>9Flux2ImageProcessor.concatenate_images.<locals>.<genexpr>   s     6v))v   c              3   8   #    U  H  oR                   v   M     g 7fr.   )r(   rL   s     r   rO   rP      s     6vvrQ   )   rS   rS   r?   )lencopymodeconvertsumr&   r!   r"   newr(   paster'   )rG   rN   total_width
max_heightbackground_colornew_imgx_offsety_offsets           r   concatenate_images&Flux2ImageProcessor.concatenate_images   s     v;!!9>>## OUUfsE(9#++e$sBfU 6v666v66
 +))--'@BRS C"ZZ/A5HMM#(34		!H 
 # Vs   ,C )T       TFTF)   @      )rh   )__name__
__module____qualname____firstlineno____doc__r   boolr1   r   staticmethodr!   r"   r*   r8   r<   rE   listra   __static_attributes____classcell__)r   s   @r   r   r      s   ,   "#%!!#%*

 
 !	

 
 
 
 #
 
( fq%yy%25%LO%`c%	% %N Kciioo KC KZ]ZcZcZiZi K K N399?? N N6yy6 6 	6
 
6@ 4		#8 SYY__  r   r   )r/   	PIL.Imager!   configuration_utilsr   image_processorr   r   rc   r   r   <module>rv      s"      5 0b+ br   