
    /~h'                         d dl mZ d dlmZ ddlmZmZmZmZ  G d de	      Z
e
j                  fdZd Z G d	 d
e	      Z G d de	      Z G d de	      Zy)   )Anchor)histogram_entropy   )Image
ImageChops	ImageDraw	ImageStatc                   (    e Zd ZdZdZdZdZeeeefZy)SidetrblN)__name__
__module____qualname__TOPRIGHTBOTTOMLEFTALL     _/var/www/peopleoo.sandbox-dev.co.uk/venv/lib/python3.12/site-packages/pilkit/processors/crop.pyr   r      s$    
CEFDvt
$Cr   r   c                    t         j                  |v r|d   ndt         j                  |v r|d   ndt         j                  |v r|d   n| j                  d   t         j
                  |v r|d   n| j                  d   f}| j                  |      S )N    r   r      )r   r   r   r   sizer   crop)imgbboxsidess      r   _cropr#      sy    99%Q188u$Q!::&QCHHQK;;%'QSXXa[	D 88D>r   c                 p   t        j                  d| j                  d      }| j                  d   dz
  | j                  d   dz
  }}|dkD  r0|dkD  r+t        j                  |      }|j                  dd||gd       t        j                  | j                  d      j                  |            j                  S )N1r   r   r   RGBA)r   newr   r   Draw	rectangler	   Statconvert	histogrammedian)r    maskwhdraws        r   detect_border_colorr2      s    99S#((A&D88A;?CHHQK!OqA1uQ~~d#1a|Q'>>#++f-77=>EEEr   c                   8    e Zd ZdZddej
                  fdZd Zy)TrimBorderColorz/Trims a color from the sides of an image.

    Ng333333?c                 .    || _         || _        || _        y)a  
        :param color: The color to trim from the image, in a 4-tuple RGBA value,
            where each component is an integer between 0 and 255, inclusive. If
            no color is provided, the processor will attempt to detect the
            border color automatically.
        :param tolerance: A number between 0 and 1 where 0. Zero is the least
            tolerant and one is the most.
        :param sides: A list of sides that should be trimmed. Possible values
            are provided by the :class:`Side` enum class.

        N)colorr"   	tolerance)selfr6   r7   r"   s       r   __init__zTrimBorderColor.__init__%   s     

"r   c                 H   |j                  d      }| j                  xs t        t        |            }t	        j
                  d|j                  |      }t        j                  ||      }| j                  dvrd| j                  cxk  rdk  sn t        d| j                  z        t        j                  |t        | j                  dz              j                  d      }t        j                  ||      }|j                         }|rt        ||| j                         }|S )Nr&   )r   r   r   r   zN%s is an invalid tolerance. Acceptable values are between 0 and 1 (inclusive).   )r+   r6   tupler2   r   r'   r   r   
differencer7   
ValueErrorconstantintsubtractgetbboxr#   r"   )r8   r    sourceborder_colorbgdifftmpr!   s           r   processzTrimBorderColor.process5   s    V$zzGU+>v+F%GYYvsxx6$$VR0>>' +!+  "<>Bnn"M N N%%dC0D,EFWV_ &&tS1D||~T4::.C
r   )r   r   r   __doc__r   r   r9   rH   r   r   r   r4   r4   !   s     "S # r   r4   c                       e Zd ZdZddZd Zy)Cropa  
    Crops an image, cropping it to the specified width and height. You may
    optionally provide either an anchor or x and y coordinates. This processor
    functions exactly the same as ``ResizeCanvas`` except that it will never
    enlarge the image.

    Nc                 J    || _         || _        || _        || _        || _        y )N)widthheightanchorxy)r8   rM   rN   rO   rP   rQ   s         r   r9   zCrop.__init__T   s%    
r   c                     ddl m} |j                  \  }}t        || j                        t        || j
                        }} |||| j                  | j                  | j                        j                  |      S )Nr   )ResizeCanvas)rO   rP   rQ   )
resizerS   r   minrM   rN   rO   rP   rQ   rH   )r8   r    rS   original_widthoriginal_height	new_width
new_heights          r   rH   zCrop.process[   se    (*-((' #NDJJ ?OT[[1 	 Iz$++&&DFF$$+GCL	1r   )NNNNN)r   r   r   rI   r9   rH   r   r   r   rK   rK   K   s    1r   rK   c                   $    e Zd ZdZddZd Zd Zy)	SmartCropa  
    Crop an image to the specified dimensions, whittling away the parts of the
    image with the least entropy.

    Based on smart crop implementation from easy-thumbnails:
        https://github.com/SmileyChris/easy-thumbnails/blob/master/easy_thumbnails/processors.py#L193

    Nc                      || _         || _        y)zr
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.

        N)rM   rN   )r8   rM   rN   s      r   r9   zSmartCrop.__init__p   s     
r   c                     t        |      }t        |      }|r,t        ||z  dz
        dk  r||dz  k\  r||fS |dz  }|||z
  fS ||kD  rd|fS |dfS )z
        Calculate the entropy of two slices (from the start and end of an axis),
        returning a tuple containing the amount that should be added to the start
        and removed from the end of the axis.

        r   g{Gz?r   r   )r   abs)r8   start_slice	end_sliceslicer=   start_entropyend_entropy
half_slices           r   compare_entropyzSmartCrop.compare_entropyy   sz     *+6'	23}{:Q>?$FUQY&e|#!Juz111;&e8O!8Or   c                    |j                   \  }}t        |t        || j                        z
        }t        |t        || j                        z
        }dx}}||}	}|rut        |t        |dz  d            }
|j                  |d||
z   |f      }|j                  ||
z
  d||f      }| j                  |||
|      \  }}||z  }||z  }||z
  |z
  }|ru|rut        |t        |dz  d            }
|j                  d||||
z   f      }|j                  d|	|
z
  ||	f      }| j                  |||
|      \  }}||z  }|	|z  }	||z
  |z
  }|ru||||	f}|j                  |      }|S )Nr      
   )r   r@   rU   rM   rN   maxr   re   )r8   r    source_xsource_ydiff_xdiff_ylefttoprightbottomra   startendaddremoveboxs                   r   rH   zSmartCrop.process   s    XX(XHdjj 99:XHdkk ::;s (vFaK 45EHHdAte|X>?E((EEM1eX>?C..uc5&IKCCKDVOEc\F*F  FaK 45EHHahe<=E((Av~x@AC..uc5&IKC3JCfFc\F*F  S%(hhsm
r   )NN)r   r   r   rI   r9   re   rH   r   r   r   r[   r[   f   s    ,r   r[   N)baser   utilsr   libr   r   r   r	   objectr   r   r#   r2   r4   rK   r[   r   r   r   <module>r{      sX     $ 9 9%6 %  88 F'f 'T16 16D Dr   