U
    mÇfS
  ã                   @   s>   d Z ddlZddlZddlmZ G dd„ deƒZd	dd„ZdS )
zO
Utility functions for handling images.

Requires Pillow as you might imagine.
é    N)ÚFilec                   @   s0   e Zd ZdZedd„ ƒZedd„ ƒZdd„ ZdS )	Ú	ImageFilez€
    A mixin for use alongside django.core.files.base.File, which provides
    additional features for dealing with images.
    c                 C   s   |   ¡ d S )Nr   ©Ú_get_image_dimensions©Úself© r   ú</tmp/pip-unpacked-wheel-siwebuq3/django/core/files/images.pyÚwidth   s    zImageFile.widthc                 C   s   |   ¡ d S )Né   r   r   r   r   r	   Úheight   s    zImageFile.heightc                 C   s,   t | dƒs&| j}|  ¡  t| |d| _| jS )NÚ_dimensions_cache)Úclose)ÚhasattrÚclosedÚopenÚget_image_dimensionsr   )r   r   r   r   r	   r      s
    
zImageFile._get_image_dimensionsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr
   r   r   r   r   r   r	   r      s   

r   Fc           	   
   C   s*  ddl m} | ¡ }t| dƒr6| }| ¡ }| d¡ n*zt| dƒ}W n tk
rZ   Y dS X d}z¨d}| 	|¡}|sxqz| 
|¡ W n^ tjk
r¾ } z|jd  d¡r¬n‚ W 5 d	}~X Y n( tjk
rÒ   Y n tk
rä   Y nX |jrø|jjW ¢S |d
9 }qfW ¢dS |r| ¡  n
| |¡ X d	S )z®
    Return the (width, height) of an image, given an open file or a path.  Set
    'close' to True to close the file at the end if it is initially in an open
    state.
    r   )r   ÚreadÚrb)NNTi   zError -5Né   )ZPILr   ÚParserr   ÚtellÚseekr   ÚOSErrorr   r   ÚfeedÚzlibÚerrorÚargsÚ
startswithÚstructÚRuntimeErrorÚimageÚsize)	Zfile_or_pathr   ZPillowImageFileÚpÚfileZfile_posÚ
chunk_sizeÚdataÚer   r   r	   r   "   sB    



r   )F)r   r$   r    Zdjango.core.filesr   r   r   r   r   r   r	   Ú<module>   s
   