U
    ?hk!                  	   @  st   d dl mZ d dlZddlmZmZ dZddd dd	dd
ddg	Zddd dd	dd
ddg	ZG dd dZ	G dd dZ
dS )    )annotationsN   )Image_imagingmorphi                        c                   @  s   e Zd ZdZd ddddddZd	dd
ddZddddZddddZddddddZdddddddZ	ddddZ
dS )!
LutBuilderaT  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()

    Nlist[str] | None
str | NoneNone)patternsop_namereturnc                 C  sv   |d k	r|| _ ng | _ d | _|d k	rrddgdgddgdgddgdddgd	}||krhd
| d}t||| | _ d S )Nz1:(... ... ...)->0z4:(00. 01. ...)->1z4:(... .0. .1.)->1z4:(... .0. ..1)->1z4:(... .1. .0.)->0z4:(... .1. ..0)->0z4:(.0. .1. ...)->1z4:(01. .1. ...)->1)ZcornerZ	dilation4Z	dilation8Zerosion4Zerosion8edgezUnknown pattern !)r   lut	Exception)selfr   r   Zknown_patternsmsg r   @/var/www/html/venv/lib/python3.8/site-packages/PIL/ImageMorph.py__init__A   s&    zLutBuilder.__init__z	list[str])r   r   c                 C  s   |  j |7  _ d S Nr   )r   r   r   r   r   add_patterns\   s    zLutBuilder.add_patterns)r   c                   s.   ddgd t  fddttD | _d S )Nr   r      c                 3  s   | ]}| @ d k V  qdS )r   Nr   ).0imsymbolsr   r   	<genexpr>b   s     z/LutBuilder.build_default_lut.<locals>.<genexpr>)	bytearrayrangeLUT_SIZEr   r   r   r#   r   build_default_lut_   s    zLutBuilder.build_default_lutbytearray | Nonec                 C  s   | j S r   r   r*   r   r   r   get_lutd   s    zLutBuilder.get_lutstrz	list[int])patternpermutationr   c                   s(   t |dkstd fdd|D S )zstring_permute takes a pattern and a permutation and returns the
        string permuted according to the permutation list.
        	    c                 3  s   | ]} | V  qd S r   r   )r!   pr0   r   r   r&   l   s     z-LutBuilder._string_permute.<locals>.<genexpr>)lenAssertionErrorjoin)r   r0   r1   r   r5   r   _string_permuteg   s    zLutBuilder._string_permuteintzlist[tuple[str, int]])basic_patternoptionsbasic_resultr   c           	      C  s   ||fg}d|krJ|d d }t dD ]"}|| |d d t|f q&d|krt|}|d| D ]\}}|| |t|f qfd|krt|}|d| D ]>\}}|d	d
dd	d
d}dt| }|||f q|S )zpattern_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.4r   r	   r   MNN0Z1)r(   appendr9   ROTATION_MATRIXr6   MIRROR_MATRIXreplacer:   )	r   r;   r<   r=   r   resr"   nr0   r   r   r   _pattern_permuten   s$    
zLutBuilder._pattern_permuter'   c                 C  sH  |    | jdk	stg }| jD ]x}td|dd}|sPd| d }t||d}|d}t	|d	}|d
ddd}|| 
|||7 }q g }|D ]4}|d dddd}|t||d f qttD ]`}	t|	dd }
ddt|
  |
 ddd }
|D ](\}}||
rddg| | j|	< qq| jS )zlCompile all patterns into a morphology lut.

        TBD :Build based on (file) morphlut:modify_lut
        Nz(\w*):?\s*\((.+?)\)\s*->\s*(\d)
r3   zSyntax error in pattern ""r   r   r    r   .Xz[01]rB   r2   r?   )r+   r   r7   r   researchrH   r   groupr:   rK   rE   compiler(   r)   binr6   match)r   r   r4   r$   r   r<   r0   resultZcompiled_patternsr"   Z
bitpatternrr   r   r   	build_lut   s0    


zLutBuilder.build_lut)NN)__name__
__module____qualname____doc__r   r   r+   r.   r9   rK   rY   r   r   r   r   r      s   $    r   c                   @  s   e Zd ZdZdddddddd	Zd
ddddZd
ddddZd
ddddZdddddZdddddZ	dddddZ
dS )MorphOpz*A class for binary morphological operatorsNr,   r   r   r   )r   r   r   r   c                 C  s<   || _ |dk	r t|d | _ n|dk	r8t|d | _ dS )z&Create a binary morphological operatorN)r   r   )r   r   rY   )r   r   r   r   r   r   r   r      s
    zMorphOp.__init__zImage.Imageztuple[int, Image.Image])imager   c                 C  sb   | j dkrd}t||jdkr,d}t|t|j|jd}tt	| j |j
j|j
j}||fS )zRun 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)r   r   mode
ValueErrorr   newsizer   applybytesimid)r   r_   r   Zoutimagecountr   r   r   rg      s    

zMorphOp.applyzlist[tuple[int, int]]c                 C  sB   | j dkrd}t||jdkr,d}t|tt| j |jjS )zGet 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`.Nr`   ra   rb   )	r   r   rc   rd   r   rV   rh   ri   rj   r   r_   r   r   r   r   rV      s    

zMorphOp.matchc                 C  s$   |j dkrd}t|t|jjS )zGet 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`.ra   rb   )rc   rd   r   get_on_pixelsri   rj   rl   r   r   r   rm      s    
zMorphOp.get_on_pixelsr/   )filenamer   c              	   C  sH   t |d}t| | _W 5 Q R X t| jtkrDd| _d}t|dS )z!Load an operator from an mrl filerbNzWrong size operator file!)openr'   readr   r6   r)   r   )r   rn   fr   r   r   r   load_lut   s    zMorphOp.load_lutc              	   C  s<   | j dkrd}t|t|d}|| j  W 5 Q R X dS )zSave an operator to an mrl fileNr`   wb)r   r   rp   write)r   rn   r   rr   r   r   r   save_lut   s
    
zMorphOp.save_lut)r   r   c                 C  s
   || _ dS )z#Set the lut from an external sourceNr-   )r   r   r   r   r   set_lut  s    zMorphOp.set_lut)NNN)rZ   r[   r\   r]   r   rg   rV   rm   rs   rv   rw   r   r   r   r   r^      s      
r^   )
__future__r   rQ   r3   r   r   r)   rF   rG   r   r^   r   r   r   r   <module>   s4                