a
    �˜ëj½  ã                   @   sj   d dl Z d dlZd dlmZ d dlZd dlmZ d dl	m
Z
mZ d dlmZ e  e¡Zdd„ Zdd„ ZdS )	é    N)ÚPath)Ú	add_inputÚ	get_input)ÚBooleanOptionalActionc                 C   sv   t | dd� | jdddtdd� | jdtdd	d
� | jdddd� | jddd� | jdddd� | jdtddd� d S )NT)Úpagesz--output-dirz-oz-Output directory to take the extracted images)ÚrequiredÚtypeÚhelpz--max-depthé   zAMaximum recursion depth to consider when looking for pageobjects.)r   Údefaultr	   z--use-bitmapÚ
store_truezREnforce the use of bitmaps rather than attempting a smart extraction of the image.)Úactionr	   z--formatzNImage format to use when saving bitmaps. (Fallback if doing smart extraction.))r	   z--renderznWhen --use-bitmap is given, whether to get rendered bitmaps, taking masks and transform matrices into account.z--scale-to-originalzËWhen --use-bitmap --render is given, whether to scale the image so it is rendered at its native resolution, or close to that. This should improve output quality. The default is True, but you may opt out.)r   r   r	   )r   Úadd_argumentr   Úintr   )Úparser© r   ú[/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pypdfium2_cli/extract_images.pyÚattach   sB    üüýþýür   c              
   C   s:  | j  ¡ st| j ƒ‚| jr&| js&d| _t| ƒ}ttt| j	ƒd ƒƒ}| j	D ]ê}|| }|j
tjf| jd�}t|ƒ}ttt|ƒƒƒ}t|ƒD ]¨\}}d||d ||d f }	| j | jj› d|	› � }
zH| jrø|j| j| jd� ¡ }| |
› d| j› �¡ n|j|
| jd� W n tj�y(   t ¡  Y n0 | ¡  qŠqJd S )	NZpngé   )ÚfilterÚ	max_depthz	%0*d_%0*dÚ_)ÚrenderÚscale_to_originalÚ.)Z	fb_format)Ú
output_dirÚis_dirÚNotADirectoryErrorZ
use_bitmapÚformatr   ÚlenÚstrÚmaxr   Zget_objectsÚpdfium_cZFPDF_PAGEOBJ_IMAGEr   ÚlistÚ	enumerateÚinputÚstemZ
get_bitmapr   r   Zto_pilÚsaveÚextractÚpdfiumZPdfiumErrorÚ	tracebackÚ	print_excÚclose)ÚargsZpdfZ	n_pdigitsÚiÚpageZimagesZ	n_idigitsÚjÚimageÚtagÚprefixZ	pil_imager   r   r   Úmain5   s2    


þr4   )Úloggingr*   Úpathlibr   Z	pypdfium2r)   Zpypdfium2.rawÚrawr"   Zpypdfium2_cli._parsersr   r   Zpypdfium2_cfg.stlr   Ú	getLoggerÚ__name__Úloggerr   r4   r   r   r   r   Ú<module>   s   
$