o
    vh'                     @   s   d dl mZ d dlmZ ddlmZmZmZmZ G dd de	Z
e
jfddZd	d
 ZG dd de	ZG dd de	ZG dd de	ZdS )   )Anchor)histogram_entropy   )Image
ImageChops	ImageDraw	ImageStatc                   @   s(   e Zd ZdZdZdZdZeeeefZdS )SidetrblN)__name__
__module____qualname__TOPRIGHTBOTTOMLEFTALL r   r   T/var/www/html/hyperkenya/venv/lib/python3.10/site-packages/pilkit/processors/crop.pyr	      s    r	   c                 C   sj   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}| |S )N    r   r      )r	   r   r   r   sizer   crop)imgbboxsidesr   r   r   _crop   s   
r   c                 C   st   t d| jd}| jd d | jd d }}|dkr.|dkr.t|}|dd||gd t| d	|j
S )N1r   r   r   RGBA)r   newr   r   Draw	rectangler   Statconvert	histogrammedian)r   maskwhdrawr   r   r   detect_border_color   s   
r-   c                   @   s*   e Zd ZdZddejfddZdd ZdS )TrimBorderColorz/Trims a color from the sides of an image.

    Ng333333?c                 C   s   || _ || _|| _dS )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)selfr/   r0   r   r   r   r   __init__%   s   
zTrimBorderColor.__init__c                 C   s   | d}| jptt|}td|j|}t||}| j	dvrHd| j	  kr,dks4n t
d| j	 t|t| j	d  d}t||}| }|rUt||| j}|S )Nr!   )r   r   r   r   zN%s is an invalid tolerance. Acceptable values are between 0 and 1 (inclusive).   )r&   r/   tupler-   r   r"   r   r   
differencer0   
ValueErrorconstantintsubtractgetbboxr   r   )r1   r   sourceborder_colorbgdifftmpr   r   r   r   process5   s"   

zTrimBorderColor.process)r   r   r   __doc__r	   r   r2   r@   r   r   r   r   r.   !   s    r.   c                   @   s"   e Zd ZdZdddZdd ZdS )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                 C   s"   || _ || _|| _|| _|| _d S )N)widthheightanchorxy)r1   rC   rD   rE   rF   rG   r   r   r   r2   T   s
   
zCrop.__init__c                 C   sN   ddl m} |j\}}t|| jt|| j}}|||| j| j| jd	|S )Nr   )ResizeCanvas)rE   rF   rG   )
resizerH   r   minrC   rD   rE   rF   rG   r@   )r1   r   rH   original_widthoriginal_height	new_width
new_heightr   r   r   r@   [   s   



zCrop.process)NNNNN)r   r   r   rA   r2   r@   r   r   r   r   rB   K   s    
rB   c                   @   s*   e Zd ZdZd	ddZdd Zdd ZdS )
	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                 C   s   || _ || _dS )zr
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.

        N)rC   rD   )r1   rC   rD   r   r   r   r2   p   s   
zSmartCrop.__init__c                 C   sh   t |}t |}|r(t|| d dk r(||d kr||fS |d }||| fS ||kr0d|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)r1   start_slice	end_sliceslicer5   start_entropyend_entropy
half_slicer   r   r   compare_entropyy   s   zSmartCrop.compare_entropyc                 C   sN  |j \}}t|t|| j }t|t|| j }d }}||}}	|r^t|t|d d}
||d||
 |f}|||
 d||f}| |||
|\}}||7 }||8 }|| | }|s$|rt|t|d d}
|d||||
 f}|d|	|
 ||	f}| |||
|\}}||7 }|	|8 }	|| | }|s`||||	f}||}|S )Nr      
   )r   r8   rJ   rC   rD   maxr   rW   )r1   r   source_xsource_ydiff_xdiff_ylefttoprightbottomrS   startendaddremoveboxr   r   r   r@      s4   

		
zSmartCrop.process)NN)r   r   r   rA   r2   rW   r@   r   r   r   r   rO   f   s
    
		rO   N)baser   utilsr   libr   r   r   r   objectr	   r   r   r-   r.   rB   rO   r   r   r   r   <module>   s    
	*