Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
ImageMorph.cpython-313.pyc142 linesDownload Raw Back to __pycache__
1�

2v�j�)��d�SSKJr SSKrSSKJrJr Sr/SQr/SQr"SS	5r	"S3S5r4g)�)�annotationsN�)�Image�
_imagingmorphi)	��r��r���)	r
rrrr5rrr	rc�~�\rSrSrSrSS
SjjrSSjrSSjrSSjrSSjr	SS	jr6SS7jrSrg)�8LutBuilder�a�A class for building a MorphLut from a descriptive language9 10The input patterns is a list of a strings sequences like these::11 12    4:(...13       .1.14       111)->115 16(whitespaces including linebreaks are ignored). The option 417describes a series of symmetry operations (in this case a184-rotation), the pattern is described by:19 20- . or X - Ignore21- 1 - Pixel is on22- 0 - Pixel is off23 24The result of the operation is described after "->" string.25 26The default is to return the current pixel value, which is27returned if no other match is found.28 29Operations:30 31- 4 - 4 way rotation32- N - Negate33- 1 - Dummy op for no other operation (an op must always be given)34- M - Mirroring35 36Example::37 38    lb = LutBuilder(patterns = ["4:(... .1. 111)->1"])39    lut = lb.build_lut()40 41Nc��SUlUb3SS/S/SS/S/SS//SQS	.nX#;aS42US3n[U5eX2UlgUbXlg/Ulg)z�43:param patterns: A list of input patterns, or None.44:param op_name: The name of a known pattern. One of "corner", "dilation4",45   "dilation8", "erosion4", "erosion8" or "edge".46:exception Exception: If the op_name is not recognized.47N�1:(... ... ...)->0z4:(00. 01. ...)->1z4:(... .0. .1.)->1z4:(... .0. ..1)->1z4:(... .1. .0.)->0z4:(... .1. ..0)->0)rz4:(.0. .1. ...)->1z4:(01. .1. ...)->1)�corner�	dilation4�	dilation8�erosion4�erosion8�edgezUnknown pattern �!)�lut�	Exception�patterns)�selfr�op_name�known_patterns�msgs     �TD:\code\apps\devtools\python\user_packages\Python313\site-packages\PIL/ImageMorph.py�__init__�LutBuilder.__init__As���&*�����/�1E�F�2�3�2�4H�I�1�2�1�3G�H��
�N��,�(��	��3����n�$�*�3�D�M�
�
!�$�M��D�M�c�.�U=RU-
slg)zD48Append to list of patterns.49 50:param patterns: Additional patterns.51N)r)rrs  r!�add_patterns�LutBuilder.add_patternsbs��	
�
�
��!�
r$c�z^^�SS/mSm[UU4Sj[[555UlUR$)zs52Set the current LUT, and return it.53 54This is the default LUT that patterns will be applied against when building.55rr�c3�:># �UHnTUT-S:�v� M g7f)rN�)�.0�i�m�symbolss  ��r!�	<genexpr>�/LutBuilder.build_default_lut.<locals>.<genexpr>rs����K�?�a�W�a�!�e�q�[�1�?�s�)�	bytearray�range�LUT_SIZEr)rr.r/s @@r!�build_default_lut�LutBuilder.build_default_lutjs2����a�&�����K�5��?�K�K����x�x�r$c��UR$)z56Returns the current LUT57�r)rs r!�get_lut�LutBuilder.get_lutus
���x�x�r$c�\^�[U5S:XdeSRU4SjU55$)zeTakes a pattern and a permutation and returns the58string permuted according to the permutation list.59�	�c3�.># �UH60nTUv� M g7f)Nr+)r,�p�patterns  �r!r0�-LutBuilder._string_permute.<locals>.<genexpr>�s����7�;�a�w�q�z�;�s�)�len�join)rr@�permutations ` r!�_string_permute�LutBuilder._string_permute{s-����;��1�$�$�$��w�w�7�;�7�7�7r$c��X4/nSU;aGUSSn[S5H0nURURUSS[5U45 M2 SU;a@[	U5nUSUH,up�URURU[615U45 M. SU;ak[	U5nUSUHWup�UR
S	S625R
SS	5R
S63S5nS[U5-64nURX�45 MY U$)z�Takes a basic pattern and its result and clones65the pattern according to the modifications described in the $options66parameter. It returns a list of all cloned patterns.�4�����rr67r�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���68�'�>��H�
�A� (��!��������!5�!5�g�}�!M�s� S�T�!-��'�>��H�
�A� (��!����!�/�/�#�s�3�;�;�C��E�M�M�c�SV�W���#�c�(�l�������/�	!-��r$c��UR5 URce/nURH�n[R"SURSS55nU(dSU-S-n[
U5eURS5nURS5n[URS	55nURS69S5RSS5nXRXeU5-
nM� /nUHRnUSRSS
5RS
S5nUR[R"U5US45 MT [[5Hbn	[U	5SSn70SS[U715-72-U73-SSS2n74UH1upkUR!U755(dMSS/UURU	'M3 Md UR$)zgCompile all patterns into a morphology LUT, and return it.76 77This is the data to be passed into MorphOp.Nz(\w):?\s*\((.+?)\)\s*->\s*(\d)�78r=zSyntax error in pattern "�"rr
r� r�.�Xz[01]rLr<rI)r5rr�re�searchrRr�grouprSrYrO�compiler3r4�binrB�match)rrr?r.r rUr@�result�compiled_patternsr-�79bitpattern�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���80�"�"�3��,�4�4�S�&�A�A��$�$�b�j�j��m�W�Q�Z�%@�A� ��x��A��Q�����J���S��_�!4�5�81�B�D�b�D�I�J�/�82���=�=��,�,�#$�a�&��)�D�H�H�Q�K�0�!��x�x�r$)rr)NN)r�list[str] | Noner�83str | None�return�None)rz	list[str]rorp)ror2)ro�bytearray | None)r@�strrDz	list[int]rorr)rTrrrUrrrVrSrozlist[tuple[str, int]])
�__name__�84__module__�__qualname__�__firstlineno__�__doc__r"r&r5r9rErYrk�__static_attributes__r+r$r!rrsl��!�HHL��(��:D��	
��B"�	��8�� ��+.��>A��	��@(r$rc�v�\rSrSrSrSS
SjjrSSjrSSjrSSjrSSjr	SS	jr85SS86jrSrg)�MorphOp��z*A class for binary morphological operatorsNc�Z�Uc87UcXlg[X25R5Ulg)a�Create a binary morphological operator.88 89If the LUT is not provided, then it is built using LutBuilder from the op_name90or the patterns.91 92:param lut: The LUT data.93:param patterns: A list of input patterns, or None.94:param op_name: The name of a known pattern. One of "corner", "dilation4",95"dilation8", "erosion4", "erosion8", "edge".96:exception Exception: If the op_name is not recognized.97N)rrrk)rrrrs    r!r"�MorphOp.__init__�s(��"�����H�!�(�4�>�>�@�D�Hr$c�Z�URc
Sn[U5eURS;a
Sn[U5e[R98"URUR5n[R"[UR5UR5UR55nXC4$)aRun a single morphological operation on an image.99 100Returns a tuple of the number of changed pixels and the101morphed image.102 103:param image: A 1-mode or L-mode image.104:exception Exception: If the current operator is None.105:exception ValueError: If the image is not 1 or L mode.�No operator loaded�rN�L�Image mode must be 1 or L)rr�mode�106ValueErrorr�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���URc
Sn[U5eURS;a
Sn[U5e[R107"[
UR5UR55$)aHGet a list of coordinates matching the morphological operation on108an image.109 110Returns a list of tuples of (x,y) coordinates of all matching pixels. See111:ref:`coordinate-system`.112 113:param image: A 1-mode or L-mode image.114:exception Exception: If the current operator is None.115:exception ValueError: If the image is not 1 or L mode.rr�r�)rrr�r�rrfr�r��rr�r s   r!rf�
MorphOp.match�sY���8�8��&�C��C�.� ��:�:�Z�'�-�C��S�/�!��"�"�5����?�E�K�K�M�B�Br$c��URS;a
Sn[U5e[R"UR	55$)aGet a list of all turned on pixels in a 1 or L mode image.116 117Returns a list of tuples of (x,y) coordinates of all non-empty pixels. See118:ref:`coordinate-system`.119 120:param image: A 1-mode or L-mode image.121: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���[US5n[UR55UlSSS5 [	UR5[122:waSUlSn[
U5eg!,(df   N@=f)z�123Load an operator from an mrl file124 125:param filename: The file to read from.126:exception Exception: If the length of the file data is not 512.127�rbNzWrong size operator file!)�openr2�readrrBr4r)r�filename�fr s    r!�load_lut�MorphOp.load_luts]���(�D�
!�Q� �����*�D�H�"��t�x�x�=�H�$��D�H�-�C��C�.� �%�"�
!�s�A&�&128A4c��URc
Sn[U5e[US5nURUR5 SSS5 g!,(df   g=f)z�129Save an operator to an mrl file.130 131:param filename: The destination file.132:exception Exception: If the current operator is None.133Nr�wb)rrr��write)rr�r r�s    r!�save_lut�MorphOp.save_lut*sE���8�8��&�C��C�.� �
�(�D�
!�Q�
�G�G�D�H�H��"�
!�
!�s�A�134Ac��Xlg)z=135Set the LUT from an external source136 137:param lut: A new LUT.138Nr8)rrs  r!�set_lut�MorphOp.set_lut7s	���r$r8)NNN)rrqrrnrrmrorp)r��Image.Imageroztuple[int, Image.Image])r�r�rozlist[tuple[int, int]])r�rrrorp)rrqrorp)
rsrtrurvrwr"r�rfr�r�r�r�rxr+r$r!rzrz�s_��4�!%�"�%)�	A�
�A��A�#�	A�139140�A�,�(C�&:�
!��r$rz)�141__future__rrar=rrr4rPrQrrzr+r$r!�<module>r�s<��#�	�"�����142�
�m�m�`p�pr$
codekingpro/portable-devtools · Team Ai