%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/PIL/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/PIL/__pycache__/ImageMorph.cpython-312.pyc

�

��eL��d�ddlmZddlZddlmZmZdZgd�Zgd�ZGd�d	�Z	Gd
�d�Z
y)�)�annotationsN�)�Image�
_imagingmorphi)	��r��r���)	r
rrrr
rrr	rc�<�eZdZdZd
d�Zd�Zd�Zd�Zd�Zd�Z	d	�Z
y)�
LutBuilderaTA 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��|�||_ng|_d|_|�5ddgdgddgdgddggd�d�}||vrd	|zd
z}t|��|||_yy)N�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 �!)�patterns�lut�	Exception)�selfr�op_name�known_patterns�msgs     �0/usr/lib/python3/dist-packages/PIL/ImageMorph.py�__init__zLutBuilder.__init__As�����$�D�M��D�M������/�1E�F�2�3�2�4H�I�1�2�1�3G�H��
�N��n�,�(�7�2�S�8����n�$�*�7�3�D�M�#�c�.�|xj|z
c_y�N�r)rrs  r �add_patternszLutBuilder.add_patternsZs���
�
��!�
r"c�b���ddg�d�t��fd�tt�D��|_y)Nrr�c3�4�K�|]}�|�zdkD���y�w)rN�)�.0�i�m�symbolss  ��r �	<genexpr>z/LutBuilder.build_default_lut.<locals>.<genexpr>`s�����K�a�W�a�!�e�q�[�1�K�s�)�	bytearray�range�LUT_SIZEr)rr-r.s @@r �build_default_lutzLutBuilder.build_default_lut]s'����a�&�����K�5��?�K�K��r"c��|jSr$�r)rs r �get_lutzLutBuilder.get_lutbs���x�x�r"c�X��t|�dk(sJ�dj�fd�|D��S)z�string_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        �	�c3�(�K�|]	}�|���y�wr$r*)r+�p�patterns  �r r/z-LutBuilder._string_permute.<locals>.<genexpr>js�����7�a�w�q�z�7�s�)�len�join)rr<�permutations ` r �_string_permutezLutBuilder._string_permutees-����;��1�$�$�$��w�w�7�;�7�7�7r"c��||fg}d|vrE|dd}td�D]/}|j|j|ddt�|f��1d|vr?t	|�}|d|D],\}}|j|j|t
�|f��.d|vrkt	|�}|d|D]X\}}|j
d	d
�j
dd	�j
d
d�}dt|�z
}|j||f��Z|S)z�pattern_permute 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���rr
r�MN�N�0�Z�1)r1�appendr@�ROTATION_MATRIXr=�
MIRROR_MATRIX�replace�int)	r�
basic_pattern�options�basic_resultr�resr,�nr<s	         r �_pattern_permutezLutBuilder._pattern_permutels(��#�L�1�2���'�>��2�,�q�/�C��1�X�
������)�)�(�2�,�q�/�?�K�S�Q��
�
�'�>��H�
�A� (��!��
U��������!5�!5�g�}�!M�s� S�T�
U��'�>��H�
�A� (��!��
0����!�/�/�#�s�3�;�;�C��E�M�M�c�SV�W���#�c�(�l������#��/�	
0��r"c�D�|j�g}|jD]�}tjd|j	dd��}|sd|zdz}t|��|j
d�}|j
d�}t|j
d��}|j	d	d�j	dd�}||j|||�z
}��t|�D]I\}}|d
j	dd�j	dd
�}tj|�}||df||<�Ktt�D]Z}t|�dd}	ddt|	�z
z|	zddd�}	|D]+\}}
|j|	�s�d
dg|
|j |<�-�\|j S)zlCompile all patterns into a morphology lut.

        TBD :Build based on (file) morphlut:modify_lut
        z(\w*):?\s*\((.+?)\)\s*->\s*(\d)�
