K iwHddlmZddlZddlmZddlmZGddZeZy)) annotationsN)cached_property)ImageceZdZ d d dZeddZeddZeddZeddZeddZ eddZ edd Z edd Z edd Z y)StatNct|tjr|j||_n%t|tr||_n d}t |t t t|jdz|_y)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)self image_or_listmaskmsgs S/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/PIL/ImageStat.py__init__z Stat.__init__ sb* mU[[ 1",,T2DF  t ,"DF8CC. %DFF s 234 cdd}tdt|jdDcgc]}||j|dc}Scc}w)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. cd\}}tdD] }||s |}ntdddD]}||s |}||fS||fS)N)rr r)r)r res_minres_maxis rminmaxzStat.extrema..minmaxMso% GW3Z Q<G 3B' Q<GG# #  G# #rrr N)r list[int]returnztuple[int, int])rrr )rr!r s rextremaz Stat.extrema>s= $-2!S[#,FGqtvvabz"GGGsAc tdt|jdDcgc]}t|j||dzc}Scc}w)z2Total number of pixels for each band in the image.rr )rrr sumrr s rcountz Stat.count[s=383tvv;2LMQDFF1q3w'(MMMs"Acg}tdt|jdD]=}d}tdD]}|||j||zzz }|j|?|S)z-Sum of all pixels for each band in the image.rr )rrr append)rvr layer_sumjs rr&zStat.sum`so q#dff+s+ AI3Z /QA..  / HHY   rc g}tdt|jdD]I}d}tdD]&}||dzt|j||zzz }(|j |K|S)z5Squared sum of all pixels for each band in the image.rr r*)rrr floatr+)rr,r sum2r.s rr2z Stat.sum2lsw q#dff+s+ AD3Z 6Atvva!e}!555 6 HHTN   rc|jDcgc]2}|j|r|j||j|z nd4c}Scc}w)zAAverage (arithmetic mean) pixel level for each band in the image.r)rr(r&r's rmeanz Stat.meanxsBNRZZXtzz!} djjm+!CXXXs7A cg}|jD]X}d}|j|dz}|dz}tdD]}||j||zz}||kDsn|j Z|S)z.Median pixel level for each band in the image.rr0r )rr(rr r+)rr,r shalfbr.s rmedianz Stat.median}s  AA::a=A%DCA3Z q1u %t8  HHQK rc|jDcgc]E}|j|r2tj|j||j|z ndGc}Scc}w)z2RMS (root-mean-square) for each band in the image.r)rr(mathsqrtr2r's rrmszStat.rmssU ZZ 8rKs'.# %E<E