+
    Ci-                     L   ^ RI t ^ RIt^ RIHt ^ RIHtHtHtHtH	t	H
t
 ^ RIt ^ RIt^ R	IHtHt ^ R
IHt ^ RIHt ^ RIHt ^ RIHt ^ RIHtHt ^ RI H!t!H"t" ^RI#H$t$ ^RI%H%t%H&t& ^RI'H(t( ]PR                  ! ^4       ]!! ^4      t* ! R R]4      t+R R lt,R R lt-R R lt.R R lt/R R lt0R R  lt1R! R" lt2R# R$ lt3R% R& lt4R)R' R( llt5R#   ] dL    ]! R4       ]! R4       ]! 4        ]! R4       ]! R4       ]! 4        ]! R4       ]P"                  ! ^4        Li ; i)*    N)Enum)AnyListOptionalTupleUnioncastzNo onnxruntime backend found.z-Please install rembg with CPU or GPU support:z'    pip install "rembg[cpu]"  # for CPUz3    pip install "rembg[gpu]"  # for NVIDIA/CUDA GPUzLFor more information, see: https://github.com/danielgatis/rembg#installation)ImageImageOps)r
   )estimate_alpha_cf)estimate_foreground_ml)stack_images)binary_erosiongaussian_filter)diskopening)new_session)sessionssessions_names)BaseSessionc                   "    ] tR t^'t^ t^t^tRtR# )
ReturnType N)__name__
__module____qualname____firstlineno__BYTESPILLOWNDARRAY__static_attributes__r       F/var/www/html/photoedit/myenv/lib/python3.14/site-packages/rembg/bg.pyr   r   '   s    EFGr"   r   c                `    V ^8  d   QhR\         R\         R\        R\        R\        R\         /# )   imgmaskforeground_thresholdbackground_thresholderode_structure_sizereturn)PILImageint)formats   "r#   __annotate__r/   -   sD     2 2	2
2 2 	2
 2 2r"   c                   V P                   R8X  g   V P                   R8X  d   V P                  R4      p \        P                  ! V 4      p\        P                  ! V4      pWb8  pWc8  pRp	V^ 8  d(   \        P                  ! WD3\        P
                  R7      p	\        WyR7      p\        W^R7      p\        P                  ! VP                  \        P
                  ^R7      p
^W&   ^ W&   VR	,          pV
R	,          p\        W4      p\        W4      p\        W4      p\        P                  ! V^,          ^ ^4      P                  \        P
                  4      p\        P                  ! V4      pV# )
a  
Perform alpha matting on an image using a given mask and threshold values.

This function takes a PIL image `img` and a PIL image `mask` as input, along with
the `foreground_threshold` and `background_threshold` values used to determine
foreground and background pixels. The `erode_structure_size` parameter specifies
the size of the erosion structure to be applied to the mask.

The function returns a PIL image representing the cutout of the foreground object
from the original image.
RGBACMYKRGBN)dtype)	structure)r5   border_value)r4   
fill_valueg     o@)modeconvertnpasarrayonesuint8r   fullshaper   r   r   clipastyper
   	fromarray)r&   r'   r(   r)   r*   	img_array
mask_arrayis_foregroundis_backgroundr5   trimapimg_normalizedtrimap_normalizedalpha
foregroundcutouts   &&&&&           r#   alpha_matting_cutoutrM   -   s*   $ xx6SXX/kk% 

3ID!J5M5MIaGG!8
	 #=FM"=TUVMWWZ%%RXX#FFFF&Nn@E'>J*,FWWVc\1c*11"((;F__V$FMr"   c                <    V ^8  d   QhR\         R\         R\         /# r%   r&   r'   r+   r,   )r.   s   "r#   r/   r/   b   s!      h h 8 r"   c                x    \         P                  ! RV P                  ^ 4      p\         P                  ! WV4      pV# )aG  
Perform a simple cutout operation on an image using a mask.

This function takes a PIL image `img` and a PIL image `mask` as input.
It uses the mask to create a new image where the pixels from `img` are
cut out based on the mask.

The function returns a PIL image representing the cutout of the original
image using the mask.
r1   )r
   newsize	composite)r&   r'   emptyrL   s   &&  r#   naive_cutoutrV   b   s/     IIfsxx!,E__S.FMr"   c                <    V ^8  d   QhR\         R\         R\         /# rO   rP   )r.   s   "r#   r/   r/   r   s!        h r"   c                (    V P                  V4       V # )z
Apply the specified mask to the image as an alpha cutout.

Args:
    img (PILImage): The image to be modified.
    mask (PILImage): The mask to be applied.

