ó
    ©Rj±)  ã                  ód   • S SK Jr  S SKrSSKJrJr  Sr/ SQr/ SQr " S S	5      r	 " S
 S5      r
g)é    )ÚannotationsNé   )ÚImageÚ_imagingmorphi   )	é   é   r   é   é   r   é   é   é   )	r   r   r   r   r
   r   r   r	   r   c                  ó~   • \ rS rSrSr S     SS jjrSS jrSS jrSS jrSS jr	        SS	 jr
SS
 jrSrg)Ú
LutBuilderé   aü  A class for building a MorphLut from a descriptive language

The input patterns is a list of a strings sequences like these::

    4:(...
       .1.
       111)->1

(whitespaces including linebreaks are ignored). The option 4
describes a series of symmetry operations (in this case a
4-rotation), the pattern is described by:

- . or X - Ignore
- 1 - Pixel is on
- 0 - Pixel is off

The result of the operation is described after "->" string.

The default is to return the current pixel value, which is
returned if no other match is found.

Operations:

- 4 - 4 way rotation
- N - Negate
- 1 - Dummy op for no other operation (an op must always be given)
- M - Mirroring

Example::

    lb = LutBuilder(patterns = ["4:(... .1. 111)->1"])
    lut = lb.build_lut()

Nc                ó    • SU l         Ub3  SS/S/SS/S/SS// SQS	.nX#;  a  S
U S3n[        U5      eX2   U l        gUb  Xl        g/ U l        g)zê
:param patterns: A list of input patterns, or None.
:param op_name: The name of a known pattern. One of "corner", "dilation4",
   "dilation8", "erosion4", "erosion8" or "edge".
:exception Exception: If the op_name is not recognized.
Nú1:(... ... ...)->0z4:(00. 01. ...)->1z4:(... .0. .1.)->1z4:(... .0. ..1)->1z4:(... .1. .0.)->0z4:(... .1. ..0)->0)r   z4:(.0. .1. ...)->1z4:(01. .1. ...)->1)ÚcornerÚ	dilation4Ú	dilation8Úerosion4Úerosion8ÚedgezUnknown pattern Ú!)ÚlutÚ	ExceptionÚpatterns)Úselfr   Úop_nameÚknown_patternsÚmsgs        Ú:D:\diplom_project\venv\Lib\site-packages\PIL/ImageMorph.pyÚ__init__ÚLutBuilder.__init__A   s€   € ð &*ˆŒØÑà/Ð1EÐFØ2Ð3Ø2Ð4HÐIØ1Ð2Ø1Ð3GÐHòñˆNð Ó,Ø(¨¨	°Ð3�Ü “nÐ$à*Ñ3ˆD�MØÑ!Ø$�MàˆD�Mó    c                ó.   • U =R                   U-  sl         g)zD
Append to list of patterns.

