a :jg@sBddlmZddlZddlmZddlmZGdddZeZdS)) annotationsN)cached_property)Imagec@seZdZdddddddZedd d d Zed d d dZedd ddZedd ddZedd ddZ ed d ddZ edd ddZ edd ddZ edd ddZ dS)StatNzImage.Image | list[int]zImage.Image | NoneNone) image_or_listmaskreturncCsTt|tjr|||_nt|tr,||_n d}t|ttt|jd|_dS)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 listN) isinstancer histogramhlist TypeErrorrangelenbands)selfrr msgr7/usr/local/lib/python3.9/site-packages/PIL/ImageStat.py__init__ s  z Stat.__init__zlist[tuple[int, int]])r cs2dddddfddtdtjd DS) 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]ztuple[int, int])r r cSsPd\}}tdD]}||r|}q&qtdddD]}||r2|}qHq2||fS)N)rr r)r)r Zres_minZres_maxirrrminmaxMs zStat.extrema..minmaxcsg|]}j|dqS)N)r.0rrrrr Yz Stat.extrema..rr rrrrrr rextrema>s z Stat.extremarcs fddtdtjdDS)z2Total number of pixels for each band in the image.cs"g|]}tj||dqS)r )sumrrr$rrr!^r"zStat.count..rr r#r$rr$rcount[sz Stat.countz list[float]cCsRg}tdt|jdD]6}d}tdD]}|||j||7}q&||q|S)z-Sum of all pixels for each band in the image.rr )rrrappend)rvrZ layer_sumjrrrr&`s  zStat.sumcCsZg}tdt|jdD]>}d}tdD]"}||dt|j||7}q&||q|S)z5Squared sum of all pixels for each band in the image.rr r()rrrfloatr))rr*rsum2r+rrrr.ls   z Stat.sum2csfddjDS)zAAverage (arithmetic mean) pixel level for each band in the image.cs g|]}j|j|qSr)r&r'rr$rrr!{r"zStat.mean..rr$rr$rmeanxsz Stat.meancCsdg}|jD]T}d}|j|d}|d}tdD]"}||j||}||kr0qTq0||q |S)z.Median pixel level for each band in the image.rr,r )rr'rrr))rr*rsZhalfbr+rrrmedian}s   z Stat.mediancsfddjDS)z2RMS (root-mean-square) for each band in the image.cs&g|]}tj|j|qSr)mathsqrtr.r'rr$rrr!r"zStat.rms..r/r$rr$rrmsszStat.rmscsfddjDS)z$Variance for each band in the image.cs8g|]0}j|j|dj|j|qS)g@)r.r&r'rr$rrr!szStat.var..r/r$rr$rvars zStat.varcsfddjDS)z.Standard deviation for each band in the image.csg|]}tj|qSr)r4r5r7rr$rrr!r"zStat.stddev..r/r$rr$rstddevsz Stat.stddev)N)__name__ __module__ __qualname__rrr%r'r&r.r0r3r6r7r8rrrrrs(  r) __future__rr4 functoolsrrrGlobalrrrrs