Returns:
    PILImage: The modified image with the alpha cutout applied.
)putalpha)r&   r'   s   &&r#   putalpha_cutoutrZ   r   s     LLJr"   c                F    V ^8  d   QhR\         \        ,          R\        /# )r%   imgsr+   )r   r,   )r.   s   "r#   r/   r/      s      T(^  r"   c                P    V P                  ^ 4      pV  F  p\        W4      pK  	  V# )z
Concatenate multiple images vertically.

Args:
    imgs (List[PILImage]): The list of images to be concatenated.

Returns:
    PILImage: The concatenated image.
)popget_concat_v)r\   pivotims   &  r#   get_concat_v_multirb      s*     HHQKEU' Lr"   c                <    V ^8  d   QhR\         R\         R\         /# )r%   img1img2r+   rP   )r.   s   "r#   r/   r/      s!      x x H r"   c                    \         P                  ! RV P                  V P                  VP                  ,           34      pVP	                  V R4       VP	                  V^ V P                  34       V# )z
Concatenate two images vertically.

Args:
    img1 (PILImage): The first image.
    img2 (PILImage): The second image to be concatenated below the first image.

Returns:
    PILImage: The concatenated image.
r1   )r   r   )r
   rR   widthheightpaste)rd   re   dsts   && r#   r_   r_      sT     ))FTZZt{{)BC
DCIIdFIIdQ$%Jr"   c                X    V ^8  d   QhR\         P                  R\         P                  /# )r%   r'   r+   )r:   ndarray)r.   s   "r#   r/   r/      s"     
 
rzz 
bjj 
r"   c                    \        V \        4      p \        V P                  \        P
                  4      ^R7      p \        P                  ! V ^8  ^ ^4      P                  \        P                  4      p V # )z
Post Process the mask for a smooth boundary by applying Morphological Operations
Research based on paper: https://www.sciencedirect.com/science/article/pii/S2352914821000757
args:
    mask: Binary Numpy Mask
)sigma)r   kernelr   rA   r:   float64wherer=   )r'   s   &r#   post_processrr      sS     4 D4;;rzz2!<D88D3J3'..rxx8DKr"   c                r    V ^8  d   QhR\         R\        \        \        \        \        3,          R\         /# )r%   r&   colorr+   )r,   r   r-   )r.   s   "r#   r/   r/      s/       sCc7I1J x r"   c                    \         P                  ! RV P                  \        V4      4      p\         P                  ! W 4      pV# )z
Apply the specified background color to the image.

Args:
    img (PILImage): The image to be modified.
    color (Tuple[int, int, int, int]): The RGBA color to be applied.

Returns:
    PILImage: The modified image with the background color applied.
r1   )r
   rR   rS   tuplealpha_composite)r&   rt   
backgroundcolored_images   &&  r#   apply_background_colorrz      s4     6388U5\:J))*:Mr"   c                0    V ^8  d   QhR\         R\         /# )r%   r&   r+   rP   )r.   s   "r#   r/   r/      s     
8 
8x 
8H 
8r"   c                J    \        \        \        P                  ! V 4      4      # )z
Fix the orientation of the image based on its EXIF data.

Args:
    img (PILImage): The image to be fixed.

Returns:
    PILImage: The fixed image.
)r	   r,   r   exif_transpose)r&   s   &r#   fix_image_orientationr~      s     (11#677r"   c                B    V ^8  d   QhR\         \        R3,          RR/# )r%   models.r+   N)rv   str)r.   s   "r#   r/   r/      s"     7 7E#s(O 7 7r"   c                x   \        V 4      ^ 8X  d   \        R4       \        \        4      p V  Fb  p\        P
                  ! V4      pVf'   \        RV 24       \        P                  ! ^4       KC  \        RV 24        VP                  4        Kd  	  R#   \         d   p\        RT 24        Rp?K  Rp?ii ; i)z'
Download models for image processing.
z+No models specified, downloading all modelsNzError: no model found: zDownloading model: zError downloading model: )
lenprintrv   r   r   getsysexitdownload_models	Exception)r   modelsessiones   &   r#   r   r      s     6{a;<~&,,u%?+E734HHQK'w/07'')   71!5667s   ?BB9 B44B9c                   V ^8  d   QhR\         \        \        \        P                  3,          R\
        R\        R\        R\        R\        \        ,          R\
        R\
        R	\        \        \        \        \        \        3,          ,          R
\
        R\        \        ,          R\        \        ,          R\         \        \        \        P                  3,          /# )r%   dataalpha_matting"alpha_matting_foreground_threshold"alpha_matting_background_thresholdalpha_matting_erode_sizer   	only_maskpost_process_maskbgcolorforce_return_bytesargskwargsr+   )r   bytesr,   r:   rl   boolr-   r   r   r   r   )r.   s   "r#   r/   r/      s     m m
