
    ⨽g.                         d Z ddlZddlZddlmZmZmZ  ej                  e	      Z
dZ G d de      Z G d de      Zd	 Z G d
 d      Zy)z"
PIL formats for multiple images.
    N   )PillowFormatimage_as_uintndarray_to_pilc                       e Zd ZdZdZy)
TIFFFormatizTIFF format (Pillow)N)__name__
__module____qualname___modes_description     d/var/www/it7/html/youtubeDownloader/venv/lib/python3.12/site-packages/imageio/plugins/pillowmulti.pyr   r      s    F)Lr   r   c                   B    e Zd ZdZdZdZ G d dej                        Zy)	GIFFormatz(See :mod:`imageio.plugins.pillow_legacy`iIz Static and animated gif (Pillow)c                   ,    e Zd Z	 	 	 	 	 	 ddZd Zd Zy)GIFFormat.WriterNc           	         ddl m} t        d |j                  d      D              \  }}	}
|dk(  r|	dk\  rt	        d| d      t        |      }|d	k  s|d
kD  rt        d      |dvr<d	t        t        j                  d      dz         z  }t        j                  d|z         |dt        |      z  | _        nEt        |t        t        f      r|D cg c]  }t        |       c}| _        nt        |      | _        t        |      }|dk  s|t        d      k(  rd}t        |      }t        |      }|rdnd	| _        | j"                  j%                         }t'        ||||t        |            | _        y c c}w )Nr   )__version__c              3   2   K   | ]  }t        |        y wN)int).0xs     r   	<genexpr>z)GIFFormat.Writer._open.<locals>.<genexpr>)   s     'R1A'Rs   .
   r   zPillow vz is not supported by ImageIO's legacy pillow plugin when writing GIFs. Consider switching to the new plugin or downgrading to `pillow<10.1.0`.      z!GIF quantize param must be 2..256)r!                @      r"   r(   g+?zDWarning: palettesize (%r) modified to a factor of two between 2-256.g      ?inf)PILr   tuplesplitImportErrorr   
ValueErrornplog2loggerwarningfloat	_duration
isinstancelistbool_disposerequestget_file	GifWriter_writer)selfloopdurationfpspalettesize	quantizersubrectanglespillow_versionmajorminorpatchdfps                r   _openzGIFFormat.Writer._open   su    :"''R8L8LS8Q'R"RE5%{uz!~. /@ @  k*KQ+"3 !DEE"AA3rwws|e';#<<)+67
 !$uSz!1HtUm44<!=q%(!=!&x;DqyDE%L0t9D /M!.AADM &&(B$M4C4DDL ">s   E9c                 8    | j                   j                          y r   )r<   closer=   s    r   _closezGIFFormat.Writer._closeQ   s    LL r   c                 `   t        |d      }|j                  dk(  r|j                  d   dk(  r|d d d d df   }| j                  }t	        |t
              r/|t        t        |      dz
  | j                  j                           }| j                  }| j                  j                  |||       y )Nr$   )bitdepth   r   r   )r   ndimshaper4   r5   r6   minlenr<   _countr8   	add_image)r=   immetar?   disposes        r   _append_datazGIFFormat.Writer._append_dataT   s    rA.Bww!| 11a[~~H(D)#CH(94<<;N;N$OPmmGLL""2x9r   )r   Nr    r"   r   F)r
   r   r   rJ   rN   r\   r   r   r   Writerr      s&     1	f	!
	r   r]   N)r
   r   r   __doc__r   r   r   r]   r   r   r   r   r      s#    2F5LA$$ Ar   r   c                 (    | j                  dd      S )Nr!   little)	byteorder)to_bytes)r	   s    r   intToBinrc   a   s    ::a8:,,r   c                   b    e Zd ZdZ	 	 	 	 ddZd Zd Zd Zd Zd Z	dd	Z
d
 ZddZd ZddZy)r;   zClass that for helping write the animated GIF file. This is based on
    code from images2gif.py (part of visvis). The version here is modified
    to allow streamed writing.
    c                     || _         || _        || _        || _        || _        d | _        d | _        d| _        ddlm	} || _	        y )Nr   )getdata)
rI   opt_subrectangleopt_loopopt_quantizeropt_palette_size_previous_image_global_paletterW   PIL.GifImagePluginrf   )r=   filerg   rh   ri   rj   rf   s          r   __init__zGifWriter.__init__k   sK      0 * 0##.r   c                    |d}}| j                   r| j                  |      \  }}| j                  || j                  | j                        }ddlm}  ||      d   d   }| j                  dk(  r$| j                  ||| j                         || _
        | j                  |||||       || _        | xj                  dz  c_        y )Nr   r   r   )	getheaderrQ   r   )rg   getSubRectangleconverToPILri   rj   rm   rr   rW   write_headerrh   rl   write_imagerk   )	r=   rY   r?   r[   im_rectrectim_pilrr   palettes	            r   rX   zGifWriter.add_image   s    F   004MGT!!'4+=+=t?T?TU
 	1F#A&q) ;;!fgt}}=#*D $'B  "qr   c                     | j                  |      }| j                  |      }| j                  j                  |       | j                  j                  |       | j                  j                  |       y r   )getheaderAnim	getAppExtrI   write)r=   rY   globalPaletter>   headerappexts         r   ru   zGifWriter.write_header   sP    ##B'%fm$fr   c                 X    | j                   j                  dj                  d             y )N;utf-8)rI   r~   encoderM   s    r   rL   zGifWriter.close   s    cjj)*r   c                 :   | j                   }| j                  |      }d}|r3t        |      dk  r%||j                  d      z  }|rt        |      dk  r%t        |      dk(  sJ | j	                  ||      }	| j                  ||      }
|| j                  k7  s|dk7  rE|j                  |
       |j                  |	       |j                  |       |j                  d       n"|j                  |
       |j                  |       |D ]  }|j                  |        y )Nr      r   r!      )rI   rf   rV   popgetImageDescriptorgetGraphicsControlExtrl   r~   )r=   rY   rz   rx   r?   r[   rI   dataimdeslidgraphextrH   s               r   rv   zGifWriter.write_image   s    WW ||Bs5zBTXXa[ E s5zB5zR %%b$/--h@ t+++AHHXHHSMHHWHHW HHXHHUO  	AHHQK	r   c                     d}|t        |j                  d         z  }|t        |j                  d         z  }|dz  }|S )z4Get animation header. To replace PILs getheader()[0]s   GIF89ar   r   s     rc   size)r=   rY   bbs      r   r|   zGifWriter.getheaderAnim   sD    
