
    g                        d dl Z d dlmZ d dlmZmZmZmZmZ d dl	Z
d dlZd dlmZmZmZ d dlmZ  e         e j"                  e      Zdedeeef   fdZdd	ed
ededefdZ	 ddeeef   dededee
j2                  ee   ef   fdZy)    N)Path)AnyDictListTupleUnion)ExifTagsImageTiffTags)register_heif_openerimg_pilreturnc                    | j                         j                  d      }|j                         D ci c],  \  }}|t        j                  v st        j                  |   |. }}}| j                         }|j                         D ci c]5  \  }}|t
        j                  v rt
        j                  |   j                  |7 }}}i ||S c c}}w c c}}w )zReturn exif information as a dictionary.

    Args:
    ----
        img_pil: A Pillow image.

    Returns:
    -------
        A dictionary with extracted EXIF information.

    ii  )getexifget_ifditemsr	   TAGSr   TAGS_V2name)r   img_exifkv	exif_dict	tiff_tags	tiff_dicts          </home/cameronsmith/repos/ml-depth-pro/src/depth_pro/utils.pyextract_exifr      s      ((0H191AXAQ(--EWq!1$XIX!I OO%Aq    	  !#I 
 &i%9%% Ys   CC
:Cwidthheightf_mmc                 p    |t        j                  | dz  |dz  z         z  t        j                  d      z  S )zDConvert a focal length given in mm (35mm film equivalent) to pixels.g       @iP  )npsqrt)r   r   r    s      r   fpx_from_f35r$   *   s1    "''%*vs{233bggm6LLL    pathauto_rotateremove_alphac           
         t         j                  d|  d       t        |       } | j                  j	                         dv r(t        j                  | d      }|j                         }nt        j                  |       }t        |      }|j                  j                  dd      }|r|j                  dd	      }|d
k(  r |j                  t        j                        }nh|dk(  r |j                  t        j                        }nC|dk(  r |j                  t        j                         }n|d	k7  rt         j#                  d| d       t%        j&                  |      }|j(                  d
k  s|j*                  d   d	k(  rt%        j,                  |||f      }|r|dddddd
f   }t         j                  d|j*                  d    d|j*                  d	           |j                  d|j                  d|j                  dd                  }	|	F|	dkD  rAt         j                  d|	 d       t/        |j*                  d	   |j*                  d   |	      }
nd}
|||
fS )a  Load an RGB image.

    Args:
    ----
        path: The url to the image to load.
        auto_rotate: Rotate the image based on the EXIF data, default is True.
        remove_alpha: Remove the alpha channel, default is True.

    Returns:
    -------
        img: The image loaded as a numpy array.
        icc_profile: The color profile of the image.
        f_px: The optional focal length in pixels, extracting from the exif data.

    zLoading image z ...)z.heicT)convert_hdr_to_8biticc_profileNOrientation            zIgnoring image orientation .   z	HxW: r   xFocalLengthIn35mmFilmFocalLenIn35mmFilmFocalLengthIn35mmFormatz	focal length @ 35mm film: mm)LOGGERdebugr   suffixlowerpillow_heif	open_heif	to_pillowr
   openr   infoget	transpose
ROTATE_180
ROTATE_270	ROTATE_90warningr"   arrayndimshapedstackr$   )r&   r'   r(   	heif_filer   r   r+   exif_orientationimgf_35mmf_pxs              r   load_rgbrP   /   s   $ LL>$t,-:D{{i'))$DI	%%'**T"G$H,,""=$7K #<<q9q ''(8(89G"''(8(89G"''8G"NN89I8J!LM
((7
C
xx!|syy|q(iic3(!Q(m
LL7399Q<.#))A,89 \\ (,,/H$"O	
F fqj3F82>?CIIaL#))A,?T!!r%   )2   )TT)loggingpathlibr   typingr   r   r   r   r   numpyr"   r<   PILr	   r
   r   r   	getLogger__name__r8   strr   floatr$   boolndarraybytesrP    r%   r   <module>r_      s      0 0   ) ) ,  			8	$&% &DcN &4M Mu ME M5 M LPA"
c	
A")-A"DHA"
2::tE{E)*A"r%   