x+
,mm ),m ),	m
 "m k"m m m eCc3./0m m C=m smm 5(BJJ&'mr"   c
                   \        V \        4      '       g	   V	'       dX   \        P                  p\	        \
        \        P                  ! \        P                  ! \	        \        V 4      4      4      4      pM\        V \
        4      '       d"   \        P                  p\	        \
        V 4      pMx\        V \        P                  4      '       d6   \        P                  p\	        \
        \        P                  ! V 4      4      pM#\        RP!                  \#        V 4      4      4      hVP%                  RR4      p\'        V4      pVf   \)        R.V
O5/ VB pVP*                  ! V.V
O5/ VB p. pV F  pV'       d4   \        P                  ! \-        \        P.                  ! V4      4      4      pV'       d   TpM:V'       d    \1        VVVVV4      pM!V'       d   \3        VV4      pM\5        VV4      pVP7                  V4       K  	  Tp\9        V4      ^ 8  d   \;        V4      pVe   V'       g   \=        VV4      p\        P                  V8X  d   V# \        P                  V8X  d   \        P>                  ! V4      # \        P                  ! 4       pVPA                  VR4       VPC                  ^ 4       VPE                  4       #   \         d'    T'       d   \3        TT4      p L\5        TT4      p ELi ; i)aW  
Remove the background from an input image.

This function takes in various parameters and returns a modified version of the input image with the background removed. The function can handle input data in the form of bytes, a PIL image, or a numpy array. The function first checks the type of the input data and converts it to a PIL image if necessary. It then fixes the orientation of the image and proceeds to perform background removal using the 'u2net' model. The result is a list of binary masks representing the foreground objects in the image. These masks are post-processed and combined to create a final cutout image. If a background color is provided, it is applied to the cutout image. The function returns the resulting cutout image in the format specified by the input 'return_type' parameter or as python bytes if force_return_bytes is true.

Parameters:
    data (Union[bytes, PILImage, np.ndarray]): The input image data.
    alpha_matting (bool, optional): Flag indicating whether to use alpha matting. Defaults to False.
    alpha_matting_foreground_threshold (int, optional): Foreground threshold for alpha matting. Defaults to 240.
    alpha_matting_background_threshold (int, optional): Background threshold for alpha matting. Defaults to 10.
    alpha_matting_erode_size (int, optional): Erosion size for alpha matting. Defaults to 10.
    session (Optional[BaseSession], optional): A session object for the 'u2net' model. Defaults to None.
    only_mask (bool, optional): Flag indicating whether to return only the binary masks. Defaults to False.
    post_process_mask (bool, optional): Flag indicating whether to post-process the masks. Defaults to False.
    bgcolor (Optional[Tuple[int, int, int, int]], optional): Background color for the cutout image. Defaults to None.
    force_return_bytes (bool, optional): Flag indicating whether to return the cutout image as bytes. Defaults to False.
    *args (Optional[Any]): Additional positional arguments.
    **kwargs (Optional[Any]): Additional keyword arguments.

Returns:
    Union[bytes, PILImage, np.ndarray]: The cutout image with the background removed.
z^Input type {} is not supported. Try using force_return_bytes=True to force python bytes outputrY   Fu2netPNG)#
isinstancer   r   r   r	   r,   r
   openioBytesIOr   r:   rl   r    rB   
ValueErrorr.   typer^   r~   r   predictrr   arrayrM   rZ   rV   appendr   rb   rz   r;   saveseekread)r   r   r   r   r   r   r   r   r   r   r   r   return_typer&   rY   maskscutoutsr'   rL   bios   &&&&&&&&&&*,        r#   remover      sG   H $"4 &&8UZZ

4t3D(EFG	D(	#	# ''8T"	D"**	%	% ((8U__T23lssT

 	
 zz*e,H  
$Cg777OOC1$1&1EG??<#?@DF5-66, (d3%c40v7 : F
7|a#G,9'8K'[(zz&!!
**,C
KKUHHQK88:=  5,S$7F)#t4F	5s   KK7K7'K76K7)	F   
   r   NFFNF)6r   r   enumr   typingr   r   r   r   r   r	   numpyr:   onnxruntimeortImportErrorr   r   PILr
   r   	PIL.Imager,   !pymatting.alpha.estimate_alpha_cfr   +pymatting.foreground.estimate_foreground_mlr   pymatting.util.utilr   scipy.ndimager   r   skimage.morphologyr   r   session_factoryr   r   r   sessions.baser   set_default_logger_severityro   r   rM   rV   rZ   rb   r_   rr   rz   r~   r   r   r   r"   r#   <module>r      s    	 
  : :    ' ? N , 9 , ( . &    "	a 2j  "
"
87*m mq  
	
)*	
9:	G	
34	
?@	G	V HHQK
s   C AD#"D#