CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ImagePalette.cpython-310.pyc83 linesDownload Raw Back to __pycache__
1o

2��Yi�$�@s�ddlmZddlZddlmZddlmZddlmZm	Z	m3Z4mZdZer,ddlm
Z
Gd	d5�d6�Zd+dd�Zd,dd�Zd-dd�Zd.d/dd�Zd.d/d d!�Zd0d1d#d$�Zd.d/d%d&�Zd2d)d*�ZdS)3�)�annotationsN)�Sequence)�IO�)�GimpGradientFile�GimpPaletteFile�7ImageColor�PaletteFileF)�Imagec@s�eZdZdZ		d,d-d8d�Zed.d
d��Zejd/dd��Zed0dd��Zejd1dd��Zd2dd�Z	d3dd�Z9d4dd�ZeZ	d5d6d"d#�Z
	d7d8d&d'�Zd9d*d+�ZdS):�ImagePalettea�10    Color palette for palette mapped images11 12    :param mode: The mode to use for the palette. See:13        :ref:`concept-modes`. Defaults to "RGB"14    :param palette: An optional palette. If given, it must be a bytearray,15        an array or a list of ints between 0-255. The list must consist of16        all channels for one color followed by the next color (e.g. RGBRGBRGB).17        Defaults to an empty palette.18    �RGBN�mode�str�palette�(Sequence[int] | bytes | bytearray | None�return�NonecCs"||_d|_|p19t�|_d|_dS�N)r
