o
    üÞhÍ  ã                   @  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   úM/var/www/html/premium_crap/venv/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   Úres_minÚ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"   Ú	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   Úfloatr0   )r   r1   r"   Úsum2r3   r   r   r   r6   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   r4   r   )r   r-   r   r   r0   )r   r1   r"   ÚsÚhalfÚbr3   r   r   r   Úmedian}   s   
ÿzStat.medianc                   r7   )z2RMS (root-mean-square) for each band in the image.c                   s&   g | ]}t  ˆ j| ˆ j|  ¡‘qS r   )ÚmathÚsqrtr6   r-   r%   r*   r   r   r(   �   s   & zStat.rms.<locals>.<listcomp>r8   r*   r   r*   r   Úrms�   r:   zStat.rmsc                   r7   )z$Variance for each band in the image.c                   s8   g | ]}ˆ j | ˆ j| d  ˆ j|   ˆ j|  ‘qS )g       @)r6   r,   r-   r%   r*   r   r   r(   •   s    *ÿÿzStat.var.<locals>.<listcomp>r8   r*   r   r*   r   Úvar’   s   
þzStat.varc                   r7   )z.Standard deviation for each band in the image.c                   s   g | ]
}t  ˆ j| ¡‘qS r   )r?   r@   rB   r%   r*   r   r   r(   �   s    zStat.stddev.<locals>.<listcomp>r8   r*   r   r*   r   Ústddevš   r:   zStat.stddevr$   )r   r   r	   r
   r   r   )r   r   )r   r   )r   r.   )Ú__name__Ú
__module__Ú__qualname__r   r   r+   r-   r,   r6   r9   r>   rA   rB   rC   r   r   r   r   r      s*    ÿr   )	Ú
__future__r   r?   Ú	functoolsr   Ú r   r   ÚGlobalr   r   r   r   Ú<module>   s    