o
    	&zh&                     @   s   d dl mZ d dlmZ ddlmZm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 )   )Anchor)resolve_palette   )Imagegetattrsafec                   @   s.   e Zd ZdZeeddZd
ddZdd Zd	S )Resizez>
    Resizes an image to the specified width and height.

    zResampling.LANCZOSLANCZOSTc                 C   s   || _ || _|| _dS z
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param upscale: Should the image be enlarged if smaller than the dimensions?

        Nwidthheightupscaleselfr   r   r    r   S/var/www/html/kangema/venv/lib/python3.10/site-packages/pilkit/processors/resize.py__init__   s   
zResize.__init__c                 C   sH   | j s| j|jd k r"| j|jd k r"t|}|| j| jf| j}|S )N    r   )r   r   sizer   r   resizer   )r   imgr   r   r   process   s   &zResize.processNT)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r      s
    
r   c                   @   "   e Zd ZdZdddZdd ZdS )	ResizeToCoverz
    Resizes the image to the smallest possible size that will entirely cover the
    provided dimensions. You probably won't be using this processor directly,
    but it's used internally by ``ResizeToFill`` and ``SmartResize``.

    Tc                 C      ||| _ | _|| _dS )zr
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.

        Nr
   r   r   r   r   r   &   s   
zResizeToCover.__init__c                 C   sd   |j \}}tt| j| t| j| }tt|| tt|| }}t||| jd	|}|S )Nr   )
r   maxfloatr   r   introundr   r   r   )r   r   original_widthoriginal_heightratio	new_width
new_heightr   r   r   r   /   s   
zResizeToCover.processNr   r   r   r   r   r   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 )	ResizeToFillzQ
    Resizes an image, cropping it to the exact specified width and height.

    NTc                 C   s   || _ || _|| _|| _dS )a.  
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param anchor: Specifies which part of the image should be retained
            when cropping.
        :param upscale: Should the image be enlarged if smaller than the dimensions?

        Nr   r   anchorr   )r   r   r   r-   r   r   r   r   r   ?   s   	
zResizeToFill.__init__c                 C   s@   ddl m} t| j| j| jd|}|| j| j| jd|S )Nr   )Cropr    r-   )cropr.   r   r   r   r   r   r-   )r   r   r.   r   r   r   r   M   s   

zResizeToFill.process)NNNTr*   r   r   r   r   r+   9   s    
r+   c                   @   r   )	SmartResizea  
    The ``SmartResize`` processor is identical to ``ResizeToFill``, except that
    it uses entropy to crop the image instead of a user-specified anchor point.
    Internally, it simply runs the ``ResizeToCover`` and ``SmartCrop``
    processors in series.
    Tc                 C   r   r	   r
   r   r   r   r   r   \   s   
zSmartResize.__init__c                 C   s:   ddl m} t| j| j| jd|}|| j| j|S )Nr   )	SmartCropr    )r0   r2   r   r   r   r   r   )r   r   r2   r   r   r   r   f   s   
zSmartResize.processNr   r*   r   r   r   r   r1   U   s    

r1   c                   @   "   e Zd ZdZdddZdd ZdS )ResizeCanvaszx
    Resizes the canvas, using the provided background color if the new size is
    larger than the current image.

    Nc                 C   sl   |dus|dur|rt d|pd|pd| _| _d| _n|p!tj| _d | _| _|| _|| _|p2d| _dS )aa  
        :param width: The target width, in pixels.
        :param height: The target height, in pixels.
        :param color: The background color to use for padding.
        :param anchor: Specifies the position of the original image on the new
            canvas. Valid values are:

            - Anchor.TOP_LEFT
            - Anchor.TOP
            - Anchor.TOP_RIGHT
            - Anchor.LEFT
            - Anchor.CENTER
            - Anchor.RIGHT
            - Anchor.BOTTOM_LEFT
            - Anchor.BOTTOM
            - Anchor.BOTTOM_RIGHT

            You may also pass a tuple that indicates the position in
            percentages. For example, ``(0, 0)`` corresponds to "top left",
            ``(0.5, 0.5)`` to "center" and ``(1, 1)`` to "bottom right". This is
            basically the same as using percentages in CSS background positions.

        NzEYou may provide either an anchor or x and y coordinate, but not both.r   )   r5   r5   r   )		Exceptionxyr-   r   CENTERr   r   color)r   r   r   r:   r-   r7   r8   r   r   r   r   s   s   zResizeCanvas.__init__c           
      C   s   |j \}}| jr2t| j}| j| | j| }}tt|t|d  }tt|t|d  }n| j| j	}}t
