
     f                    P    d dl mZ d dlZd dlmZ ddlmZ  G d d          ZeZdS )    )annotationsN)cached_property   )Imagec                      e Zd Z	 dddZedd
            Zedd            Zedd            Zedd            Zedd            Z	edd            Z
edd            Zedd            Zedd            ZdS )StatNimage_or_listImage.Image | list[int]maskImage.Image | NonereturnNonec                8   t          |t          j                  r|                    |          | _        n.t          |t                    r|| _        nd}t          |          t	          t          t          | j                  dz                      | _        dS )a
  
        Calculate statistics for the given image. If a mask is included,
        only the regions covered by that mask are included in the
        statistics. You can also pass in a previously calculated histogram.

        :param image: A PIL image, or a precalculated histogram.

            .. note::

                For a PIL image, calculations rely on the
                :py:meth:`~PIL.Image.Image.histogram` method. The pixel counts are
                grouped into 256 bins, even if the image has more than 8 bits per
                channel. So ``I`` and ``F`` mode images have a maximum ``mean``,
                ``median`` and ``rms`` of 255, and cannot have an ``extrema`` maximum
                of more than 255.

        :param mask: An optional mask.
        z$first argument must be image or list   N)	
isinstancer   	histogramhlist	TypeErrorrangelenbands)selfr	   r   msgs       P/var/www/api.educacionweb.es/myenv/lib/python3.11/site-packages/PIL/ImageStat.py__init__zStat.__init__    s    * mU[11 	!",,T22DFFt,, 	!"DFF8CC.. %DFs 23344


    list[tuple[int, int]]c                n     d	d fdt          dt           j                  d          D             S )
au  
        Min/max values for each band in the image.

        .. note::
            This relies on the :py:meth:`~PIL.Image.Image.histogram` method, and
            simply returns the low and high bins used. This is correct for
            images with 8 bits per channel, but fails for other modes such as
            ``I`` or ``F``. Instead, use :py:meth:`~PIL.Image.Image.getextrema` to
            return per-band extrema for the image. This is more correct and
            efficient because, for non-8-bit modes, the histogram method uses
            :py:meth:`~PIL.Image.Image.getextrema` to determine the bins used.
        r   	list[int]r   tuple[int, int]c                    d\  }}t          d          D ]}| |         r|} nt          ddd          D ]}| |         r|} n||fS )N)   r   r   r#   )r   )r   res_minres_maxis       r   minmaxzStat.extrema.<locals>.minmaxM   s    %GW3ZZ  Q< GE 3B''  Q< GE G##r   c                @    g | ]} j         |d                    S N)r   ).0r'   r(   r   s     r   
<listcomp>z Stat.extrema.<locals>.<listcomp>Y   s-    GGGqtvabbz""GGGr   r   r   )r   r    r   r!   r   r   r   )r   r(   s   `@r   extremazStat.extrema>   sL    
	$ 
	$ 
	$ 
	$ HGGGGE!S[[#,F,FGGGGr   r    c                b      fdt          dt           j                  d          D             S )z2Total number of pixels for each band in the image.c                N    g | ]!}t          j        ||d z                      "S )r   )sumr   r+   r'   r   s     r   r,   zStat.count.<locals>.<listcomp>^   s0    MMMQDF1q3w;'((MMMr   r   r   r-   r   s   `r   countz
Stat.count[   s4     NMMM%3tv;;2L2LMMMMr   list[float]c                    g }t          dt          | j                  d          D ]A}d}t          d          D ]}||| j        ||z            z  z  }|                    |           B|S )z-Sum of all pixels for each band in the image.r   r           )r   r   r   append)r   vr'   	layer_sumjs        r   r1   zStat.sum`   s{     q#df++s++ 	  	 AI3ZZ / /QA..		HHYr   c           	         g }t          dt          | j                  d          D ]Q}d}t          d          D ](}||dz  t          | j        ||z                      z  z  })|                    |           R|S )z5Squared sum of all pixels for each band in the image.r   r   r7      )r   r   r   floatr8   )r   r9   r'   sum2r;   s        r   r?   z	Stat.sum2l   s     q#df++s++ 	 	AD3ZZ 6 6Atva!e}!5!555HHTNNNNr   c                *      fd j         D             S )zAAverage (arithmetic mean) pixel level for each band in the image.c                F    g | ]}j         |         j        |         z  S  )r1   r4   r2   s     r   r,   zStat.mean.<locals>.<listcomp>{   s*    @@@djm+@@@r   r   r3   s   `r   meanz	Stat.meanx   s"     A@@@TZ@@@@r   c                    g }| j         D ][}d}| j        |         dz  }|dz  }t          d          D ]}|| j        ||z            z   }||k    r n|                    |           \|S )z.Median pixel level for each band in the image.r   r=   r   )r   r4   r   r   r8   )r   r9   r'   shalfbr;   s          r   medianzStat.median}   s      	 	AA:a=A%DCA3ZZ  q1u%t88E HHQKKKKr   c                *      fd j         D             S )z2RMS (root-mean-square) for each band in the image.c                j    g | ]/}t          j        j        |         j        |         z            0S rB   )mathsqrtr?   r4   r2   s     r   r,   zStat.rms.<locals>.<listcomp>   s4    LLLA	$)A,A677LLLr   rC   r3   s   `r   rmszStat.rms   s"     MLLLLLLLr   c                *      fd j         D             S )z$Variance for each band in the image.c                    g | ]<}j         |         j        |         d z  j        |         z  z
  j        |         z  =S )g       @)r?   r1   r4   r2   s     r   r,   zStat.var.<locals>.<listcomp>   sU     
 
 
 Yq\TXa[C/4:a=@@DJqMQ
 
 
r   rC   r3   s   `r   varzStat.var   s0    
 
 
 
Z
 
 
 	
r   c                *      fd j         D             S )z.Standard deviation for each band in the image.c                N    g | ]!}t          j        j        |                   "S rB   )rL   rM   rQ   r2   s     r   r,   zStat.stddev.<locals>.<listcomp>   s)    ;;;1	$(1+&&;;;r   rC   r3   s   `r   stddevzStat.stddev   s!     <;;;
;;;;r   r*   )r	   r
   r   r   r   r   )r   r   )r   r    )r   r5   )__name__
__module____qualname__r   r   r.   r4   r1   r?   rD   rI   rN   rQ   rT   rB   r   r   r   r      s`       QU5 5 5 5 5< H H H _H8 N N N _N 	 	 	 _	 	 	 	 _	 A A A _A    _ M M M _M 
 
 
 _
 < < < _< < <r   r   )	
__future__r   rL   	functoolsr    r   r   GlobalrB   r   r   <module>r\      s   . # " " " " "  % % % % % %      ~< ~< ~< ~< ~< ~< ~< ~<B 
r   