U
    M»{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dœdd„Zedd	œd
d„ƒZedd	œdd„ƒZedd	œdd„ƒZedd	œdd„ƒZedd	œdd„ƒZ	edd	œdd„ƒZ
edd	œdd„ƒZedd	œdd„ƒZedd	œdd„ƒZdS )ÚStatNzImage.Image | list[int]zImage.Image | NoneÚNone)Úimage_or_listÚmaskÚreturnc                 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   úL/var/www/html/bwave/python/venv/lib/python3.8/site-packages/PIL/ImageStat.pyÚ__init__    s    
zStat.__init__zlist[tuple[int, int]])r
   c                   s2   dd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.
        ú	list[int]ztuple[int, int])r   r
   c                 S  sP   d\}}t dƒD ]}| | r|} q&qt dddƒD ]}| | r2|} qHq2||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   ©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.countzlist[float]c                 C  sR   g }t dt| jƒdƒD ]6}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   Úsum2r*   r   r   r   r-   l   s     z	Stat.sum2c                   s   ‡ 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   s    z	Stat.meanc                 C  sd   g }| j D ]T}d}| j| d }|d }tdƒD ]"}|| j||   }||kr0 qTq0| |¡ q
|S )z.Median pixel level for each band in the image.r   r+   r   )r   r&   r   r   r(   )r   r)   r   ÚsZhalfÚbr*   r   r   r   Úmedian}   s    
zStat.medianc                   s   ‡ fdd„ˆ j D ƒS )z2RMS (root-mean-square) for each band in the image.c                   s&   g | ]}t  ˆ j| ˆ j|  ¡‘qS r   )ÚmathÚsqrtr-   r&   r   r#   r   r   r!   �   s     zStat.rms.<locals>.<listcomp>r.   r#   r   r#   r   Úrms�   s    zStat.rmsc                   s   ‡ fdd„ˆ j D ƒS )z$Variance for each band in the image.c                   s8   g | ]0}ˆ j | ˆ j| d  ˆ j|   ˆ j|  ‘qS )g       @)r-   r%   r&   r   r#   r   r   r!   •   s   ÿzStat.var.<locals>.<listcomp>r.   r#   r   r#   r   Úvar’   s    
þzStat.varc                   s   ‡ fdd„ˆ j D ƒS )z.Standard deviation for each band in the image.c                   s   g | ]}t  ˆ j| ¡‘qS r   )r3   r4   r6   r   r#   r   r   r!   �   s     zStat.stddev.<locals>.<listcomp>r.   r#   r   r#   r   Ústddevš   s    zStat.stddev)N)Ú__name__Ú
__module__Ú__qualname__r   r   r$   r&   r%   r-   r/   r2   r5   r6   r7   r   r   r   r   r      s(    ÿr   )	Ú
__future__r   r3   Ú	functoolsr   Ú r   r   ÚGlobalr   r   r   r   Ú<module>   s    