o
    3g7                     @  sp   d dl mZ d dlZd dlmZ ddlmZ ddlmZ G dd	 d	Z	G d
d dej
Zedkr6e  dS dS )    )annotationsN)colors   )cli   )get_terminal_sizec                   @  sH   e Zd Zd ZdddZdd Zdd	d
Zdd Zdd Z	dd Z
dS )Imagezsize char_ratioN皙@c                 C  s   || _ || _d S N)size
char_ratio)selfr   r    r   I/home/garg/my-data/venv/lib/python3.10/site-packages/plumbum/cli/image.py__init__   s   
zImage.__init__c                 C  sh   | j s|S |d |d  | j  }t|d | |d kr(t|d | |d fS |d t|d | fS )zSelect a best possible size matching the original aspect ratio.
        Size is width, height.
        The char_ratio option gives the height of each char with respect
        to its width, zero for no effect.r   r   )r   int)r   origterm
orig_ratior   r   r   best_aspect   s   zImage.best_aspectFc                 C  s0   ddl }|r| |j|S | |j|S )zUDisplay an image on the command line. Can select a size or show in double resolution.r   N)	PIL.Imageshow_pil_doubler   openshow_pil)r   filenamedoublePILr   r   r   show"   s   z
Image.showc                 C  s$   | j du rt }| |j |S | j S )zReturn the expected image sizeN)r   r   r   )r   im	term_sizer   r   r   
_init_size-   s   
zImage._init_sizec                 C  s   |  |}||d}t|d D ])}t|d d D ]}|||f}tjtj	j
| d  qtjtjd  qtjtjd  tj  dS )zStandard show routineRGBr   r     

N)r    resizeconvertrangegetpixelsysstdoutwriter   bgrgbresetflush)r   r   r   new_imyxpixr   r   r   r   5   s   
zImage.show_pilc                 C  s   |  |}|d |d d f}||d}t|d d D ]<}t|d d D ](}|||d f}|||d d f}tjtj	j
| tjj
| @ d  q)tjtjd  qtjtjd  tj  dS )	z$Show double resolution on some fontsr   r   r   r!   u   ▀r#   r$   N)r    r%   r&   r'   r(   r)   r*   r+   r   r,   r-   fgr.   r/   )r   r   r   r0   r1   r2   r3   pixlr   r   r   r   B   s   
zImage.show_pil_double)Nr	   )F)__name__
__module____qualname__split	__slots__r   r   r   r    r   r   r   r   r   r   r      s    

r   c                   @  s   e Zd ZdZejddgddZejddgedd	d
ddd Z	ej
ddgddZej
dgedddZeejdd ZdS )ShowImageAppz Display an image on the terminalz-dz--doublez3Double resolution (looks good only with some fonts))helpz-cz--colorsr      zLevel of color, 1-4c                 C  s
   |t _d S r
   )r   	use_color)r   nr   r   r   
colors_set\   s   
zShowImageApp.colors_setz-sz--sizez#Size, should be in the form 100x150z--ratior	   zAspect ratio of the font)defaultr<   c                 C  s6   d }| j rtt| j d}t|| j|| j d S )Nr2   )r   mapr   r9   r   ratior   r   )r   r   r   r   r   r   mainf   s   zShowImageApp.mainN)r6   r7   r8   __doc__r   Flagr   switchRanger@   
SwitchAttrr   floatrC   
positionalExistingFilerD   r   r   r   r   r;   U   s    


r;   __main__)
__future__r   r)   plumbumr    r   termsizer   r   Applicationr;   r6   runr   r   r   r   <module>   s    J