�rawmode�	bytearrayr�dirty)�selfr
r�r�zE:\DocsHouse\542 percep lab latest\PerceptionLab\PerceptionLab_Portable\python_embed\Lib\site-packages\PIL/ImagePalette.py�__init__+s20zImagePalette.__init__�!Sequence[int] | bytes | bytearraycCs|jSr)�_palette�rrrrr5szImagePalette.palettecCsd|_||_dSr)�_colorsr)rrrrrr9s21�dict[tuple[int, ...], int]cCsh|jdur1t|j�}i|_tdt|j�|�D]}t|j|||��}||jvr)q|||j|<q|jS)Nr)r�lenr
�ranger�tuple)r�mode_len�i�colorrrr�colors>s222324zImagePalette.colorsr&cCs25||_dSr)r)rr&rrrr&Js26cCs<t�}|j|_|j|_|jdur|jdd�|_|j|_|Sr)rr
rrr)r�newrrr�copyNs27zImagePalette.copy�-tuple[str, Sequence[int] | bytes | bytearray]cCs |jr	|j|jfS|j|��fS)z�28        Get palette contents in format suitable for the low-level29        ``im.putpalette`` primitive.30 31        .. warning:: This method is experimental.32        )rrr
�tobytesrrrr�getdataYszImagePalette.getdata�bytescCs:|jr	d}t|��t|jt�r|jSt�d|j�}|��S)zUConvert palette to bytes.33 34        .. warning:: This method is experimental.35        �!palette contains raw palette data�B)r�36ValueError�37isinstancerr,�arrayr*)r�msgZarrrrrr*dszImagePalette.tobytes�image�Image.Image | None�e�Exception | None�intcCs�t|jt�st|j�|_t|j�t|j�}d}|r2|j�d�|j�d�f}||vr2|d7}||vs*|dkr^|rStt	t38|�����D]\}}|dkrR||vrR|}nqB|dkr^d}t|�|�|S)Nr�39backgroundZtransparencyr�rz$cannot allocate more than 256 colors)
r0rrrr r
�info�get�reversed�list�	enumerateZ	histogramr/)rr3r5�indexZspecial_colorsr$�countr2rrr�_new_color_indexts,4041���42zImagePalette._new_color_indexr%�tuple[int, ...]c43CsP|jr	d}t|��t|t�r�|jdkr,t|�dkr+|ddkr%d}t|��|dd�}n|jdkr;t|�dkr;|d	7}z|j|WSty�}zO|�||�}t|j	t44�sWJ�||j|<t|j�}||t|j�kr�|j	d||�t|�|j	|||d�|_	n	|j	t|�7_	d45|_
|WYd}~Sd}~wwdt|���}t|��)zgGiven an rgb tuple, allocate palette entry.46 47        .. warning:: This method is experimental.48        r-r����z/cannot add non-opaque RGBA color to RGB paletteNZRGBA�rErzunknown color specifier: )rr/r0r"r
r r&�KeyErrorrArrrr,r�repr)rr%r3r2r5r?r#rrr�getcolor�sF	4950�515253�����zImagePalette.getcolor�fp�
str | IO[str]c54Cs�|jr	d}t|��t|t�rt|d�}|�d�|�d|j�d��td�D]<}|�|��t|t|j�|dt|j��D]}z
|�d|j	|���Wq?t55y\|�d	�Yq?w|�d�q&|��d56S)zVSave palette to text file.57 58        .. warning:: This method is experimental.59        r-�wz60# Palette61z# Mode: �62r9r� z 0N)rr/r0r�open�writer
r!r r�63IndexError�close)rrJr2r$�jrrr�save�s"646566&�zImagePalette.save)rN)r
rrrrr)rr)rrrr)rr)r&rrr)rr)rr))rr,)NN)r3r4r5r6rr7r)r%rBr3r4rr7)rJrKrr)�__name__�67__module__�__qualname__�__doc__r�propertyr�setterr&r(r+r*ZtostringrArIrTrrrrrs,
�68697071��,rrr�datarrcCst�}||_||_d|_|S)Nr)rrrr)rr[rrrr�raw�s72r\�blackr7�white�float�	list[int]cs*|dkr�fdd�td�D�Sd}t|��)Nrcsg|]73}t�|d��qSrF�r7��.0r$�r^rr�74<listcomp>�sz#make_linear_lut.<locals>.<listcomp>r9z"unavailable when black is non-zero)r!�NotImplementedError)r]r^r2rrdr�make_linear_lut�srg�expcs�fdd�td�D�S)Ncs$g|]}t|d�dd��qS)g�o@g�?rarb�rhrrre�s$z"make_gamma_lut.<locals>.<listcomp>r9)r!rirrir�make_gamma_lut�srjrr
cs4ttdt����}|��t��fdd�|D��S)Nr9c�g|]}|t���qSr�r rb�r
rrre��znegative.<locals>.<listcomp>)r=r!r �reverser�r
rrrmr�negative�srqcs4ddlm��fdd�tdt|��D�}t||�S)Nr��randintcsg|]}�dd��qS)rrEr)rc�_rrrrre��zrandom.<locals>.<listcomp>r9)�randomrsr!r rrprrrrrv�s75rv�#fff0c0cs0dd�t�|�D��td�fdd�td�D��S)NcSsg|]}td|��qS)r)rg)rcZbandrrrre�ruzsepia.<locals>.<listcomp>rcs g|]}�|d|d�qS)rDrrb�Zbandsrrre�s i)rZgetrgbrr!rdrrxr�sepia�srycs,ttdt����}t��fdd�|D��S)Nr9crkrrlrbrmrrrernzwedge.<locals>.<listcomp>)r=r!r rrprrmr�wedge�srz�filename�tuple[bytes, str]cCs�t|d��9}tjtjtjg}|D]}z|�d�||���}|r#WnWqttfy/Yqwd}t|��Wd�|S1sAwY|S)N�rbrzcannot load palette)	rOrrr	�seekZ76getpalette�SyntaxErrorr/�OSError)r{rJZpaletteHandlersZpaletteHandlerZlutr2rrr�loads.��77���78��r�)rrr[rrr)r]r7r^r_rr`)rhr_rr`)r)r
rrr)rw)r^rrr)r{rrr|)�79__future__rr1�collections.abcr�typingr�rrrr	�
TYPE_CHECKINGr80rr\rgrjrqrvryrzr�rrrr�<module>s$8178283