o
    	&zhd1                     @   s  d dl Z d dlZd dlZd dlmZ ddlmZmZ ddlm	Z	m
Z
mZmZmZ ddgZddgZd	d
dZd-ddZdd Zd add Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZG d#d$ d$eZd.d%d&ZG d'd( d(eZ d)d* Z!d/d+d,Z"dS )0    N)UnsupportedOperation   )UnknownExtensionUnknownFormat)Image	ImageFileStringIOstring_typesgetattrsafePNGWEBPGIFz.jpgz.png)JPEGr   Tc                 K   s   t | t |||S N)
save_imager   )imgformatautoconvertoptions r   G/var/www/html/kangema/venv/lib/python3.10/site-packages/pilkit/utils.pyimg_to_fobj   s   r   c                 C   s   |  d t| S )Nr   )seekr   open)targetr   r   r   
open_image   s   

r   c                   C   s   t dk rt  da dS dS )zLoads the standard PIL file format drivers. Returns True if ``preinit()``
    was called (and there's a potential that more drivers were loaded) or False
    if there is no possibility that new drivers were loaded.

    r   TF)	_pil_initr   preinitr   r   r   r   _preinit_pil   s
   r   c                   C   s"   t   tdk rt  dadS dS )zLoads all PIL file format drivers. Returns True if ``init()`` was called
    (and there's a potential that more drivers were loaded) or False if there is
    no possibility that new drivers were loaded.

       TF)r   r   r   initr   r   r   r   	_init_pil+   s   r!   c                 C   s   t j|  S r   )r   	EXTENSIONgetlower)	extensionr   r   r   _extension_to_format:   s   r&   c                 C   sR   | r'|   } | tv rt|  }|tjv r|S tj D ]\}}|| kr&|  S qd S r   )upperDEFAULT_EXTENSIONSr   r"   items)r   extkvr   r   r   _format_to_extension>   s   
r-   c                 C   s:   zd| pd }t |d }W |S  ty   d }Y |S w )Nza%s r   )	mimetypes
guess_type
IndexError)r*   filenamemimetyper   r   r   extension_to_mimetypeN   s   r4   c                 C   s   t t| S r   )r4   format_to_extension)r   r   r   r   format_to_mimetypeW      r6   c                 C   s<   t | }|st rt | }|st rt | }|st| |S )zDReturns the format that corresponds to the provided extension.

    )r&   r   r!   r   )r%   r   r   r   r   extension_to_format[   s   

r8   c                 C   sD   d}| rt | }|st rt | }|st rt | }|s t| |S )zCReturns the first extension that matches the provided format.

    N)r-   r   r!   r   )r   r%   r   r   r   r5   i   s   

r5   c                 C   s   t j| d }zt|}W n ty   |}Y |S w | | kr'|}|S zt|}W n ty9   |}Y |S w | | krF|}|S |}|S )Nr   )ospathsplitextr5   r   r$   r8   r   )namer   original_extensionsuggested_extensionr%   original_formatr   r   r   suggest_extensiony   s*   r@   c                       s<   e Zd Z fddZdd Zdd Zdd Zd	d
 Z  ZS )FileWrapperc                    s   t t| d| d S )N_wrapped)superrA   __setattr__)selfwrapped	__class__r   r   __init__   s   zFileWrapper.__init__c                 C   s    z| j  W S  ty   tw r   )rB   filenor   AttributeErrorrE   r   r   r   rJ      s
   zFileWrapper.filenoc                 C      t | j|S r   )getattrrB   )rE   r<   r   r   r   __getattr__   r7   zFileWrapper.__getattr__c                 C   s   t | j||S r   )setattrrB   )rE   r<   valuer   r   r   rD      s   zFileWrapper.__setattr__c                 C   rM   r   )delattrrB   )rE   keyr   r   r   __delattr__   r7   zFileWrapper.__delattr__)	__name__
__module____qualname__rI   rJ   rO   rD   rT   __classcell__r   r   rG   r   rA      s    rA   c           
         s:  pi |rt  \}| |zd W n	 ty$   Y nw  fdd}tfddtD r;}nt}z|| W nD ty   tdv rWt	d ndd j