hrwwqz""
hrwwqz""
o	r   Nc                     |d}d}|t        |d         z  }|t        |d         z  }|t        |j                  d         z  }|t        |j                  d         z  }|dz  }|S )a  Used for the local color table properties per image.
        Otherwise global color table applies to all frames irrespective of
        whether additional colors comes in play that require a redefined
        palette. Still a maximum of 256 color per frame, obviously.

        Written by Ant1 on 2010-08-22
        Modified by Alex Robinson in Janurari 2011 to implement subrectangles.
        rq      ,r   r      r   )r=   rY   xyr   s       r   r   zGifWriter.getImageDescriptor   s{     :B  	hr!uo
hr!uo
hrwwqz""
hrwwqz"" 	g 	r   c                 l    |dk(  ry|dk(  rd}d}|dk7  rd}|dz  }|dz  }|t        |      z  }|dz  }|S )	z|Application extension. This part specifies the amount of loops.
        If loop is 0 or inf, it goes on infinitely.
        r   r   r   i  s   !s   NETSCAPE2.0s       )rc   )r=   r>   r   s      r   r}   zGifWriter.getAppExt   s[     1919D19 B. B+B(4. B'MB	r   c                     d}|t        |dz  dz        j                  d      z  }|t        t        |dz  dz               z  }|dz  }|dz  }|S )ab  Graphics Control Extension. A sort of header at the start of
        each image. Specifies duration and transparancy.

        Dispose
        -------
          * 0 - No disposal specified.
          * 1 - Do not dispose. The graphic is to be left in place.
          * 2 - Restore to background color. The area used by the graphic
            must be restored to the background color.
          * 3 - Restore to previous. The decoder is required to restore the
            area overwritten by the graphic with what was there prior to
            rendering the graphic.
          * 4-7 -To be defined.
        s   !rQ   r!   r   d   g      ?r   )chrr   rc   r   )r=   r?   r[   r   s       r   r   zGifWriter.getGraphicsControlExt   sb      
c7Q;1$%,,W55 	hs8c>C/011
g
g	r   c                     | j                   dk(  r|dfS | j                  }t        j                  ||z
        }|j                  dk(  r|j                  d      }t        j                  |j                  d            }t        j                  |j                  d            }|j                  rK|j                  r?t        |d         t        |d   dz         }}t        |d         t        |d   dz         }	}n
d\  }}d\  }}	|||	||f   ||ffS )ar  Calculate the minimal rectangle that need updating. Returns
        a two-element tuple containing the cropped image and an x-y tuple.

        Calculating the subrectangles takes extra time, obviously. However,
        if the image sizes were reduced, the actual writing of the GIF
        goes faster. In some cases applying this method produces a GIF faster.
        r   rq   rQ   r!   r   rR   )r   r!   )	rW   rk   r/   absrS   sumargwherer   r   )
r=   rY   prevdiffXYx0x1y0y1s
             r   rs   zGifWriter.getSubRectangle  s     ;;!v:## vvb4i 99>88A;DKK$KK$66aff1YAbEAIB1YAbEAIBFBFB"R%B,"b))r   c                 (   t        |d      }|dv r4d}|j                  d      }t        ||      }|j                  ||      }|S |dv r=|dk(  r|j                  d      }n|j                  d      }|j                  ||	      }|S t	        d
|z        )zConvert image to Paletted PIL image.

        PIL used to not do a very good job at quantization, but I guess
        this has improved a lot (at least in Pillow). I don't think we need
        neuqant (and we can add it later if we really want).
        gif)nqneuquantr    RGBA)colors)r   r   r!   r!   RGB)r   methodzInvalid value for quantizer: %r)r   convertNeuQuantquantizer.   )r=   rY   rB   palette_sizery   nq_samplefac
nqInstances          r   rt   zGifWriter.converToPIL9  s      E***L^^F+F!&,7J(((EF  )#A~/.__L_KF  >JKKr   )Tr   r   r"   r   )g?r!   )r"   )r
   r   r   r^   ro   rX   ru   rL   rv   r|   r   r}   r   rs   rt   r   r   r   r;   r;   e   sN     .2+B<"4*@r   r;   )r^   loggingnumpyr/   pillow_legacyr   r   r   	getLoggerr
   r1   r   r   r   rc   r;   r   r   r   <module>r      s[      F F			8	$* *
I IX-m mr   