d| j| jf| j}	|	|||f |	S )Nr   r   RGBA)r   r-   r   	get_tupler   r   r#   r"   r7   r8   r   newr:   paste)
r   r   r%   r&   r-   trim_xtrim_yr7   r8   new_imgr   r   r   r      s   
zResizeCanvas.process)NNNNr*   r   r   r   r   r4   m   s    
'r4   c                   @   r3   )	AddBorderz?
    Add a border of specific color and size to an image.

    Nc                 C   sD   || _ t|tr| | _ | _ | _| _dS |\| _| _| _| _dS )z
        :param color: Color to use for the border
        :param thickness: Thickness of the border. Can be either an int or
            a 4-tuple of ints of the form (top, right, bottom, left).
        N)r:   
isinstancer#   toprightbottomleft)r   	thicknessr:   r   r   r   r      s   
zAddBorder.__init__c                 C   sJ   |j d | j | j }|j d | j | j }t||| j| j| jd|S )Nr   r   )r:   r7   r8   )r   rG   rE   rD   rF   r4   r:   r   )r   r   r(   r)   r   r   r   r      s   
zAddBorder.process)Nr*   r   r   r   r   rB      s    
rB   c                   @   s.   e Zd ZdZddddejfddZdd ZdS )ResizeToFitzC
    Resizes an image to fit within the specified dimensions.

    NTc                 C   s"   || _ || _|| _|| _|| _dS )a  
        :param width: The maximum width of the desired image.
        :param height: The maximum height of the desired image.
        :param upscale: A boolean value specifying whether the image should
            be enlarged if its dimensions are smaller than the target
            dimensions.
        :param mat_color: If set, the target image size will be enforced and the
            specified color will be used as a background color to pad the image.

        N)r   r   r   	mat_colorr-   )r   r   r   r   rJ   r-   r   r   r   r      s
   
zResizeToFit.__init__c                 C   s   |j \}}| jd ur| jd urtt| j| t| j| }n| jd u r,t| j| }nt| j| }tt|| tt|| f}t|d |d | jd	|}| j
d urft| j| j| j
| jd	|}|S )Nr   r   r    r/   )r   r   r   minr"   r#   r$   r   r   r   rJ   r4   r-   )r   r   	cur_width
cur_heightr'   new_dimensionsr   r   r   r      s   


zResizeToFit.process)r   r   r   r   r   r9   r   r   r   r   r   r   rI      s    rI   c                   @   r3   )	Thumbnailz
    Resize the image for use as a thumbnail. Wraps ``ResizeToFill``,
    ``ResizeToFit``, and ``SmartResize``.

    Note: while it doesn't currently, in the future this processor may also
    sharpen based on the amount of reduction.

    Nc                 C   sd   || _ || _|| _|r|du rtdd}n|d u r*|d uo!|d u}|r*|d u r*d}|| _|| _d S )NFz3You can't specify an anchor point if crop is False.Tauto)r   r   r   r6   r0   r-   )r   r   r   r-   r0   r   r   r   r   r      s   
zThumbnail.__init__c                 C   sr   | j r*| jr	| jstd| jdkrt| j| j| jd}nt| j| j| j| jd}n
t| j| j| jd}|	|S )Nz7You must provide both a width and height when cropping.rP   r
   r,   )
r0   r   r   r6   r-   r1   r   r+   rI   r   )r   r   	processorr   r   r   r   
  s   

zThumbnail.process)NNNNNr*   r   r   r   r   rO      s    
	rO   N)baser   utilsr   libr   r   objectr   r   r+   r1   r4   rB   rI   rO   r   r   r   r   <module>   s    >)