a
    Sic                     @   s   d dl Z ddlmZmZmZmZ ddlmZ G dd dZdd Z	d	d
 Z
dd ZdddZdddZdddZdddZdd ZdS )    N   )GimpGradientFileGimpPaletteFile
ImageColorPaletteFile)	deprecatec                   @   sb   e Zd ZdZdddZedd Zejd	d Zd
d Zdd Z	dd Z
e
ZdddZdd ZdS )ImagePalettea  
    Color palette for palette mapped images

    :param mode: The mode to use for the palette. See:
        :ref:`concept-modes`. Defaults to "RGB"
    :param palette: An optional palette. If given, it must be a bytearray,
        an array or a list of ints between 0-255. The list must consist of
        all channels for one color followed by the next color (e.g. RGBRGBRGB).
        Defaults to an empty palette.
    RGBNr   c                 C   sL   || _ d | _|pt | _d | _|dkrHtddd  |t| jkrHtdd S )Nr   zThe size parameter
   zwrong palette size)moderawmode	bytearraypalettedirtyr   len
ValueError)selfr   r   size r   L/var/www/html/django/DPS/env/lib/python3.9/site-packages/PIL/ImagePalette.py__init__%   s    zImagePalette.__init__c                 C   s   | j S N)_paletter   r   r   r   r   /   s    zImagePalette.palettec                 C   sb   || _ t| j}i | _tdt| j|D ]4}t| j|||  }|| jv rNq(|| | j|< q(d S )Nr   )r   r   r   colorsranger   tuple)r   r   Zmode_lenicolorr   r   r   r   3   s    

c                 C   s<   t  }| j|_| j|_| jd ur0| jd d  |_| j|_|S r   )r   r   r   r   r   )r   newr   r   r   copy?   s    
zImagePalette.copyc                 C   s    | j r| j | jfS | j|  fS )z
        Get palette contents in format suitable for the low-level
        ``im.putpalette`` primitive.

        .. warning:: This method is experimental.
        )r   r   r   tobytesr   r   r   r   getdataJ   s    zImagePalette.getdatac                 C   s6   | j rtdt| jtr | jS td| j}| S )zUConvert palette to bytes.

        .. warning:: This method is experimental.
        !palette contains raw palette dataB)r   r   
isinstancer   bytesarrayr!   )r   arrr   r   r   r!   U   s    zImagePalette.tobytesc              
   C   s  | j rtdt|tr| jdkrJt|dkrh|d dkrh|dd }n| jdkrht|dkrh|d7 }z| j| W S  ty } z4t| jt	st	| j| _
t| jd }d	}|r|jd
|jdf}||v r|d7 }q|dkrB|r.ttt| D ](\}}|dkr||vr|} q.q|dkrBtd||| j|< |d t| jk r| jd|d  t| | j|d d d  | _
n|  j
t|7  _
d| _|W  Y d}~S d}~0 0 ntdt| dS )zgGiven an rgb tuple, allocate palette entry.

        .. warning:: This method is experimental.
        r#   r	            NRGBA)r+   r   
backgroundtransparencyr      r   z$cannot allocate more than 256 colorszunknown color specifier: )r   r   r%   r   r   r   r   KeyErrorr   r   r   infogetreversedlist	enumerate	histogramr&   r   repr)r   r   imageeindexZspecial_colorsr   countr   r   r   getcolord   sT    









zImagePalette.getcolorc              
   C   s   | j rtdt|tr"t|d}|d |d| j d tdD ]z}||  t|t| j |d t| j D ]<}z|d| j	|   W qz t
y   |d	 Y qz0 qz|d qH|  d
S )zVSave palette to text file.

        .. warning:: This method is experimental.
        r#   wz
# Palette
z# Mode: 
r/   r    z 0N)r   r   r%   stropenwriter   r   r   r   
IndexErrorclose)r   fpr   jr   r   r   save   s    


&zImagePalette.save)r	   Nr   )N)__name__
__module____qualname____doc__r   propertyr   setterr    r"   r!   tostringr<   rG   r   r   r   r   r      s   




4r   c                 C   s   t  }| |_||_d|_|S )Nr   )r   r   r   r   )r   datar   r   r   r   raw   s
    rP   c                 C   s6   g }| dkr.t dD ]}||| d  qnt|S )Nr   r/   r+   )r   appendNotImplementedError)blackwhitelutr   r   r   r   make_linear_lut   s    rV   c                 C   s4   g }t dD ]"}|t|d |  d d  q|S )Nr/   g     o@g      ?)r   rQ   int)exprU   r   r   r   r   make_gamma_lut   s     rY   r	   c                    s4   t tdt  }|  t  fdd|D S )Nr/   c                    s   g | ]}|t   qS r   r   .0r   r   r   r   
<listcomp>       znegative.<locals>.<listcomp>)r4   r   r   reverser   r   r   r   r]   r   negative   s    rb   c                 C   s@   ddl m} g }tdt|  D ]}||dd q t| |S )Nr   )randintr/   r+   )randomrc   r   r   rQ   r   )r   rc   r   r   r   r   r   rd      s
    rd   #fff0c0c                    s0   dd t | D  td fddtdD S )Nc                 S   s   g | ]}t d |qS )r   )rV   )r\   bandr   r   r   r^      r_   zsepia.<locals>.<listcomp>r	   c                    s    g | ]} |d   |d   qS )r*   r   r[   bandsr   r   r^      r_   i   )r   Zgetrgbr   r   )rT   r   rg   r   sepia   s    ri   c                    s,   t tdt  }t  fdd|D S )Nr/   c                    s   g | ]}|t   qS r   rZ   r[   r]   r   r   r^      r_   zwedge.<locals>.<listcomp>)r4   r   r   r   ra   r   r]   r   wedge   s    rj   c                 C   s   t | dj}tjtjtjfD ]@}z$|d || }|rBW  qfW q ttfyZ   Y q0 qtdW d    n1 sz0    Y  |S )Nrbr   zcannot load palette)	rA   r   r   r   seek
getpaletteSyntaxErrorr   OSError)filenamerE   ZpaletteHandlerrU   r   r   r   load   s    

&rq   )r	   )r	   )re   )r	   )r'    r   r   r   r   
_deprecater   r   rP   rV   rY   rb   rd   ri   rj   rq   r   r   r   r   <module>   s    


	

