o
    6dB                     @   sz  d dl Z G dd dZG dd deZG dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZ	G dd deZ
G dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZdS )0    Nc                   @      e Zd ZdS )FilterN__name__
__module____qualname__ r   r   fC:\Users\jesus\OneDrive\Desktop\erpjis_fastapi\backend\jisbackend\Lib\site-packages\PIL/ImageFilter.pyr          r   c                   @   r   )MultibandFilterNr   r   r   r   r	   r      r
   r   c                   @   s   e Zd Zdd ZdS )BuiltinFilterc                 C   s"   |j dkrd}t||j| j S )NPcannot filter palette images)mode
ValueErrorfilter
filterargsselfimagemsgr   r   r	   r      s   
zBuiltinFilter.filterN)r   r   r   r   r   r   r   r	   r      s    r   c                   @   s   e Zd ZdZd ZdddZdS )Kernela(  
    Create a convolution kernel. The current version only
    supports 3x3 and 5x5 integer and floating point kernels.

    In the current version, kernels can only be applied to
    "L" and "RGB" images.

    :param size: Kernel size, given as (width, height). In the current
                    version, this must be (3,3) or (5,5).
    :param kernel: A sequence containing kernel weights. The kernel will
                   be flipped vertically before being applied to the image.
    :param scale: Scale factor. If given, the result for each pixel is
                    divided by this value. The default is the sum of the
                    kernel weights.
    :param offset: Offset. If given, this value is added to the result,
                    after it has been divided by the scale factor.
    Nr   c                 C   sN   |d u rt dd |}|d |d  t|krd}t|||||f| _d S )Nc                 S   s   | | S Nr   )abr   r   r	   <lambda><   s    z!Kernel.__init__.<locals>.<lambda>r      z!not enough coefficients in kernel)	functoolsreducelenr   r   )r   sizeZkernelscaleoffsetr   r   r   r	   __init__9   s   zKernel.__init__Nr   r   r   r   __doc__namer#   r   r   r   r	   r   $   s    r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )
RankFilterao  
    Create a rank filter.  The rank filter sorts all pixels in
    a window of the given size, and returns the ``rank``'th value.

    :param size: The kernel size, in pixels.
    :param rank: What pixel value to pick.  Use 0 for a min filter,
                 ``size * size / 2`` for a median filter, ``size * size - 1``
                 for a max filter, etc.
    ZRankc                 C   s   || _ || _d S r   r    rank)r   r    r*   r   r   r	   r#   P      
zRankFilter.__init__c                 C   s>   |j dkrd}t||| jd | jd }|| j| jS )Nr   r      )r   r   expandr    Z
rankfilterr*   r   r   r   r	   r   T   s
   
zRankFilter.filterNr   r   r   r&   r'   r#   r   r   r   r   r	   r(   C   s
    
r(   c                   @      e Zd ZdZdZdddZdS )MedianFilterz
    Create a median filter. Picks the median pixel value in a window with the
    given size.

    :param size: The kernel size, in pixels.
    ZMedian   c                 C   s   || _ || d | _d S )Nr,   r)   r   r    r   r   r	   r#   f      zMedianFilter.__init__Nr1   r%   r   r   r   r	   r0   \       r0   c                   @   r/   )	MinFilterz
    Create a min filter.  Picks the lowest pixel value in a window with the
    given size.

    :param size: The kernel size, in pixels.
    ZMinr1   c                 C   s   || _ d| _d S r$   r)   r2   r   r   r	   r#   u   r+   zMinFilter.__init__Nr4   r%   r   r   r   r	   r6   k   r5   r6   c                   @   r/   )	MaxFilterz
    Create a max filter.  Picks the largest pixel value in a window with the
    given size.

    :param size: The kernel size, in pixels.
    ZMaxr1   c                 C   s   || _ || d | _d S )Nr   r)   r2   r   r   r	   r#      r3   zMaxFilter.__init__Nr4   r%   r   r   r   r	   r7   z   r5   r7   c                   @   s&   e Zd ZdZdZd	ddZdd ZdS )

ModeFiltera!  
    Create a mode filter. Picks the most frequent pixel value in a box with the
    given size.  Pixel values that occur only once or twice are ignored; if no
    pixel value occurs more than twice, the original pixel value is preserved.

    :param size: The kernel size, in pixels.
    ZModer1   c                 C   
   || _ d S r   )r    r2   r   r   r	   r#         
zModeFilter.__init__c                 C      | | jS r   )Z
modefilterr    r   r   r   r   r	   r         zModeFilter.filterNr4   r.   r   r   r   r	   r8      s
    
r8   c                   @   s&   e Zd ZdZd ZdddZdd ZdS )	GaussianBlura  Blurs the image with a sequence of extended box filters, which
    approximates a Gaussian kernel. For details on accuracy see
    <https://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf>

    :param radius: Standard deviation of the Gaussian kernel.
    r,   c                 C   r9   r   )radius)r   r?   r   r   r	   r#      r:   zGaussianBlur.__init__c                 C   r;   r   )Zgaussian_blurr?   r<   r   r   r	   r      r=   zGaussianBlur.filterN)r,   r.   r   r   r   r	   r>      s
    
