ó <×ï[c@shddlmZddlmZmZddlZd Zdefd„ƒYZdefd „ƒYZ dS( iÿÿÿÿ(tprint_functioni(tImaget _imagingmorphNi t LutBuildercBsSeZdZddd„Zd„Zd„Zd„Zd„Zd„Z d„Z RS( s~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() cCs¾|dk r||_n g|_d|_|dk rºiddgd6dgd6ddgd6dgd 6dd gd 6dd d gd6}||krªtd|dƒ‚n|||_ndS(Ns1:(... ... ...)->0s4:(00. 01. ...)->1tcorners4:(... .0. .1.)->1t dilation4s4:(... .0. ..1)->1t dilation8s4:(... .1. .0.)->0terosion4s4:(... .1. ..0)->0terosion8s4:(.0. .1. ...)->1s4:(01. .1. ...)->1tedgesUnknown pattern t!(tNonetpatternstlutt Exception(tselfR top_nametknown_patterns((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyt__init__3s(            cCs|j|7_dS(N(R (RR ((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyt add_patternsLscs>ddg‰d‰t‡‡fd†ttƒDƒƒ|_dS(Niiic3s#|]}ˆ|ˆ@dkVqdS(iN((t.0ti(tmtsymbols(s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pys Rsi(t bytearraytrangetLUT_SIZER (R((RRs./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pytbuild_default_lutOs cCs|jS(N(R (R((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pytget_lutTscs5t|ƒdkst‚dj‡fd†|DƒƒS(s„string_permute takes a pattern and a permutation and returns the string permuted according to the permutation list. i tc3s|]}ˆ|VqdS(N((Rtp(tpattern(s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pys \s(tlentAssertionErrortjoin(RRt permutation((Rs./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyt_string_permuteWsc Cs‚||fg}d|kr…|dd}xYtdƒD]H}|j|j|ddddddddd d d g ƒ|fƒq6Wnd |krøt|ƒ}xX|d|!D]F\}}|j|j|d ddd ddd ddg ƒ|fƒq«Wnd |kr~t|ƒ}xk|d|!D]Y\}}|jddƒjddƒjddƒ}dt|ƒ}|j||fƒqWn|S(sÉ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.t4iÿÿÿÿiiiiiiiiitMtNt0tZt1(RtappendR$R treplacetint( Rt basic_patterntoptionst basic_resultR tresRtnR((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyt_pattern_permute^s2            c Cs¾|jƒg}x¶|jD]«}tjd|jddƒƒ}|s[td|dƒ‚n|jdƒ}|jdƒ}t|jdƒƒ}|jd dƒjddƒ}||j|||ƒ7}qWx_t |ƒD]Q\}}|d jd d ƒjd d ƒ}tj |ƒ}||df||\s*(\d)s RsSyntax error in pattern "t"iiit it.tXs[01]R(i Niÿÿÿÿ(RR tretsearchR,RtgroupR-R3t enumeratetcompileRRtbinR tmatchR ( RR RRR/RtresultRt bitpatterntr((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyt build_lutƒs. "%"N( t__name__t __module__t__doc__R RRRRR$R3RB(((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyRs"     %tMorphOpcBsVeZdZdddd„Zd„Zd„Zd„Zd„Zd„Z d„Z RS( s*A class for binary morphological operatorscCs[||_|dk r0td|ƒjƒ|_n'|dk rWtd|ƒjƒ|_ndS(s&Create a binary morphological operatorRR N(R R RRB(RR RR ((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyR²s    cCs‹|jdkrtdƒ‚n|jdkr<tdƒ‚ntj|j|jdƒ}tjt |jƒ|j j |j j ƒ}||fS(s‡Run a single morphological operation on an image Returns a tuple of the number of changed pixels and the morphed imagesNo operator loadedtLs0Image must be binary, meaning it must use mode LN( R R RtmodeRtnewtsizeRtapplytbytestimtid(Rtimagetoutimagetcount((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyRK½s$cCs[|jdkrtdƒ‚n|jdkr<tdƒ‚ntjt|jƒ|jjƒS(sÈ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`.sNo operator loadedRGs0Image must be binary, meaning it must use mode LN( R R RRHRR>RLRMRN(RRO((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyR>Ìs cCs1|jdkrtdƒ‚ntj|jjƒS(s©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`.RGs0Image must be binary, meaning it must use mode L(RHRRt get_on_pixelsRMRN(RRO((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyRRÙscCs^t|dƒ}t|jƒƒ|_WdQXt|jƒtkrZd|_tdƒ‚ndS(s!Load an operator from an mrl filetrbNsWrong size operator file!(topenRtreadR R RR R(Rtfilenametf((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pytload_lutãs  cCsJ|jdkrtdƒ‚nt|dƒ}|j|jƒWdQXdS(sSave an operator to an mrl filesNo operator loadedtwbN(R R RRTtwrite(RRVRW((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pytsave_lutìscCs ||_dS(s#Set the lut from an external sourceN(R (RR ((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pytset_lutósN( RCRDRER RRKR>RRRXR[R\(((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyRF¯s   i( t __future__RRRRR8RtobjectRRF(((s./tmp/pip-build-SKekGm/pillow/PIL/ImageMorph.pyts  Ÿ