)Ej)rddlmZddlZddlmZmZdZgdZgdZGdd Z Gd d Z dS) ) annotationsN)Image _imagingmorphi) rr) r rrr r rr r rcNeZdZdZ ddd Zdd Zdd Zd dZd!dZd"dZ ddZ dS)# 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() Npatternslist[str] | Noneop_name str | NonereturnNonecd|_|;ddgdgddgdgddggdd }||vrd |d }t||||_dS| ||_dSg|_dS) a :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. N1:(... ... ...)->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 dilation8erosion4erosion8edgezUnknown pattern !)lut Exceptionr)selfrrknown_patternsmsgs @C:\PYTHON\MyICR_Suite\python\Lib\site-packages\PIL/ImageMorph.py__init__zLutBuilder.__init__As&*  /1EF2324HI1213GH  Nn,,3333nn$*73DMMM  !$DMMMDMMM list[str]c&|xj|z c_dS)z\ Append to list of patterns. :param patterns: Additional patterns. N)r)r!rs r$ add_patternszLutBuilder.add_patternsbs ! r& bytearraycddgdtfdttD|_|jS)z Set the current LUT, and return it. This is the default LUT that patterns will be applied against when building. rrc36K|]}|zdkVdS)rN).0imsymbolss r$ z/LutBuilder.build_default_lut..rs0KKaWa!eq[1KKKKKKr&)r*rangeLUT_SIZEr)r!r1r2s @@r$build_default_lutzLutBuilder.build_default_lutjsH a& KKKKK5??KKKKKxr&bytearray | Nonec|jS)z) Returns the current LUT r)r!s r$get_lutzLutBuilder.get_lutus xr&patternstr permutation list[int]cpt|dksJdfd|DS)zuTakes a pattern and a permutation and returns the string permuted according to the permutation list. c3(K|] }|V dS)Nr.)r/pr;s r$r3z-LutBuilder._string_permute..s'77awqz777777r&)lenjoin)r!r;r=s ` r$_string_permutezLutBuilder._string_permute{sE;1$$$$ww7777;777777r& basic_patternoptions basic_resultintlist[tuple[str, int]]c||fg}d|vr\|dd}tdD]>}|||ddt|f?d|vrOt |}|d|D]5\}}|||t |f6d|vrt |}|d|D]l\}}|d d d d d d }dt|z }|||fm|S) zTakes 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.4rr rMNN0Z1)r4appendrFROTATION_MATRIXrD MIRROR_MATRIXreplacerJ) r!rGrHrIrresr0nr;s r$_pattern_permutezLutBuilder._pattern_permutesh #L12 '>>2,q/C1XX  ))(2,q/?KKSQ '>>H A (!  U U !5!5g}!M!Ms STTTT '>>H A (!  0 0 !//#s33;;CEEMMcSVWW#c((l#////r&c||jJg}|jD]}tjd|dd}|sd|zdz}t ||d}|d}t|d }|d ddd}|| |||z }g}|D]a}|d d d d d}| tj ||dfbttD]i} t| dd} ddt| z z| zddd} |D],\}} || rd dg| |j| <-j|jS)zoCompile all patterns into a morphology LUT, and return it. This is the data to be passed into MorphOp.Nz(\w):?\s*\((.+?)\)\s*->\s*(\d) rAzSyntax error in pattern ""rr r r.Xz[01]rQr@rN)r6rrresearchrWr grouprJrZrTcompiler4r5binrDmatch) r!rrCr1r#rHr;resultcompiled_patternsr0 bitpatternrs r$ build_lutzLutBuilder.build_luts    x### H HA ;QYYtR=P=PQQA %1A5;nn$ggajjGggajjG__Fooc2..66tR@@G --gwGG GHH B BG ""3,,44S&AAA  $ $bjmmWQZ%@ A A A Ax , ,AQJS__!45 BDDbDIJ/ , , ==,,,#$a&)DHQK ,xr&)NN)rrrrrr)rr'rr)rr*)rr7)r;r<r=r>rr<)rGr<rHr<rIrJrrK) __name__ __module__ __qualname____doc__r%r)r6r:rFrZrkr.r&r$rrs!!HHLB""""     8888@((((((r&rcReZdZdZ ddd ZddZddZddZddZddZ ddZ dS)MorphOpz*A class for binary morphological operatorsNrr7rrrrrrcn| | ||_dSt|||_dS)aCreate 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)rrrk)r!rrrs r$r%zMorphOp.__init__s:"  DHHH!(G44>>@@DHHHr&image Image.Imagetuple[int, Image.Image]cD|jd}t||jdvrd}t|t j|j|j}tjt|j| | }||fS)a9Run 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.NNo operator loadedrSLImage mode must be 1 or L) rr mode ValueErrorrnewsizerapplybytesgetim)r!rsr#outimagecounts r$rz MorphOp.applys 8 &CC.. :Z ' '-CS// !9UZ44#E$(OOU[[]]HNNDTDTUUhr&list[tuple[int, int]]c|jd}t||jdvrd}t|t jt |j|S)axGet 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.Nrwrxrz)rr r{r|rrfrrr!rsr#s r$rfz MorphOp.matchs^ 8 &CC.. :Z ' '-CS// !"5??EKKMMBBBr&c|jdvrd}t|tj|S)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.rxrz)r{r|r get_on_pixelsrrs r$rzMorphOp.get_on_pixels s; :Z ' '-CS// !*5;;==999r&filenamer<c t|d5}t||_dddn #1swxYwYt |jt krd|_d}t |dS)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. rbNzWrong size operator file!)openr*readrrDr5r )r!rfr#s r$load_lutzMorphOp.load_luts(D ! ! +Q **DH + + + + + + + + + + + + + + + tx==H $ $DH-CC..  % $s'AA Ac|jd}t|t|d5}||jddddS#1swxYwYdS)z Save an operator to an mrl file. :param filename: The destination file. :exception Exception: If the current operator is None. Nrwwb)rr rwrite)r!rr#rs r$save_lutzMorphOp.save_lut*s 8 &CC.. (D ! ! Q GGDH                     sAAAc||_dS)zU Set the LUT from an external source :param lut: A new LUT. Nr9)r!rs r$set_lutzMorphOp.set_lut7s r&)NNN)rr7rrrrrr)rsrtrru)rsrtrr)rr<rr)rr7rr) rlrmrnror%rrfrrrrr.r&r$rqrqs44!%"%) AAAAA,(CCCC& : : : : ! ! ! !    r&rq) __future__rrarArrr5rUrVrrqr.r&r$rs#""""" """"""""   mmmmmmmm`ppppppppppr&