
    h,B                        % S SK r S SKrS SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
JrJrJrJrJr  S SKrS SKrSSKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJrJrJ r   \(       a
  S SK!r"SSK#J$r$  Sq%\\&\'      \(S'   \RR                  S:X  a  SOSr*\RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      \RV                  " S5      /r,\ " S  S!5      5       r-S"\&\'   4S# jr.S$S%S"\/4S& jr0S$S%S"\14S' jr2S(\Rf                  S"\14S) jr4S*\\&\'   \&\1   \&\Rf                     \&S%   4   S"\&\1   4S+ jr5g),    N)	dataclassfield)BytesIO)Path)TYPE_CHECKINGAnyClassVarOptionalUnion   )config)DownloadConfig)
array_cast)is_local_pathxopen)first_non_null_valueno_op_if_value_is_nullstring_to_dict   )FeatureType_IMAGE_COMPRESSION_FORMATSlittle<>z|b1|u1z<u2z>u2z<i2z>i2z<u4z>u4z<i4z>i4z<f4z>f4z<f8z>f8c                       \ rS rSr% SrSr\\   \S'   Sr	\
\S'   \" SSS9r\\   \S	'   S
r\\   \S'   \R                   " \R"                  " 5       \R$                  " 5       S.5      r\\   \S'   \" S SSS9r\\S'   S rS\\\\\\R8                  S
4   S\4S jrSS\SS
4S jjrS\S\\S4   4   4S jrS\\R@                  \RB                  \RD                  4   S\RB                  4S jr#SS\RB                  S\RB                  4S jjr$Sr%g)Image.   a  Image [`Feature`] to read image data from an image file.

Input: The Image feature accepts as input:
- A `str`: Absolute path to the image file (i.e. random access is allowed).
- A `pathlib.Path`: path to the image file (i.e. random access is allowed).
- A `dict` with the keys:

    - `path`: String with relative path of the image file to the archive file.
    - `bytes`: Bytes of the image file.

  This is useful for parquet or webdataset files which embed image files.

- An `np.ndarray`: NumPy array representing an image.
- A `PIL.Image.Image`: PIL image object.

Output: The Image features output data as `PIL.Image.Image` objects.

Args:
    mode (`str`, *optional*):
        The mode to convert the image to. If `None`, the native mode of the image is used.
    decode (`bool`, defaults to `True`):
        Whether to decode the image data. If `False`,
        returns the underlying dictionary in the format `{"path": image_path, "bytes": image_bytes}`.

Examples:

