o
    hcD                     @   s   d dl mZ z
d dlmZmZ W n	 ey   Y nw 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   U/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/willow/plugins/pillow.pyr      s    r   c                  C      dd l } | jS Nr   )	PIL.Imager   PILr   r   r   
_PIL_Image      r   c                  C   r   r   )PIL.ImageCmsImageCmsr   r   r   r   _PIL_ImageCms#   r   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dBddZej				 dCd!ed"ed#ed$efd%d&ZejdDd"ed$efd'd(ZejdEd$efd)d*Zej	+		 dFd!ed,ed$efd-d.Zej	+		 dFd!ed,ed$efd/d0ZejdFd1d2ZejdEd3d4Zejd5d6 Zejd7d8 Zeee ee!eje"d9d: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.dAS )G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   registered_extensionsvalues)r)   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      dS )N   r   r1   r   r   r   get_frame_count:      zPillowImage.get_frame_countc                 C   s$   | j }|jdv p|jdkod|jv S )N)RGBALAPtransparency)r%   modeinfo)r'   imgr   r   r   	has_alpha?   s   
zPillowImage.has_alphac                 C   r3   )NFr   r1   r   r   r   has_animationF   r6   zPillowImage.has_animationc                 C   sL   | j jdv r|  r| j d}n
| j d}n| j }t||t jjS )N1r9   r7   RGB)	r%   r;   r>   convertr"   resizer   
ResamplingLANCZOS)r'   r0   r%   r   r   r   rD   K   s   zPillowImage.resizec           	      C   s   |\}}}}| j j\}}||ks$||ks$|dks$||ks$||ks$|dkr+td|td|td|t||t||f}t| j |S )Nr   zInvalid crop dimensions: )r%   r0   r   maxminr"   crop)	r'   rectlefttoprightbottomwidthheightclamped_rectr   r   r   rI   Z   s   zPillowImage.cropc                 C   sX   t  }|jj|jj|jjd}|d }|s| S ||}|s"t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   	Transpose	ROTATE_90
ROTATE_180
ROTATE_270getr   r%   	transposer"   )r'   angler   ORIENTATION_TO_TRANSPOSEmodulo_angletranspose_code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r<|| nt ||}t|d	S )
N   z6the 'color' argument must be a 3-element tuple or listr7   r   r4         alpha_compositerB   )r>   
isinstancetuplelistlen	TypeErrorr%   rC   r   newr0   hasattrrc   r"   )r'   colorr%   	new_imager   r   r   set_background_color_rgb   s   
z$PillowImage.set_background_color_rgbc                 C      | j jdS )Nicc_profiler%   r<   rX   r1   r   r   r   get_icc_profile      zPillowImage.get_icc_profilec                 C   rn   )Nexifrp   r1   r   r   r   get_exif_data   rr   zPillowImage.get_exif_datar   c                 C   s\   |   }|du r
| S t }|t|}|  rd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/
        Nr7   rB   sRGB)renderingIntent
outputMode)	rq   r!   ImageCmsProfiler   r>   profileToProfiler%   createProfiler"   )r'   rendering_intentro   r    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}|rd|d< |r d|d< |  }|dur,||d< |  }	|	dur8|	|d	< |j|d
fi | |rJ| |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
        r@   rB   r   Tr   r   Nro   rs   JPEGjpeg)r%   r;   rC   rq   rt   saver   r   )
r'   fr   r   r   r   r%   kwargsro   	exif_datar   r   r   save_as_jpeg   s$   zPillowImage.save_as_jpegc           	      C   s   i }| j }|  }|dur&| j jdkr!|  }|j }| |d< n||d< n
|jdkr0|d}|r6d|d< |  }|durB||d< |j|dfi | |rT| |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CMYKro   rB   Tr   rs   PNGpng)	r%   rq   r;   r}   rC   rt   r   r   r   )	r'   r   r   r   r   r%   ro   pillow_imager   r   r   r   save_as_png  s(   


zPillowImage.save_as_pngc                 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 | |r2| |d t	|S )N)Lr9   r9   )paletter:   GIFgif)
r%   r;   rC   r   PaletteADAPTIVEr<   r   r   r   )r'   r   r   r%   r   r   r   r   save_as_gif<  s   

zPillowImage.save_as_gifP   losslessc           	      C   s~   ||d}| j }|  }|dur'|jdkr#|  }|j }| |d< n||d< |j|dfi | |r;|s;| |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   r   Nr   ro   WEBPwebp)r%   rq   r;   r}   r   r   r   	r'   r   r   r   r   r   r%   ro   r   r   r   r   save_as_webpO  s   

zPillowImage.save_as_webpc           	      C   s   d|i}|rddd}| j }|  }|dur-|jdkr)|  }|j }| |d< n||d< |j|dfi | |sA|rA| |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   chromaNr   ro   HEIFheic)r%   rq   r;   r}   r   r   r	   r   r   r   r   save_as_heicz  s   

zPillowImage.save_as_heicc           	      C   s   d|i}|rddd}| j }|  }|d ur-|jdkr)|  }|j }| |d< n||d< |j|dfi | |sA|rA| |d t|S )	Nr   r   r   r   r   ro   AVIFr   )r%   rq   r;   r}   r   r   r   r   r   r   r   save_as_avif  s   

zPillowImage.save_as_avifc                 C   s&   | j |d |r| |d t|S )NICOico)r%   r   r   r
   )r'   r   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   r   exif_transposer%   r"   )r'   r   r%   r   r   r   auto_orient  s   zPillowImage.auto_orientc                 C   s   | j S r#   r$   r1   r   r   r   get_pillow_image  s   zPillowImage.get_pillow_image   )costc                 C   s*   |j d t |j }|  | |S r   )r   seekr   openload)r)   
image_filer%   r   r   r   r     s   zPillowImage.openc                 C   *   | j }|jdkr|d}t|j| S )NrB   )r%   r;   rC   r   r0   tobytesr&   r   r   r   to_buffer_rgb     

zPillowImage.to_buffer_rgbc                 C   r   )Nr7   )r%   r;   rC   r   r0   r   r&   r   r   r   to_buffer_rgba  r   zPillowImage.to_buffer_rgbaN)r   )r~   FFT)FT)T)r   FT)/r   r   r   r(   classmethodr*   r/   r   	operationr2   r5   r>   r?   rD   rI   r_   rm   rq   rt   r}   intboolr   r   r   r   r   r   r   r   r   converter_fromr   r   r   r   r   r   r	   r   r
   r   converter_tor   r   r   r   r   r   r   r   r"   )   s    








!
*+,*-
	

r"   N)ior   pillow_heifr   r   ImportErrorwillow.imager   r   r   r   r	   r
   r   r   r   r   r   r   r   	Exceptionr   r   r!   r"   willow_image_classesr   r   r   r   <module>   s    <   
[