U
    ڲg                     @  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   1/tmp/pip-unpacked-wheel-xela2va7/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   ZGlobalr   r   r   r   <module>   s    