o
    Zh                     @  sB   d dl mZ d dlZd dlmZ ddlmZ G dd dZeZdS )    )annotationsN)cached_property   )Imagec                   @  s   e Zd Z	dd dd	Zed!ddZed"ddZed#ddZed#ddZed#ddZ	ed"ddZ
ed#ddZed#ddZed#ddZdS )$StatNimage_or_listImage.Image | list[int]maskImage.Image | NonereturnNonec                 C  sT   t |tjr||| _nt |tr|| _nd}t|ttt| jd | _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	   msg r   D/var/www/html/lang_env/lib/python3.10/site-packages/PIL/ImageStat.py__init__    s   
zStat.__init__list[tuple[int, int]]c                   s,   ddd  fd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                 S  sV   d\}}t dD ]
}| | r|} nqt dddD ]}| | r&|} ||fS q||fS )N)   r   r   r   )r   )r   Zres_minZres_maxir   r   r   minmaxM   s   zStat.extrema.<locals>.minmaxc                   s   g | ]} j |d  qS N)r   .0r    r!   r   r   r   
<listcomp>Y   s    z Stat.extrema.<locals>.<listcomp>r   r   N)r   r   r   r   r   r   r   r   r   r%   r   extrema>   s   
"zStat.extremar   c                   s     fddt dt jdD S )z2Total number of pixels for each band in the image.c                   s"   g | ]}t  j||d   qS )r   )sumr   r#   r(   r   r   r&   ^   s   " zStat.count.<locals>.<listcomp>r   r   r'   r(   r   r(   r   count[   s    z
Stat.countlist[float]c                 C  sR   g }t dt| jdD ]}d}t dD ]}||| j||   7 }q|| q|S )z-Sum of all pixels for each band in the image.r   r           )r   r   r   append)r   vr    Z	layer_sumjr   r   r   r*   `   s   zStat.sumc                 C  sZ   g }t dt| jdD ]}d}t dD ]}||d t| j||   7 }q|| q|S )z5Squared sum of all pixels for each band in the image.r   r   r-      )r   r   r   floatr.   )r   r/   r    sum2r0   r   r   r   r3   l   s    z	Stat.sum2c                       fdd j D S )zAAverage (arithmetic mean) pixel level for each band in the image.c                   s    g | ]} j |  j|  qS r   )r*   r+   r#   r(   r   r   r&   {   s     zStat.mean.<locals>.<listcomp>r   r(   r   r(   r   meanx      z	Stat.meanc                 C  sd   g }| j D ]*}d}| j| d }|d }tdD ]}|| j||   }||kr) nq|| q|S )z.Median pixel level for each band in the image.r   r1   r   )r   r+   r   r   r.   )r   r/   r    sZhalfbr0   r   r   r   median}   s   
zStat.medianc                   r4   )z2RMS (root-mean-square) for each band in the image.c                   s&   g | ]}t  j|  j|  qS r   )mathsqrtr3   r+   r#   r(   r   r   r&      s   & zStat.rms.<locals>.<listcomp>r5   r(   r   r(   r   rms   r7   zStat.rmsc                   r4   )z$Variance for each band in the image.c                   s8   g | ]} j |  j| d   j|    j|  qS )g       @)r3   r*   r+   r#   r(   r   r   r&      s    *zStat.var.<locals>.<listcomp>r5   r(   r   r(   r   var   s   
zStat.varc                   r4   )z.Standard deviation for each band in the image.c                   s   g | ]
}t  j| qS r   )r;   r<   r>   r#   r(   r   r   r&      s    zStat.stddev.<locals>.<listcomp>r5   r(   r   r(   r   stddev   r7   zStat.stddevr"   )r   r   r	   r
   r   r   )r   r   )r   r   )r   r,   )__name__
__module____qualname__r   r   r)   r+   r*   r3   r6   r:   r=   r>   r?   r   r   r   r   r      s*    r   )	
__future__r   r;   	functoolsr    r   r   Globalr   r   r   r   <module>   s    