r>   c                   @   s$   e Zd ZdZd Zdd Zdd ZdS )BoxBlura  Blurs the image by setting each pixel to the average value of the pixels
    in a square box extending radius pixels in each direction.
    Supports float radius of arbitrary size. Uses an optimized implementation
    which runs in linear time relative to the size of the image
    for any radius value.

    :param radius: Size of the box in one direction. Radius 0 does not blur,
                   returns an identical image. Radius 1 takes 1 pixel
                   in each direction, i.e. 9 pixels in total.
    c                 C   s   |dk r
d}t ||| _d S )Nr   zradius must be >= 0)r   r?   )r   r?   r   r   r   r	   r#      s   
zBoxBlur.__init__c                 C   r;   r   )Zbox_blurr?   r<   r   r   r	   r      r=   zBoxBlur.filterNr.   r   r   r   r	   r@      s
    r@   c                   @   s&   e Zd ZdZd Zd
ddZdd Zd	S )UnsharpMaska  Unsharp mask filter.

    See Wikipedia's entry on `digital unsharp masking`_ for an explanation of
    the parameters.

    :param radius: Blur Radius
    :param percent: Unsharp strength, in percent
    :param threshold: Threshold controls the minimum brightness change that
      will be sharpened

    .. _digital unsharp masking: https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking

    r,      r1   c                 C   s   || _ || _|| _d S r   )r?   percent	threshold)r   r?   rC   rD   r   r   r	   r#      s   
zUnsharpMask.__init__c                 C   s   | | j| j| jS r   )Zunsharp_maskr?   rC   rD   r<   r   r   r	   r      s   zUnsharpMask.filterN)r,   rB   r1   r.   r   r   r   r	   rA      s
    
rA   c                   @      e Zd ZdZdZdS )BLURZBlur)   rH      r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Nr   r   r   r'   r   r   r   r   r	   rF          rF   c                   @   rE   )CONTOURZContour)r1   r1   r      	rP   rP   rP      rP   rP   rP   rP   NrJ   r   r   r   r	   rL      rK   rL   c                   @   rE   )DETAILZDetail)rM      r   )	r   rP   r   rP   
   rP   r   rP   r   NrJ   r   r   r   r	   rR      rK   rR   c                   @   rE   )EDGE_ENHANCEzEdge-enhance)rM   r,   r   )	rP   rP   rP   rP   rT   rP   rP   rP   rP   NrJ   r   r   r   r	   rU     rK   rU   c                   @   rE   )EDGE_ENHANCE_MOREzEdge-enhance More)rM   r   r   )	rP   rP   rP   rP   	   rP   rP   rP   rP   NrJ   r   r   r   r	   rV     rK   rV   c                   @   rE   )EMBOSSZEmboss)rM   r      )	rP   r   r   r   r   r   r   r   r   NrJ   r   r   r   r	   rX     rK   rX   c                   @   rE   )
FIND_EDGESz
Find Edges)rM   r   r   rO   NrJ   r   r   r   r	   rZ   "  rK   rZ   c                   @   rE   )SHARPENZSharpen)rM   rI   r   )	r\   r\   r\       r\   r\   r\   r\   NrJ   r   r   r   r	   r[   -  rK   r[   c                   @   rE   )SMOOTHZSmooth)rM      r   )	r   r   r   r   rH   r   r   r   r   NrJ   r   r   r   r	   r^   8  rK   r^   c                   @   rE   )SMOOTH_MOREzSmooth More)rG   d   r   )r   r   r   r   r   r   rH   rH   rH   r   r   rH   ,   rH   r   r   rH   rH   rH   r   r   r   r   r   r   NrJ   r   r   r   r	   r`   C  rK   r`   c                   @   sR   e Zd ZdZdZdddZedd Zedd	d
Z	dddZ
dd Zdd ZdS )
Color3DLUTa  Three-dimensional color lookup table.

    Transforms 3-channel pixels using the values of the channels as coordinates
    in the 3D lookup table and interpolating the nearest elements.

    This method allows you to apply almost any color transformation
    in constant time by using pre-calculated decimated tables.

    .. versionadded:: 5.2.0

    :param size: Size of the table. One int or tuple of (int, int, int).
                 Minimal size in any dimension is 2, maximum is 65.
    :param table: Flat lookup table. A list of ``channels * size**3``
                  float elements or a list of ``size**3`` channels-sized
                  tuples with floats. Channels are changed first,
                  then first dimension, then second, then third.
                  Value 0.0 corresponds lowest value of output, 1.0 highest.
    :param channels: Number of channels in the table. Could be 3 or 4.
                     Default is 3.
    :param target_mode: A mode for the result image. Should have not less
                        than ``channels`` channels. Default is ``None``,
                        which means that mode wouldn't be changed.
    zColor 3D LUTr1   Nc                 K   s  |dvr