```py
>>> from datasets import load_dataset, Image
>>> ds = load_dataset("AI-Lab-Makerere/beans", split="train")
>>> ds.features["image"]
Image(decode=True, id=None)
>>> ds[0]["image"]
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x500 at 0x15E52E7F0>
>>> ds = ds.cast_column('image', Image(decode=False))
{'bytes': None,
 'path': '/root/.cache/huggingface/datasets/downloads/extracted/b0a21163f78769a2cf11f58dfc767fb458fc7cea5c05dccc0144a2c0f0bc1292/train/healthy/healthy_train.85.jpg'}
```
NmodeTdecodeF)defaultrepridPIL.Image.Imagedtypebytespathpa_type)r!   initr"   _typec                     U R                   $ N)r)   )selfs    Q/home/james-whalen/.local/lib/python3.13/site-packages/datasets/features/image.py__call__Image.__call___   s    ||    valuereturnc                 p   [         R                  (       a  SSKnO[        S5      e[	        U[
        5      (       a  [        R                  " U5      n[	        U[        5      (       a  USS.$ [	        U[        5      (       a  [        UR                  5       5      SS.$ [	        U[        [        45      (       a  SUS.$ [	        U[        R                  5      (       a  [        U5      $ [	        XR                  R                  5      (       a  [!        U5      $ UR#                  S5      b;  [$        R&                  R)                  US   5      (       a  SUR#                  S5      S.$ UR#                  S5      c  UR#                  S5      b#  UR#                  S5      UR#                  S5      S.$ [+        SU S	35      e)
zEncode example into a format for Arrow.

Args:
    value (`str`, `np.ndarray`, `PIL.Image.Image` or `dict`):
        Data passed as input to Image feature.

Returns:
    `dict` with "path" and "bytes" fields
r   N4To support encoding images, please install 'Pillow'.r(   r'   r(   r&   r'   zUAn image sample should have one of 'path' or 'bytes' but they are missing or None in .)r   PIL_AVAILABLE	PIL.ImageImportError
isinstancelistnparraystrr   absoluter'   	bytearrayndarrayencode_np_arrayr   encode_pil_imagegetosr(   isfile
ValueError)r.   r3   PILs      r/   encode_exampleImage.encode_exampleb   sX    TUUeT""HHUOEeS!!!D11t$$ 01DAAy122 511rzz**"5))yy//#E**YYv*rww~~eFm/L/L!599V+<==YYw+uyy/@/L"YYw/69JKKghmgnnop r2   c                    U R                   (       d  [        S5      e[        R                  (       a	  SSKnSSKnO[        S5      eUc  0 nUS   US   pTUGc  Uc  [        SU S35      e[        U5      (       a  UR                  R                  U5      nOUR                  S	5      S
   nUR                  [        R                  5      (       a  [        R                  O[        R                  n[!        Xx5      n	U	b  UR#                  U	S   5      OSn
[%        U
S9n['        USUS9 n[)        UR+                  5       5      nSSS5        UR                  R                  U5      nO$UR                  R                  [)        U5      5      nUR-                  5         UR/                  5       R#                  UR                  R0                  R2                  R4                  5      b  UR6                  R9                  U5      nU R:                  (       a5  U R:                  UR:                  :w  a  UR=                  U R:                  5      nU$ ! , (       d  f       GN
= f)a	  Decode example image file into image data.

Args:
    value (`str` or `dict`):
        A string with the absolute image file path, a dictionary with
        keys:

        - `path`: String with absolute or relative image file path.
        - `bytes`: The bytes of the image file.
    token_per_repo_id (`dict`, *optional*):
        To access and decode
        image files from private repositories on the Hub, you can pass
        a dictionary repo_id (`str`) -> token (`bool` or `str`).

Returns:
    `PIL.Image.Image`
zMDecoding is disabled for this feature. Please use Image(decode=True) instead.r   Nz4To support decoding images, please install 'Pillow'.r(   r'   zCAn image should have one of 'path' or 'bytes' but both are None in r8   ::repo_idtokenrbdownload_config)r    RuntimeErrorr   r9   r:   PIL.ImageOpsr;   rI   r   r   opensplit
startswithHF_ENDPOINTHUB_DATASETS_URLHUB_DATASETS_HFFS_URLr   rF   r   r   r   readloadgetexifExifTagsBaseOrientationImageOpsexif_transposer   convert)r.   r3   token_per_repo_idrJ   r(   bytes_image
source_urlpatternsource_url_fieldsrR   rU   fs                r/   decode_exampleImage.decode_example   s   $ {{nooTUU$ "V}eGnf>| #fglfmmn!opp &&IINN40E!%D!1"!5J &001C1CDD //#99 
 )7z(K%O`Ol)--.?	.JKrv  '55&AOtT?Kq!(!2 LIINN62EIINN76?3E

==?syy1166BBCOLL//6E99ejj0MM$)),E LKs   &I
Ir   c                 V    SSK Jn  U R                  (       a  U $ U" S5      U" S5      S.$ )zfIf in the decodable state, return the feature itself, otherwise flatten the feature into a dictionary.r   )Valuebinarystringr&   )featuresrq   r    )r.   rq   s     r/   flattenImage.flatten   s2    # {{ 	
 xh	
r2   storagec           
         [         R                  R                  UR                  5      (       a%   UR	                  [         R
                  " 5       5      n[         R                  R                  UR                  5      (       ag  [         R                  " S/[        U5      -  [         R                  " 5       S9n[         R                  R                  X1/SS/UR                  5       S9nGO[         R                  R                  UR                  5      (       ag  [         R                  " S/[        U5      -  [         R
                  " 5       S9n[         R                  R                  X/SS/UR                  5       S9nGO)[         R                  R!                  UR                  5      (       a  UR                  R#                  S5      S:  a  UR%                  S5      nO5[         R                  " S/[        U5      -  [         R                  " 5       S9nUR                  R#                  S5      S:  a  UR%                  S5      nO5[         R                  " S/[        U5      -  [         R
                  " 5       S9n[         R                  R                  X4/SS/UR                  5       S9nO[         R                  R'                  UR                  5      (       a  [         R                  " UR)                  5        Vs/ s H)  oUb"  [+        [,        R                  " U5      5      S   OSPM+     sn[         R                  " 5       S9n[         R                  " S/[        U5      -  [         R
                  " 5       S9n[         R                  R                  X4/SS/UR                  5       S9n[/        XR0                  5      $ ! [         R                   a  n[        SU 35      UeSnAff = fs  snf )a  Cast an Arrow array to the Image arrow storage type.
The Arrow types that can be converted to the Image pyarrow storage type are:

- `pa.string()` - it must contain the "path" data
- `pa.large_string()` - it must contain the "path" data (will be cast to string if possible)
- `pa.binary()` - it must contain the image bytes
- `pa.struct({"bytes": pa.binary()})`
- `pa.struct({"path": pa.string()})`
- `pa.struct({"bytes": pa.binary(), "path": pa.string()})`  - order doesn't matter
- `pa.list(*)` - it must contain the image array data

Args:
    storage (`Union[pa.StringArray, pa.StructArray, pa.ListArray]`):
        PyArrow array to cast.

Returns:
    `pa.StructArray`: Array in the Image arrow storage type, that is
        `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
zvFailed to cast large_string to string for Image feature. This can happen if string values exceed 2GB. Original error: Ntyper'   r(   maskr   )patypesis_large_stringrz   castrs   ArrowInvalidrI   	is_stringr?   lenrr   StructArrayfrom_arraysis_null	is_binary	is_structget_field_indexr   is_list	to_pylistrD   r>   r   r)   )r.   rw   ebytes_array
path_arrayarrs         r/   cast_storageImage.cast_storage   s   ( 88##GLL11!,,ryy{3 88gll++((D6CL#8ryy{KKnn00+1G'SYIZahapapar0sGXX--4&3w<"7biikJJnn00'1FRXHY`g`o`o`q0rGXX--||++G49%mmG4 hhvG'<299;O||++F3q8$]]62
XXtfs7|&;"))+N
nn00+1JWV\L]dkdsdsdu0vGXXgll++((ahararatuatZ]O#/8QUUatuYY[K 4&3w<"7biikJJnn00)GV+<;CVCVCX 1 G '<<00? ??  ''(c+ 	0 vs   $N< 90O'<O$OO$c           	      x  ^ Tc  0 m[         U4S j5       n[        R                  " UR                  5        Vs/ s H  nUb  US   c  U" US   5      OUS   OSPM      sn[        R                  " 5       S9n[        R                  " UR                  S5      R                  5        Vs/ s H&  ofb  [        R                  R                  U5      OSPM(     sn[        R                  " 5       S9n[        R                  R                  XW/SS/UR                  5       S9n[        XR                  5      $ s  snf s  snf )a   Embed image files into the Arrow array.

Args:
    storage (`pa.StructArray`):
        PyArrow array to embed.

Returns:
    `pa.StructArray`: Array in the Image arrow storage type, that is
        `pa.struct({"bytes": pa.binary(), "path": pa.string()})`.
Nc                 z  > U R                  S5      S   nUR                  [        R                  5      (       a  [        R                  O[        R
                  n[        X5      nUb  TR                  US   5      OS n[        US9n[        U SUS9 nUR                  5       sS S S 5        $ ! , (       d  f       g = f)NrN   rO   rP   rQ   rS   rT   )rY   rZ   r   r[   r\   r]   r   rF   r   r   r^   )r(   rj   rk   rl   rR   rU   rm   rg   s          r/   path_to_bytes*Image.embed_storage.<locals>.path_to_bytes  s    D)"-J+5+@+@ASAS+T+T''Z`ZvZv  !/z CK\Kh%))*;I*FGnrE,59OtT?Cqvvx DCCs   B,,
B:r'   r(   ry   r{   )r   r}   r?   r   rr   r   rG   r(   basenamers   r   r   r   r   r)   )r.   rw   rg   r   xr   r(   r   s     `     r/   embed_storageImage.embed_storage  s*    $ "			  
 		  hh !**,,A UVTaQwZ-?qy)QwZgkk, 
 XXNUmm\bNcNmNmNopNod'7RWWd#TANop

 ..,,k-FRXHY`k`s`s`u,v'<<00 qs   %D2'-D7 r-   )&__name__
__module____qualname____firstlineno____doc__r   r
   r@   __annotations__r    boolr   r#   r%   r	   r}   structrr   rs   r)   r   r+   r0   r   r'   rB   dictr>   rC   rK   rn   ru   StringArrayr   	ListArrayr   r   __static_attributes__r   r2   r/   r   r   .   s<   %N D(3-FDd7B7,E8C=,YYbiik'RSGXc]SwU?E3?'E#uirzzSd*d$e 'jn 'R;D ;EV ;z
}d33E.FFG 
61E".."..",,*V$W 61\^\j\j 61p&1R^^ &1PRP^P^ &1 &1r2   r   r4   c                  b   [         R                  (       a  SS Kn O[        S5      e[        c~  U R
                  R                  5         [        [        U R
                  R                  R                  5       5      [        U R
                  R                  R                  5       5      -  5      q[        $ )Nr   r6   )r   r9   r:   r;   r   r   r*   r=   setOPENkeysSAVE)rJ   s    r/   list_image_compression_formatsr   6  sr    PQQ ")		%)#ciinn.A.A.C*Ds399>>K^K^K`Ga*a%b"%%r2   ri   r$   c                     [        5       nU R                  [        5       ;   a  U R                  nOU R                  S;   a  SOSnU R	                  XS9  UR                  5       $ )zmConvert a PIL Image object to bytes using native compression if possible, otherwise use PNG/TIFF compression.)1LLARGBRGBAPNGTIFF)format)r   r   r   r   savegetvalue)ri   bufferr   s      r/   image_to_bytesr   C  sP    YF||577**(GGV	JJvJ%??r2   c                 ~    [        U S5      (       a  U R                  S:w  a  U R                  S S.$ S [        U 5      S.$ )Nfilename r7   )hasattrr   r   )ri   s    r/   rE   rE   N  s9    uj!!enn&:66~e'<==r2   r?   c                 J   [         R                  (       a  SS KnO[        S5      eU R                  nUR
                  S:w  a  UR
                  O[        nUR                  nUR                  nS nU R                  SS  (       aQ  US;  a  [        SU SU S35      e[        R                  " S	5      nX&:w  a  [        R                  " S
U SU S35        OU[        ;   a  UnOUS:  ao  X4-   [        U5      -   n[        R                  " U5      [        ;   a4  [        R                  " U5      n[        R                  " S
U SU S35        OUS-  nUS:  a  Mo  Uc  [        SU S[         35      eUR                   R#                  U R%                  U5      5      nS ['        U5      S.$ )Nr   r6   =r   )uizUnsupported array dtype z for image encoding. Only z' is supported for multi-channel arrays.r   zDowncasting array dtype z to z to be compatible with 'Pillow'r   zCannot downcast dtype z- to a valid image dtype. Valid image dtypes: r7   )r   r9   r:   r;   r%   	byteorder_NATIVE_BYTEORDERkinditemsizeshape	TypeErrorr>   warningswarn_VALID_IMAGE_ARRAY_DTPYESr@   r   	fromarrayastyper   )	r?   rJ   r%   dtype_byteorder
dtype_kinddtype_itemsize
dest_dtype	dtype_strri   s	            r/   rD   rD   U  s   PQQKKE).C)?eooEVOJ^^NJ {{12Z'*5'1KJ<W~  XXe_
MM4UG4
|Kjkl	+	+
!'4s>7JJIxx	"&??XXi0
 8tJ<Onop1$ ! (/\]v\wx  IIZ 89E>%#899r2   objsc                    [         R                  (       a  SSKnO[        S5      eU (       a  [	        U 5      u  p#[        U[        5      (       a  U  Vs/ s H  o3b  USS.OSPM     sn$ [        U[        R                  5      (       a'  [        [        5      nU  Vs/ s H
  o4" U5      PM     sn$ [        X1R                  R                  5      (       a'  [        [        5      nU  Vs/ s H
  o4" U5      PM     sn$ U $ U $ s  snf s  snf s  snf )zmEncode a list of objects into a format suitable for creating an extension array of type `ImageExtensionType`.r   Nr6   r7   )r   r9   r:   r;   r   r<   r@   r>   rC   r   rD   r   rE   )r   rJ   _objobj_to_image_dict_funcs        r/   objects_to_list_of_image_dictsr     s     PQQ%d+c3Y]^Y]RUOS40MY]^^c2::&&%;O%L";?@4C*3/4@@YY__--%;<L%M";?@4C*3/4@@K _ A As   C>D&D)6rG   sysr   dataclassesr   r   ior   pathlibr   typingr   r   r	   r
   r   numpyr>   pyarrowr}   r   r   download.download_configr   tabler   utils.file_utilsr   r   utils.py_utilsr   r   r   r:   rJ   rt   r   r   r=   r@   r   r   r   r%   r   r   r   r'   r   r   rE   rC   rD   r   r   r2   r/   <module>r      s   	 
  (   @ @    5  3 Y Y % 37 HT#Y/ 6==H4C#  HHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUOHHUO $ D1 D1 D1N
&S	 
&+  >- >$ >(:2:: (:$ (:V
S	4:tBJJ'7>O9PP
Q	$Zr2   