
    j                     v    d dl mZmZmZ d dlZd dlZddlmZ ddlm	Z	m
Z
 ddlmZ dd	gZg d
Z G d de	      Zy)    )BinaryIOAnyUnionN   )exiftool_metadata   )DocumentConverterDocumentConverterResult)
StreamInfoz
image/jpegz	image/png)z.jpgz.jpegz.pngc            	       d    e Zd ZdZdedededefdZdededede	fdZ
dd	dedededef   fd
Zy)ImageConverterz
    Converts images to markdown via extraction of metadata (if `exiftool` is installed), and description via a multimodal LLM (if an llm_client is configured).
    file_streamstream_infokwargsreturnc                     |j                   xs dj                         }|j                  xs dj                         }|t        v ryt        D ]  }|j                  |      s y y)N TF)mimetypelower	extensionACCEPTED_FILE_EXTENSIONSACCEPTED_MIME_TYPE_PREFIXES
startswith)selfr   r   r   r   r   prefixs          g/root/aria/tools/markitdown-venv/lib/python3.12/site-packages/markitdown/converters/_image_converter.pyacceptszImageConverter.accepts   sg      ((.B557 **0b779	001 	F""6*	     c           	      Z   d}t        ||j                  d            }|rdD ]  }||v s|| d||    dz  } |j                  d      }|j                  d      }|B|@| j                  |||||j                  d	      
      }	|	|d|	j                         z   dz   z  }t	        |      S )Nr   exiftool_path)r    )
	ImageSizeTitleCaptionDescriptionKeywordsArtistAuthorDateTimeOriginal
CreateDateGPSPositionz: 

llm_client	llm_model
llm_prompt)clientmodelpromptz
# Description:
)markdown)r   get_get_llm_descriptionstripr
   )
r   r   r   r   
md_contentmetadatafr,   r-   llm_descriptions
             r   convertzImageConverter.convert'   s     
 %vzz/'B
  : =QCr(1+b"99J:  ZZ-
JJ{+	!i&;"77!zz,/ 8 O *2_5J5J5LLtSS
&
 	
r   N)r1   c                   ||j                         dk(  rd}|j                  }|s)t        j                  d|j                  xs dz         \  }}|sd}|j                         }	 t        j                  |j                               j                  d      }		 |j                  |       d| d|	 }dd	|d
dd|idgdg}|j                  j                  j                  ||      }|j                  d   j                   j"                  S # t        $ r}
Y d }
~
|j                  |       y d }
~
ww xY w# |j                  |       w xY w)Nr   z(Write a detailed caption for this image._dummyzapplication/octet-streamzutf-8zdata:z;base64,usertext)typer>   	image_urlurl)r?   r@   )rolecontent)r0   messagesr   )r5   r   	mimetypes
guess_typer   tellbase64	b64encodereaddecode	ExceptionseekchatcompletionscreatechoicesmessagerC   )r   r   r   r/   r0   r1   content_type_cur_posbase64_imageedata_urirD   responses                 r   r4   z#ImageConverter._get_llm_descriptionW   s[    >V\\^r1?F #++'22K117R8OL! 5L ""$	&!++K,<,<,>?FFwOL W% <.?
 #V4 +!8&
  ;;**111Q"**2225  	W%	 W%s$   $2D
 
	D.D1 )D..D1 1E)__name__
__module____qualname____doc__r   r   r   boolr   r
   r:   r   strr4    r   r   r   r      s       	
 
$.
.
  .
 	.

 
!.
n 3333  33 
tSy	33r   r   )typingr   r   r   rH   rE   	_exiftoolr   _base_converterr	   r
   _stream_infor   r   r   r   r`   r   r   <module>re      sA    ' '   ( H %  
 5 z3& z3r   