a
    CgcD                     @   s   d dl mZ zd dlmZmZ W n ey2   Y n0 d dlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ G dd deZdd Zdd	 ZG d
d deZegZdS )    )BytesIO)AvifImagePluginHeifImagePlugin)AvifImageFileBadImageOperationErrorBMPImageFileGIFImageFileHeicImageFileIcoImageFileImageJPEGImageFilePNGImageFileRGBAImageBufferRGBImageBufferTIFFImageFileWebPImageFilec                   @   s   e Zd ZdS )UnsupportedRotationN)__name__
__module____qualname__ r   r   T/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/willow/plugins/pillow.pyr      s   r   c                  C   s   dd l } | jS Nr   )Z	PIL.Imager   PILr   r   r   
_PIL_Image   s    r   c                  C   s   dd l } | jS r   )ZPIL.ImageCmsImageCmsr   r   r   r   _PIL_ImageCms#   s    r   c                   @   s  e Zd Zdd Zedd Zedd Zejdd Z	ejd	d
 Z
ejdd Zejdd Zejdd Zejdd Zejdd Zejdd Zdd Zdd ZejdAddZejdBeeeed!d"d#ZejdCeed$d%d&ZejdDed'd(d)ZejdEeeed+d,d-ZejdFeeed+d.d/ZejdGd0d1ZejdHd2d3Zejd4d5 Zejd6d7 Zeee ee!eje"d8d9ee#ee$ee%ee&ee'ee(d:d; Z)e*e+d<d= Z,e*e-d>d? Z.d@S )IPillowImagec                 C   s
   || _ d S Nimageselfr!   r   r   r   __init__*   s    zPillowImage.__init__c                 C   s
   t   d S r   )r   )clsr   r   r   check-   s    zPillowImage.checkc                 C   s   t   }|| v S r   )r   Zregistered_extensionsvalues)r%   Zimage_formatformatsr   r   r   is_format_supported1   s    
zPillowImage.is_format_supportedc                 C   s   | j jS r   )r!   sizer#   r   r   r   get_size6   s    zPillowImage.get_sizec                 C   s   dS )N   r   r+   r   r   r   get_frame_count:   s    zPillowImage.get_frame_countc                 C   s$   | j }|jdv p"|jdko"d|jv S )N)RGBAZLAPtransparency)r!   modeinfo)r#   imgr   r   r   	has_alpha?   s    
zPillowImage.has_alphac                 C   s   dS )NFr   r+   r   r   r   has_animationF   s    zPillowImage.has_animationc                 C   sL   | j jdv r0|  r"| j d}q6| j d}n| j }t||t jjS )N1r0   r/   RGB)	r!   r2   r5   convertr   resizer   Z
ResamplingZLANCZOS)r#   r*   r!   r   r   r   r;   K   s    zPillowImage.resizec           	      C   s   |\}}}}| j j\}}||ksH||ksH|dksH||ksH||ksH|dkrVtd|td|td|t||t||f}t| j |S )Nr   zInvalid crop dimensions: )r!   r*   r   maxminr   crop)	r#   ZrectlefttoprightZbottomwidthheightZclamped_rectr   r   r   r>   Z   s*    zPillowImage.cropc                 C   sX   t  }|jj|jj|jjd}|d }|s.| S ||}|sDtd| j|}t	|S )zp
        Accept a multiple of 90 to pass to the underlying Pillow function
        to rotate the image.
        )Z      i  ih  zLSorry - we only support right angle rotations - i.e. multiples of 90 degrees)