:param patterns: Additional patterns.
N)r   )r   r   s     r!   Úadd_patternsÚLutBuilder.add_patternsb   s   € ð 	�Š˜Ñ!Žr$   c                óz   ^^• SS/mSm[        UU4S j[        [        5       5       5      U l        U R                  $ )zs
Set the current LUT, and return it.

This is the default LUT that patterns will be applied against when building.
r   r   é   c              3  ó:   >#   • U  H  nTUT-  S :„     v •  M     g7f)r   N© )Ú.0ÚiÚmÚsymbolss     €€r!   Ú	<genexpr>Ú/LutBuilder.build_default_lut.<locals>.<genexpr>r   s   øé € ÐKº?°a˜W a¨!¡e¨q¡[Ö1º?ùs   ƒ)Ú	bytearrayÚrangeÚLUT_SIZEr   )r   r.   r/   s    @@r!   Úbuild_default_lutÚLutBuilder.build_default_lutj   s2   ù€ ð �a�&ˆØˆÜÕK¼5Ä¼?ÓKÓKˆŒØ�x‰xˆr$   c                ó   • U R                   $ )z
Returns the current LUT
©r   )r   s    r!   Úget_lutÚLutBuilder.get_lutu   s   € ð �x‰xˆr$   c                ó\   ^• [        U5      S:X  d   eSR                  U4S jU 5       5      $ )zeTakes a pattern and a permutation and returns the
string permuted according to the permutation list.
é	   Ú c              3  ó.   >#   • U  H
  nTU   v •  M     g 7f)Nr+   )r,   ÚpÚpatterns     €r!   r0   Ú-LutBuilder._string_permute.<locals>.<genexpr>€   s   øé € Ð7ª; a�w˜q–zª;ùs   ƒ)ÚlenÚjoin)r   r@   Úpermutations    ` r!   Ú_string_permuteÚLutBuilder._string_permute{   s-   ø€ ô �;Ó 1Ó$Ð$Ð$Ø�w‰wÔ7©;Ó7Ó7Ð7r$   c                ó  • X4/nSU;   aG  US   S   n[        S5       H0  nUR                  U R                  US   S   [        5      U45        M2     SU;   a@  [	        U5      nUSU  H,  u  p…UR                  U R                  U[
        5      U45        M.     SU;   ak  [	        U5      nUSU  HW  u  p…UR                  S	S
5      R                  SS	5      R                  S
S5      nS[        U5      -
  nUR                  X…45        MY     U$ )z©Takes a basic pattern and its result and clones
the pattern according to the modifications described in the $options
parameter. It returns a list of all cloned patterns.Ú4éÿÿÿÿr   r
   r   ÚMNÚNÚ0ÚZÚ1)r3   ÚappendrE   ÚROTATION_MATRIXrB   ÚMIRROR_MATRIXÚreplaceÚint)	r   Úbasic_patternÚoptionsÚbasic_resultr   Úresr-   Únr@   s	            r!   Ú_pattern_permuteÚLutBuilder._pattern_permute‚   s  € ð #Ð1Ð2ˆð �'‹>Ø˜2‘,˜q‘/ˆCÜ˜1–X�Ø—‘Ø×)Ñ)¨(°2©,°q©/¼?ÓKÈSÐQöñ ð
 �'‹>Ü�H“ˆAØ (¨¨!£‘�Ø—‘ ×!5Ñ!5°g¼}Ó!MÈsÐ SÖTñ !-ð �'‹>Ü�H“ˆAØ (¨¨!£‘�à!Ÿ/™/¨#¨sÓ3×;Ñ;¸CÀÓE×MÑMÈcÐSVÓW�Øœ#˜c›(‘l�Ø—‘  Ö/ñ	 !-ð ˆr$   c                ó†  • U R                  5         U R                  c   e/ nU R                   H¶  n[        R                  " SUR                  SS5      5      nU(       d  SU-   S-   n[        U5      eUR                  S5      nUR                  S5      n[        UR                  S	5      5      nUR                  S
S5      R                  SS5      nXR                  XeU5      -  nM¸     / nU HR  nUS   R                  SS5      R                  SS5      nUR                  [        R                  " U5      US   45        MT     [        [        5       Hb  n	[        U	5      SS n
SS[        U
5      -
  -  U
-   SSS2   n
U H1  u  pkUR!                  U
5      (       d  M  SS/U   U R                  U	'   M3     Md     U R                  $ )zgCompile all patterns into a morphology LUT, and return it.

This is the data to be passed into MorphOp.Nz(\w):?\s*\((.+?)\)\s*->\s*(\d)Ú
r=   zSyntax error in pattern "Ú"r   r   r   Ú r   Ú.ÚXz[01]rL   r<   rI   )r5   r   r   ÚreÚsearchrR   r   ÚgrouprS   rY   rO   Úcompiler3   r4   ÚbinrB   Úmatch)r   r   r?   r.   r    rU   r@   ÚresultÚcompiled_patternsr-   Ú
bitpatternÚrs               r!   Ú	build_lutÚLutBuilder.build_lut¢   s›  € ð 	×ÑÔ Ø�x‰xÑ#Ð#Ð#Øˆð —”ˆAÜ—	’	Ð;¸Q¿Y¹YÀtÈRÓ=PÓQˆAÞØ1°AÑ5¸Ñ;�Ü “nÐ$Ø—g‘g˜a“jˆGØ—g‘g˜a“jˆGÜ˜Ÿ™ ›“_ˆFð —o‘o c¨2Ó.×6Ñ6°t¸RÓ@ˆGà×-Ñ-¨gÀÓGÑGŠHñ ð ÐÛˆGØ˜‘
×"Ñ" 3¨Ó,×4Ñ4°S¸&ÓAˆAØ×$Ñ$¤b§j¢j°£m°W¸Q±ZÐ%@ÖAñ  ô ”x–ˆAä˜Q›  ˜ˆJØ ¤S¨£_Ñ!4Ñ5¸
ÑBÁDÀbÀDÑIˆJã/‘
�Ø—=‘= ×,Ó,Ø#$ a &¨¡)�D—H‘H˜Q“Kó 0ñ !ð �x‰xˆr$   )r   r   )NN)r   úlist[str] | Noner   ú
str | NoneÚreturnÚNone)r   z	list[str]ro   rp   )ro   r2   )ro   úbytearray | None)r@   ÚstrrD   z	list[int]ro   rr   )rT   rr   rU   rr   rV   rS   ro   zlist[tuple[str, int]])Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r"   r&   r5   r9   rE   rY   rk   Ú__static_attributes__r+   r$   r!   r   r      sl   † ñ!ðH HLðØ(ðØ:Dðà	õôB"ô	ôô8ðØ ðØ+.ðØ>Aðà	ô÷@(r$   r   c                  óv   • \ rS rSrSr   S       SS jjrSS jrSS jrSS jrSS jr	SS	 jr
SS
 jrSrg)ÚMorphOpéÍ   z*A class for binary morphological operatorsNc                óZ   • Uc
  Uc  Xl         g[        X25      R                  5       U l         g)aˆ  Create a binary morphological operator.

If the LUT is not provided, then it is built using LutBuilder from the op_name
or the patterns.

:param lut: The LUT data.
:param patterns: A list of input patterns, or None.
:param op_name: The name of a known pattern. One of "corner", "dilation4",
"dilation8", "erosion4", "erosion8", "edge".
:exception Exception: If the op_name is not recognized.
N)r   r   rk   )r   r   r   r   s       r!   r"   ÚMorphOp.__init__Ð   s(   € ð" Ñ ¡Ø�Hä! (Ó4×>Ñ>Ó@ˆD�Hr$   c                óZ  • U R                   c  Sn[        U5      eUR                  S;  a  Sn[        U5      e[        R
                  " UR                  UR                  5      n[        R                  " [        U R                   5      UR                  5       UR                  5       5      nXC4$ )a  Run a single morphological operation on an image.

Returns a tuple of the number of changed pixels and the
morphed image.

:param image: A 1-mode or L-mode image.
:exception Exception: If the current operator is None.
:exception ValueError: If the image is not 1 or L mode.úNo operator loaded©rN   ÚLúImage mode must be 1 or L)r   r   ÚmodeÚ
ValueErrorr   ÚnewÚsizer   ÚapplyÚbytesÚgetim)r   Úimager    ÚoutimageÚcounts        r!   r‡   ÚMorphOp.applyæ   s�   € ð �8‰8ÑØ&ˆCÜ˜C“.Ð à�:‰:˜ZÓ'Ø-ˆCÜ˜S“/Ð!Ü—9’9˜UŸZ™Z¨¯©Ó4ˆÜ×#Ò#¤E¨$¯(©(£O°U·[±[³]ÀHÇNÁNÓDTÓUˆØˆÐr$   c                óà   • U R                   c  Sn[        U5      eUR                  S;  a  Sn[        U5      e[        R
                  " [        U R                   5      UR                  5       5      $ )aH  Get a list of coordinates matching the morphological operation on
an image.

Returns a list of tuples of (x,y) coordinates of all matching pixels. See
:ref:`coordinate-system`.

:param image: A 1-mode or L-mode image.
:exception Exception: If the current operator is None.
:exception ValueError: If the image is not 1 or L mode.r   r€   r‚   )r   r   rƒ   r„   r   rf   rˆ   r‰   ©r   rŠ   r    s      r!   rf   ÚMorphOp.matchú   sY   € ð �8‰8ÑØ&ˆCÜ˜C“.Ð à�:‰:˜ZÓ'Ø-ˆCÜ˜S“/Ð!Ü×"Ò"¤5¨¯©£?°E·K±K³MÓBÐBr$   c                ó„   • UR                   S;  a  Sn[        U5      e[        R                  " UR	                  5       5      $ )a  Get a list of all turned on pixels in a 1 or L mode image.

Returns a list of tuples of (x,y) coordinates of all non-empty pixels. See
:ref:`coordinate-system`.

:param image: A 1-mode or L-mode image.
:exception ValueError: If the image is not 1 or L mode.r€   r‚   )rƒ   r„   r   Úget_on_pixelsr‰   r�   s      r!   r’   ÚMorphOp.get_on_pixels  s6   € ð �:‰:˜ZÓ'Ø-ˆCÜ˜S“/Ð!Ü×*Ò*¨5¯;©;«=Ó9Ð9r$   c                óî   • [        US5       n[        UR                  5       5      U l        SSS5        [	        U R                  5      [
        :w  a  SU l        Sn[        U5      eg! , (       d  f       N@= f)z�
Load an operator from an mrl file

:param filename: The file to read from.
:exception Exception: If the length of the file data is not 512.
ÚrbNzWrong size operator file!)Úopenr2   Úreadr   rB   r4   r   )r   ÚfilenameÚfr    s       r!   Úload_lutÚMorphOp.load_lut  s]   € ô �(˜DÔ! QÜ  §¡£Ó*ˆDŒH÷ "ô ˆt�x‰x‹=œHÓ$ØˆDŒHØ-ˆCÜ˜C“.Ð ð %÷ "Õ!ús   �A&Á&
A4c                óº   • U R                   c  Sn[        U5      e[        US5       nUR                  U R                   5        SSS5        g! , (       d  f       g= f)z�
Save an operator to an mrl file.

:param filename: The destination file.
:exception Exception: If the current operator is None.
Nr   Úwb)r   r   r–   Úwrite)r   r˜   r    r™   s       r!   Úsave_lutÚMorphOp.save_lut*  sE   € ð �8‰8ÑØ&ˆCÜ˜C“.Ð Ü�(˜DÔ! QØ�G‰G�D—H‘HÔ÷ "×!Ö!ús   §AÁ
Ac                ó   • Xl         g)z=
Set the LUT from an external source

:param lut: A new LUT.
Nr8   )r   r   s     r!   Úset_lutÚMorphOp.set_lut7  s	   € ð �r$   r8   )NNN)r   rq   r   rn   r   rm   ro   rp   )rŠ   úImage.Imagero   ztuple[int, Image.Image])rŠ   r¤   ro   zlist[tuple[int, int]])r˜   rr   ro   rp   )r   rq   ro   rp   )rs   rt   ru   rv   rw   r"   r‡   rf   r’   rš   rŸ   r¢   rx   r+   r$   r!   rz   rz   Í   s_   † Ù4ð !%Ø"Ø%)ð	AàðAð ðAð #ð	Að
 
õAô,ô(Cô&:ô!ô÷r$   rz   )Ú
__future__r   ra   r=   r   r   r4   rP   rQ   r   rz   r+   r$   r!   Ú<module>r¦      s<   ðõ #ã 	ç "à€ò€ò
€÷mñ m÷`pò pr$   