r9zSyntax error in pattern "�"rr
r� r�.�Xz[01]NrFr8rC)r3r�re�searchrLr�grouprMrS�	enumerate�compiler1r2�binr=�matchr)rrr;r-rrOr<�resultr,�
bitpattern�rs           r �	build_lutzLutBuilder.build_lut�s���
	
��� ������	H�A��	�	�<�a�i�i��b�>Q�R�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�w��G�G�H�	H�$�H�-�	*�J�A�w���
�"�"�3��,�4�4�S�&�A�A��
�
�1�
�A��g�a�j�/�H�Q�K�	*��x��	,�A��Q�����J���S��_�!4�5�
�B�D�b�D�I�J� �
,���1��7�7�:�&�#$�a�&��)�D�H�H�Q�K�
,�	,��x�x�r")NN)�__name__�
__module__�__qualname__�__doc__r!r&r3r6r@rSrdr*r"r rrs,��!�F4�2"�L�
�8��<)r"rc�<�eZdZdZd
d�Zd�Zd�Zd�Zd�Zd�Z	d	�Z
y)�MorphOpz*A class for binary morphological operatorsNc��||_|� t|��j�|_y|� t|��j�|_yy)z&Create a binary morphological operatorN)rr%)rrrd)rrrrs    r r!zMorphOp.__init__�sD�������!�'�2�<�<�>�D�H�
�
!�!�8�4�>�>�@�D�H�"r"c�n�|j�
d}t|��|jdk7r
d}t|��t	j
|j|jd�}tjt|j�|jj|jj�}||fS)z�Run a single morphological operation on an image

        Returns a tuple of the number of changed pixels and the
        morphed imageN�No operator loaded�L�Image mode must be L)rr�mode�
ValueErrorr�new�sizer�apply�bytes�im�id)r�imager�outimage�counts     r rtz
MorphOp.apply�s���
�8�8��&�C��C�.� ��:�:���(�C��S�/�!��9�9�U�Z�Z����T�:���#�#�E�$�(�(�O�U�X�X�[�[�(�+�+�.�.�Q���h��r"c���|j�
d}t|��|jdk7r
d}t|��t	j
t
|j�|jj�S)z�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`.rmrnro)	rrrprqrr`rurvrw�rrxrs   r r`z
MorphOp.match�s[���8�8��&�C��C�.� ��:�:���(�C��S�/�!��"�"�5����?�E�H�H�K�K�@�@r"c��|jdk7r
d}t|��tj|jj
�S)z�Get a list of all turned on pixels in a binary image

        Returns a list of tuples of (x,y) coordinates
        of all matching pixels. See :ref:`coordinate-system`.rnro)rprqr�
get_on_pixelsrvrwr|s   r r~zMorphOp.get_on_pixels�s8���:�:���(�C��S�/�!��*�*�5�8�8�;�;�7�7r"c���t|d�5}t|j��|_ddd�t	|j�t
k7rd|_d}t
|��y#1swY�:xYw)z!Load an operator from an mrl file�rbNzWrong size operator file!)�openr0�readrr=r2r)r�filename�frs    r �load_lutzMorphOp.load_lut�sb��
�(�D�
!�	+�Q� �����*�D�H�	+��t�x�x�=�H�$��D�H�-�C��C�.� �%�	+�	+�s�A%�%A.c��|j�
d}t|��t|d�5}|j|j�ddd�y#1swYyxYw)zSave an operator to an mrl fileNrm�wb)rrr��write)rr�rr�s    r �save_lutzMorphOp.save_lut�sL���8�8��&�C��C�.� �
�(�D�
!�	�Q�
�G�G�D�H�H��	�	�	�s�A�Ac��||_y)z#Set the lut from an external sourceNr5)rrs  r �set_lutzMorphOp.set_lut�s	����r")NNN)rerfrgrhr!rtr`r~r�r�r�r*r"r rjrj�s*��4�A�� 
A�	8�!��r"rj)�
__future__rrZr9rrr2rJrKrrjr*r"r �<module>r�s<��#�	�"�����
�
�V�V�rI�Ir"

Zerion Mini Shell 1.0