r   Z	TransposeZ	ROTATE_90Z
ROTATE_180Z
ROTATE_270getr   r!   Z	transposer   )r#   Zangler   ZORIENTATION_TO_TRANSPOSEZmodulo_angleZtranspose_codeZrotatedr   r   r   rotater   s    
zPillowImage.rotatec                 C   s   |   s| S t|ttfr&t|dks.td| jd}t 	d| jj
|d |d |d df}t|drx|| nt ||}t|d	S )
N   z6the 'color' argument must be a 3-element tuple or listr/   r   r-         alpha_compositer9   )r5   
isinstancetuplelistlen	TypeErrorr!   r:   r   newr*   hasattrrK   r   )r#   colorr!   Z	new_imager   r   r   set_background_color_rgb   s    
z$PillowImage.set_background_color_rgbc                 C   s   | j jdS )Nicc_profiler!   r3   rF   r+   r   r   r   get_icc_profile   s    zPillowImage.get_icc_profilec                 C   s   | j jdS )NexifrV   r+   r   r   r   get_exif_data   s    zPillowImage.get_exif_datar   c                 C   s\   |   }|du r| S t }|t|}|  r4dnd}|j| j||d||d}t|S )aY  
        Transforms the color of the image to fit inside sRGB color gamut using the
        embedded ICC profile. The resulting image will always be in RGB(A) mode
        and will have a small generic sRGB ICC profile embedded.

        If the image does not have an ICC profile this operation is a no-op.
        Images without a profile are commonly assumed to be in sRGB color space
        already.

        :param rendering_intent: Controls how out-of-gamut colors and handled.
        Defaults to 0 (perceptual) because this is what Pillow defaults to.
        :return: PillowImage in RGB mode
        :raises: PIL.ImageCms.PyCMSError

        Further reading:
            * https://pillow.readthedocs.io/en/stable/reference/ImageCms.html#PIL.ImageCms.profileToProfile
            * https://www.permajet.com/blog/rendering-intents-explained/
        Nr/   r9   ZsRGB)ZrenderingIntentZ
outputMode)	rW   r   ZImageCmsProfiler   r5   ZprofileToProfiler!   ZcreateProfiler   )r#   Zrendering_intentrU   r   Zoutput_moder!   r   r   r   transform_colorspace_to_srgb   s    z(PillowImage.transform_colorspace_to_srgbU   FT)qualityoptimizeprogressiveapply_optimizersc           
      C   s   | j jdv r| j d}n| j }d|i}|r4d|d< |r@d|d< |  }|durX||d< |  }	|	durp|	|d	< |j|d
fi | |r| |d t|S )a  
        Save the image as a JPEG file.

        :param f: the file or file-like object to save to
        :param quality: the image quality
        :param optimize: Whether Pillow should optimize the file. When True, Pillow will
            attempt to compress the palette by eliminating unused colors.
        :param progressive: whether to save as progressive JPEG file.
        :param apply_optimizers: controls whether to run any configured optimizer libraries
        :return: JPEGImageFile
        r7   r9   r\   Tr]   r^   NrU   rX   ZJPEGZjpeg)r!   r2   r:   rW   rY   saver]   r   )
r#   fr\   r]   r^   r_   r!   kwargsrU   	exif_datar   r   r   save_as_jpeg   s$    zPillowImage.save_as_jpeg)r]   r_   c           	      C   s   i }| j }|  }|durL| j jdkrB|  }|j }| |d< q`||d< n|jdkr`|d}|rld|d< |  }|dur||d< |j|dfi | |r| |d	 t|S )
a  
        Save the image as a PNG file.

        :param f: the file or file-like object to save to
        :param optimize: Whether Pillow should optimize the file. When True, Pillow will
            attempt to compress the palette by eliminating unused colors.
        :param apply_optimizers: controls whether to run any configured optimizer libraries
        :return: PNGImageFile
        NCMYKrU   r9   Tr]   rX   ZPNGZpng)	r!   rW   r2   rZ   r:   rY   r`   r]   r   )	r#   ra   r]   r_   rb   r!   rU   pillow_imagerc   r   r   r   save_as_png  s(    


zPillowImage.save_as_png)r_   c                 C   sl   | j }|jdvr$|jdt jjd}i }d|jv r@|jd |d< |j|dfi | |rd| |d t	|S )N)Lr0   r0   )Zpaletter1   ZGIFZgif)
r!   r2   r:   r   ZPaletteZADAPTIVEr3   r`   r]   r   )r#   ra   r_   r!   rb   r   r   r   save_as_gif<  s    