d}t || | | _}|| _|| _|dd}|d |d  |d  }d}	d }
t|d	rCzdd l}
W n	 tyB   Y nw |
rst	||
j
rs|rQ| }|j|| f||f|d |d |d |ffv rp||| }n3d}	n0|ryt|}|rt	|d ttfrg |}}|D ]}t||krd
| d}t ||| q|	st||| krd| d|d  d|d  d|d  dt| 
}t ||| _d S )Nr1      )Only 3 or 4 output channels are supported_copy_tableTr   r   r,   Fshapez2The elements of the table should have a length of .zThe table should have either channels * size**3 float items or size**3 items of channels-sized tuples with floats. Table should be: xz. Actual length: )r   _check_sizer    channelsr   gethasattrnumpyImportError
isinstanceZndarraycopyrh   Zreshapelisttupler   extendtable)r   r    rv   rl   target_modekwargsr   Z
copy_tableitemsZ
wrong_sizero   Z	raw_tableZpixelr   r   r	   r#   k  sj   


zColor3DLUT.__init__c              
   C   s   z| \}}}W n  t y } zd}t ||d }~w ty'   | | | f} Y nw dd | D } | D ]}d|  kr=dksDn d}t |q1| S )Nz>Size should be either an integer or a tuple of three integers.c                 S   s   g | ]}t |qS r   )int).0rj   r   r   r	   
<listcomp>  s    z*Color3DLUT._check_size.<locals>.<listcomp>r,   A   z Size should be in [2, 65] range.)r   	TypeError)r    _er   size_1dr   r   r	   rk     s    
zColor3DLUT._check_sizec              	   C   s   |  |\}}}|dvrd}t|dg|| | |  }	d}
t|D ],}t|D ]%}t|D ]}|||d  ||d  ||d  |	|
|
| < |
|7 }
q/q)q#| |||f|	||ddS )aS  Generates new LUT using provided callback.

        :param size: Size of the table. Passed to the constructor.
        :param callback: Function with three parameters which correspond
                         three color channels. Will be called ``size**3``
                         times with values from 0.0 to 1.0 and should return
                         a tuple with ``channels`` elements.
        :param channels: The number of channels which should return callback.
        :param target_mode: Passed to the constructor of the resulting
                            lookup table.
        rd   rf   r   r   Frl   rw   rg   )rk   r   range)clsr    callbackrl   rw   r   size_2dsize_3dr   rv   idx_outr   grr   r   r	   generate  s,   
zColor3DLUT.generateFc              	   C   s   |dvr
d}t || j}|p|}| j\}}	}
dg||	 |
 |  }d}d}t|
D ]F}t|	D ]?}t|D ]8}| j|||  }|rZ|||d  ||	d  ||
d  g|R  }n|| }||||| < ||7 }||7 }q6q0q*t| | j|||p|| jddS )a#  Transforms the table values using provided callback and returns
        a new LUT with altered values.

        :param callback: A function which takes old lookup table values
                         and returns a new set of values. The number
                         of arguments which function should take is
                         ``self.channels`` or ``3 + self.channels``
                         if ``with_normals`` flag is set.
                         Should return a tuple of ``self.channels`` or
                         ``channels`` elements if it is set.
        :param with_normals: If true, ``callback`` will be called with
                             coordinates in the color cube as the first
                             three arguments. Otherwise, ``callback``
                             will be called only with actual color values.
        :param channels: The number of channels in the resulting lookup table.
        :param target_mode: Passed to the constructor of the resulting
                            lookup table.
        )Nr1   re   rf   r   r   Fr   )r   rl   r    r   rv   typer   )r   r   Zwith_normalsrl   rw   r   Zch_inZch_outr   r   r   rv   Zidx_inr   r   r   r   valuesr   r   r	   	transform  sD   




zColor3DLUT.transformc                 C   sX   | j j d| jj j dj| j d| jdg}| jr$|d| j  dd|S )Nz from zsize={:d}x{:d}x{:d}z	channels=dztarget_mode=z<{}> )		__class__r   rv   formatr    rl   r   appendjoin)r   r   r   r   r	   __repr__  s   
zColor3DLUT.__repr__c              	   C   sD   ddl m} || jp|j|jj| j| jd | jd | jd | jS )Nr   )Imager   r,   )	 r   Zcolor_lut_3dr   Z
ResamplingZBILINEARrl   r    rv   )r   r   r   r   r   r	   r     s   
zColor3DLUT.filter)r1   N)FNN)r   r   r   r&   r'   r#   staticmethodrk   classmethodr   r   r   r   r   r   r   r	   rc   P  s    
<

#6
rc   )r   r   r   r   r   r(   r0   r6   r7   r8   r>   r@   rA   rF   rL   rR   rU   rV   rX   rZ   r[   r^   r`   rc   r   r   r   r	   <module>   s0   