
    i                     N    d dl Z d dlmZ d dlZddlmZ ddlmZ  G d de      Z	y)    N)List   )register_to_config)VaeImageProcessorc                   P    e Zd ZdZe	 	 	 	 	 ddededededef
 fd       Ze	 dde	j                  j                  d	ed
edede	j                  j                  f
d       Zedde	j                  j                  dede	j                  j                  fd       Zedde	j                  j                  fd       Zde	j                  j                  dedede	j                  j                  fdZedee	j                  j                     de	j                  j                  fd       Z xZS )Flux2ImageProcessora  
    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_convert_rgb (`bool`, *optional*, defaults to be `True`):
            Whether to convert the images to RGB format.
    	do_resizevae_scale_factorvae_latent_channelsdo_normalizedo_convert_rgbc                 .    t         |   |||||       y )N)r	   r
   r   r   r   )super__init__)selfr	   r
   r   r   r   	__class__s         s/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/diffusers/pipelines/flux2/image_processor.pyr   zFlux2ImageProcessor.__init__+   s&     	- 3%) 	 	
    imagemax_aspect_ratiomin_side_lengthmax_areareturnc                 8   t        | t        j                  j                        st        dt	        |              | j
                  \  }}||k  s||k  rt        d| d| d| d      t        ||z  ||z        }||kD  rt        d| d| d|dd	| d
	      | S )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_inputz%Flux2ImageProcessor.check_image_input<   s    & %1DT%[MRSS

v ?"f&>#E7"VH4WXgWhhjk 
 56>6E>:**,UG2fXY|TWFX Y,,<+=RA 
 r   target_areac                     | j                   \  }}t        j                  |||z  z        }t        ||z        }t        ||z        }| j	                  ||ft
        j                  j                  j                        S N)	r"   mathsqrtintresizer   r   
ResamplingLANCZOS)r   r(   image_widthimage_heightscaler$   r%   s          r   _resize_to_target_areaz*Flux2ImageProcessor._resize_to_target_aread   sk    $)JJ!\		+|)CDEK%'(\E)*||UFOSYY-A-A-I-IJJr   c                 d    | j                   \  }}||z  }||k  r| S t        j                  | |      S r*   )r"   r   r4   )r   r(   r1   r2   pixel_counts        r   _resize_if_exceeds_areaz+Flux2ImageProcessor._resize_if_exceeds_arean   s:    $)JJ!\!L0+%L"99%MMr   r$   r%   c                 ~    |j                   \  }}||z
  dz  }||z
  dz  }||z   }||z   }	|j                  ||||	f      S )a  
        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%   r1   r2   lefttoprightbottoms
             r   _resize_and_cropz$Flux2ImageProcessor._resize_and_cropv   sZ    * %*JJ!\e#)f$*uvzz4eV455r   imagesc                    t        |       dk(  r| d   j                         S | D cg c]$  }|j                  dk7  r|j                  d      n|& } }t	        d | D              }t        d | D              }d}t        j                  j                  d||f|      }d}| D ]7  }||j                  z
  dz  }|j                  |||f       ||j                  z  }9 |S c c}w )zk
        Concatenate a list of PIL images horizontally with center alignment and white background.
           r   RGBc              3   4   K   | ]  }|j                     y wr*   )r$   .0imgs     r   	<genexpr>z9Flux2ImageProcessor.concatenate_images.<locals>.<genexpr>   s     6#))6   c              3   4   K   | ]  }|j                     y wr*   )r%   rE   s     r   rH   z9Flux2ImageProcessor.concatenate_images.<locals>.<genexpr>   s     66rI   )   rK   rK   r9   )lencopymodeconvertsumr#   r   r   newr%   paster$   )r@   rG   total_width
max_heightbackground_colornew_imgx_offsety_offsets           r   concatenate_imagesz&Flux2ImageProcessor.concatenate_images   s     v;!!9>>## OUUsE(9#++e$sBUU 6v666v66
 +))--Z'@BRS  	"C"SZZ/A5HMM#(34		!H	"
 # Vs   )C)T       TT)   @      )r^   )__name__
__module____qualname____doc__r   boolr-   r   staticmethodr   r   r'   r4   r7   r?   r   rY   __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+   typingr   	PIL.Imager   configuration_utilsr   image_processorr   r    r   r   <module>rk      s%       5 0Z+ Zr   