zPillowImage.save_as_gifP   )r\   losslessr_   c           	      C   s~   ||d}| j }|  }|durN|jdkrF|  }|j }| |d< n||d< |j|dfi | |rv|sv| |d t|S )a  
        Save the image as a WEBP file.

        :param f: the file or file-like object to save to
        :param quality: the image quality
        :param lossless: whether to save as lossless WEBP file.
        :param apply_optimizers: controls whether to run any configured optimizer libraries.
            Note that when lossless=True, this will be ignored.
        :return: WebPImageFile
        )r\   rk   Nre   rU   ZWEBPZwebp)r!   rW   r2   rZ   r`   r]   r   	r#   ra   r\   rk   r_   rb   r!   rU   rf   r   r   r   save_as_webpO  s    

zPillowImage.save_as_webpc           	      C   s   d|i}|rddd}| j }|  }|durZ|jdkrR|  }|j }| |d< n||d< |j|dfi | |s|r| |d	 t|S )
a  
        Save the image as a HEIC file.

        :param f: the file or file-like object to save to
        :param quality: the image quality
        :param lossless: whether to save as lossless HEIC/HEIF file.
        :param apply_optimizers: controls whether to run any configured optimizer libraries.
            Note that when lossless=True, this will be ignored.
        :return: HeicImageFile
        r\     r\   ZchromaNre   rU   ZHEIFheic)r!   rW   r2   rZ   r`   r]   r	   rl   r   r   r   save_as_heicz  s    

zPillowImage.save_as_heicc           	      C   s   d|i}|rddd}| j }|  }|d urZ|jdkrR|  }|j }| |d< n||d< |j|dfi | |s|r| |d t|S )	Nr\   rn   ro   rp   re   rU   ZAVIFrq   )r!   rW   r2   rZ   r`   r]   r   rl   r   r   r   save_as_avif  s    

zPillowImage.save_as_avifc                 C   s&   | j |d |r| |d t|S )NZICOZico)r!   r`   r]   r
   )r#   ra   r_   r   r   r   save_as_ico  s    zPillowImage.save_as_icoc                 C   s    ddl m} || j}t|S )Nr   )ImageOps)r   ru   Zexif_transposer!   r   )r#   ru   r!   r   r   r   auto_orient  s    zPillowImage.auto_orientc                 C   s   | j S r   r    r+   r   r   r   get_pillow_image  s    zPillowImage.get_pillow_image   )Zcostc                 C   s*   |j d t |j }|  | |S r   )ra   seekr   openload)r%   Z
image_filer!   r   r   r   rz     s    zPillowImage.openc                 C   s*   | j }|jdkr|d}t|j| S )Nr9   )r!   r2   r:   r   r*   tobytesr"   r   r   r   to_buffer_rgb  s    

zPillowImage.to_buffer_rgbc                 C   s*   | j }|jdkr|d}t|j| S )Nr/   )r!   r2   r:   r   r*   r|   r"   r   r   r   to_buffer_rgba  s    

zPillowImage.to_buffer_rgbaN)r   )r[   FFT)FT)T)rj   FT)rj   FT)rj   FT)T)/r   r   r   r$   classmethodr&   r)   r   Z	operationr,   r.   r5   r6   r;   r>   rG   rT   rW   rY   rZ   intboolrd   rg   ri   rm   rr   rs   rt   rv   rw   Zconverter_fromr   r   r   r   r   r   r	   r   r
   rz   Zconverter_tor   r}   r   r~   r   r   r   r   r   )   s   








!
*    +,   *   -
	

r   N)ior   Zpillow_heifr   r   ImportErrorZwillow.imager   r   r   r   r	   r
   r   r   r   r   r   r   r   	Exceptionr   r   r   r   Zwillow_image_classesr   r   r   r   <module>   s   <   [