o
    “j  ã                   @  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   úWC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\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|} q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   Nr'   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   ç        N)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-   é   N)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 | rˆ j| ˆ j |  nd ‘qS ©r   )r+   r*   r#   r(   r   r   r&   {   s   . zStat.mean.<locals>.<listcomp>N©r   r(   r   r(   r   Úmeanx   ó   z	Stat.meanc                 C  sd   g }| j D ]*}d}| j| d }|d }tdƒD ]}|| j||   }||kr) q*q| |¡ q|S )z.Median pixel level for each band in the image.r   r1   r   N)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                   s4   g | ]}ˆ j | rt ˆ j| ˆ j |  ¡nd ‘qS r5   )r+   ÚmathÚsqrtr3   r#   r(   r   r   r&   �   s    &ÿÿzStat.rms.<locals>.<listcomp>Nr6   r(   r   r(   r   Úrms�   s   
þzStat.rmsc                   r4   )z$Variance for each band in the image.c                   sF   g | ]}ˆ j | rˆ j| ˆ j| d  ˆ j |   ˆ j |  nd‘qS )g       @r   )r+   r3   r*   r#   r(   r   r   r&   ˜   s    ý.ÿüzStat.var.<locals>.<listcomp>Nr6   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>Nr6   r(   r   r(   r   Ústddev¡   r8   zStat.stddevr"   )r   r   r	   r
   r   r   )r   r   )r   r   )r   r,   )Ú__name__Ú
__module__Ú__qualname__r   r   r)   r+   r*   r3   r7   r;   r>   r?   r@   r   r   r   r   r      s*    ÿr   )	Ú
__future__r   r<   Ú	functoolsr   Ú r   r   ÚGlobalr   r   r   r   Ú<module>   s    	