o
    Uݢg                     @  sd   d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 dddZ
dd	 ZG d
d dZdS )z:Functions and classes for loading and manipulating images.    )annotationsN)ImageImageOpsjpegc                 C  sR   t | d}| }W d   n1 sw   Y  t|d}d| d| S )ziOpens a file using PIL and returns it encoded as a base64 string.

    :param filename:
    :return:
    rbNutf-8zdata:image/z;base64,)openreadbase64	b64encodedecode)filenamefmtZ
image_file
image_showencoded_string r   g/oak/stanford/groups/akundaje/marinovg/programs/cellranger-9.0.1/lib/python/cellranger/spatial/image.py_base64_encode_image   s
   
r   c                 C  s2   t j| \}}|d}|dkrd}t| |dS )zbase64 encode image..Zjpgr   )r   )ospathsplitextremoveprefixr   )fname_r   r   r   r   base64_encode_image   s
   
r   c                   @  s<   e Zd ZdZddddZedd Zdd
dZdddZdS )WebImagez>A class for working with and investigating simple image files.Nr   strc                 C  sx   t || _|| _t|}|j\| _| _W d   n1 sw   Y  |dur*|ndd| jd | jd g| _|| _	dS )aZ  Create a new instance that can base64 encode the image and give coordinates.

        :param filename: Image file name
        :param cropbox: optional [ x0, y0, x1, y1 ] just held as an attribute,
        :               defaults to whole image
        :param markersize: optional marker size for plotly for plotting a capture area spot
        Nr      )
r   _base64r   r   r   sizewidthheightcropbox
markersize)selfr   r#   r$   imgr   r   r   __init__)   s   
&
zWebImage.__init__c                 C  s   | j S )zC:return: String for a web summary,i.e. "data:image/jpg;base64,...".)r   )r%   r   r   r   base64_encoded_str8   s   zWebImage.base64_encoded_strreturnc              	   C  s   t | j*}t }t|j|dd | }W d   n1 s$w   Y  W d   n1 s3w   Y  t	
|d}d| S )z6Get encoded string of grascaled image for web summary.ZPNG)formatNr   zdata:image/png;base64,)r   r   r   ioBytesIOr   Z	grayscalesavegetvaluer
   r   r   )r%   r&   Z	img_bytesr   r   r   r   r   base64_encoded_grayscale_image=   s   


z'WebImage.base64_encoded_grayscale_imagec                 C  s   |s|st d|s| j| | j }n
|s| j| | j }tj| j\}}tjt	 d| }t
| j}|||ft
jj}W d   n1 sLw   Y  || |  t|S )z:param new_width: New image height.

        :param new_height: New image width
        :return: A base64 encoded string with the new image in it.
        z4Width and/or height must be set when resizing image.Ztmp_N)
ValueErrorr!   r"   r   r   splitr   jointempfilemkdtempr   r   resizeZ
ResamplingZLANCZOSr-   closer   )r%   Z	new_widthZ
new_heightr   r   tmp_filer&   Zimg2r   r   r   resize_and_encode_imageF   s   
z WebImage.resize_and_encode_image)NN)r   r   )r)   r   )	__name__
__module____qualname____doc__r'   propertyr(   r/   r8   r   r   r   r   r   &   s    

	r   )r   )r<   
__future__r   r
   r+   r   r3   ZPILr   r   r   r   r   r   r   r   r   <module>   s   
	