d d d	j
d  j
d
  }|tjk rs tj}	|t_z	|| W |	t_n|	t_w Y nw zd W S  ty   Y S w )aR  
    Wraps PIL's ``Image.save()`` method. There are two main benefits of using
    this function over PIL's:

    1. It gracefully handles the infamous "Suspension not allowed here" errors.
    2. It prepares the image for saving using ``prepare_image()``, which will do
        some common-sense processing given the target format.

    r   c                    s@   t   j|  fi  W d    d S 1 sw   Y  d S r   )quietsave)fp)r   r   r   r   r   rZ      s   "zsave_image.<locals>.savec                 3   s    | ]}t  |V  qd S r   )
isinstance).0t)outfiler   r   	<genexpr>   s    zsave_image.<locals>.<genexpr>exif         r   )prepare_imageupdater   rK   anyr	   rA   IOErrormaxlensizer   MAXBLOCK)
r   r_   r   r   r   save_kwargsrZ   wrappernew_maxblockold_maxblockr   )r   r   r   r_   r   r      sJ   

	

r   c                   @   s    e Zd ZdZdd Zdd ZdS )rY   z
    A context manager for suppressing the stderr activity of PIL's C libraries.
    Based on http://stackoverflow.com/a/978264/155370

    c                 C   s   zt j | _W n ty   Y d S  ty   Y d S w zttjtj	| _
W n
 ty1   Y d S w t| j| _t| j
| j d S r   )sys
__stderr__rJ   	stderr_fdrK   r   r9   r   devnullO_RDWRnull_fdOSErrordupolddup2rL   r   r   r   	__enter__   s   zquiet.__enter__c                 O   sL   t | dd sd S t | dd sd S t| j| j t| j t| j d S )Nrv   ry   )rN   r9   rz   ry   rs   closerv   )rE   argskwargsr   r   r   __exit__  s   zquiet.__exit__N)rU   rV   rW   __doc__r{   r   r   r   r   r   rY      s    rY   c                 C   sF  d}i }|  }| jdkrG|tv rnt|tv rD|   |  d }t|dd }ttdd}| 	dj	d	|d
d} | 
d
| d
|d< nBd}n?| jd	krp|tv rcz	| jd |d< W n, tyb   Y n$w |tv rm| 	d} nd}n| 	d} |dkrttdd}| j	d	|d} |rddlm} | | 	d} |dkrd|d< | |fS )a  
    Prepares the image for saving to the provided format by doing some
    common-sense conversions. This includes things like preserving transparency
    and quantizing. This function is used automatically by ``save_image()``
    immediately before saving unless you specify ``autoconvert=False``. It is
    provided as a utility for those doing their own processing.

    :param img: The image to prepare for saving.
    :param format: The format that the image will be saved to.

    FRGBAc                 S   s   | dkrdS dS )N      r   r   )ar   r   r   <lambda>9  s    zprepare_image.<locals>.<lambda>zPalette.ADAPTIVEADAPTIVERGBPr   )palettecolorstransparencyTr   )r   r   )
MakeOpaquer   optimize)r'   modeRGBA_TRANSPARENCY_FORMATSPALETTE_TRANSPARENCY_FORMATSloadsplitr   evalr
   convertpasteinfoKeyError
processorsr   process)r   r   make_opaquerm   alphamaskr   r   r   r   r   re     sF   

	

re   c                 C   sR   ddl m} | j}||pg | } |p| jp|pd}|pi }t| ||fi |S )Nr   )ProcessorPipeliner   )r   r   r   r   r   )r   r   r   r   r   r   r?   r   r   r   process_imageh  s   r   )T)NT)NNTN)#r9   r/   rq   ior   
exceptionsr   r   libr   r   r   r	   r
   r   r   r(   r   r   r   r   r!   r&   r-   r4   r6   r8   r5   r@   objectrA   r   rY   re   r   r   r   r   r   <module>   s8    
	
G$X