
    :h                    H    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y)    )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y)StatNc                   t        |t        j                        r|j                  |      | _        n%t        |t              r|| _        nd}t        |      t	        t        t        | j                        dz              | _        y)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)selfimage_or_listmaskmsgs       K/var/www/urcfiles/bundle/venv/lib/python3.12/site-packages/PIL/ImageStat.py__init__zStat.__init__    sb    * mU[[1",,T2DFt,"DF8CC. %DFFs 234
    c                    dd}t        dt        | j                        d      D cg c]  } || j                  |d        c}S c c}w )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.
        c                    d\  }}t        d      D ]  }| |   s	|} n t        ddd      D ]  }| |   s	|} ||fS  ||fS )N)   r   r
   r   )r   )r   res_minres_maxis       r   minmaxzStat.extrema.<locals>.minmaxM   se    %GW3ZQ<G   3B'Q<GG##	 ( G##r   r   r
   N)r   	list[int]returnztuple[int, int])r   r   r   )r   r!   r    s      r   extremazStat.extrema>   sC    
	$ -2!S[#,FG,Fqtvvabz",FGGGs   Ac           	         t        dt        | j                        d      D cg c]  }t        | j                  ||dz           c}S c c}w )z2Total number of pixels for each band in the image.r   r
   )r   r   r   sumr   r    s     r   countz
Stat.count[   sC     383tvv;2LM2LQDFF1q3w'(2LMMMs   "Ac                    g }t        dt        | j                        d      D ]=  }d}t        d      D ]  }||| j                  ||z      z  z  } |j                  |       ? |S )z-Sum of all pixels for each band in the image.r   r
           )r   r   r   append)r   vr    	layer_sumjs        r   r&   zStat.sum`   se     q#dff+s+AI3ZQA..	  HHY	 ,
 r   c           	         g }t        dt        | j                        d      D ]I  }d}t        d      D ]&  }||dz  t        | j                  ||z            z  z  }( |j	                  |       K |S )z5Squared sum of all pixels for each band in the image.r   r
   r*      )r   r   r   floatr+   )r   r,   r    sum2r.   s        r   r2   z	Stat.sum2l   sm     q#dff+s+AD3ZAtvva!e}!555  HHTN	 ,
 r   c                z    | j                   D cg c]!  }| j                  |   | j                  |   z  # c}S c c}w )zAAverage (arithmetic mean) pixel level for each band in the image.)r   r&   r(   r'   s     r   meanz	Stat.meanx   s5     6:ZZ@Zdjjm+Z@@@s   &8c                    g }| j                   D ]X  }d}| j                  |   dz  }|dz  }t        d      D ]  }|| j                  ||z      z   }||kD  s n |j	                         Z |S )z.Median pixel level for each band in the image.r   r0   r
   )r   r(   r   r   r+   )r   r,   r    shalfbr.   s          r   medianzStat.median}   sy     AA::a=A%DCA3Zq1u%t8   HHQK  r   c                    | j                   D cg c]4  }t        j                  | j                  |   | j                  |   z        6 c}S c c}w )z2RMS (root-mean-square) for each band in the image.)r   mathsqrtr2   r(   r'   s     r   rmszStat.rms   s@     BFLA		$))A,A67LLLs   9Ac                    | j                   D cg c]D  }| j                  |   | j                  |   dz  | j                  |   z  z
  | j                  |   z  F c}S c c}w )z$Variance for each band in the image.g       @)r   r2   r&   r(   r'   s     r   varzStat.var   sa    
 ZZ
 YYq\TXXa[C/4::a=@@DJJqMQ
 	
 
s   A	Ac                    | j                   D cg c]$  }t        j                  | j                  |         & c}S c c}w )z.Standard deviation for each band in the image.)r   r;   r<   r?   r'   s     r   stddevzStat.stddev   s1     15

;
1		$((1+&
;;;s   );)N)r   zImage.Image | list[int]r   zImage.Image | Noner#   None)r#   zlist[tuple[int, int]])r#   r"   )r#   zlist[float])__name__
__module____qualname__r   r   r$   r(   r&   r2   r4   r9   r=   r?   rA    r   r   r   r      s    QU545<N5	5< H H8 N N 	 	 	 	 A A   M M 
 
 < <r   r   )	
__future__r   r;   	functoolsr    r   r   GlobalrF   r   r   <module>rK      s'   . #  % ~< ~<B 
r   