
    
3jy(                         S SK r S SKrS SKrS SKrS SKJr  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5      rS SKrS	 rS
\4S jrS r " S S5      r\\/rg)    N)Path)ImageImageOps)PngInfo   )logc                   \    \ rS rSrSr\S 5       rSrSrSr	Sr
SS	 jrS
 r\SS j5       rSrg)MTB_LoadImageSequence   a  Load an image sequence from a folder. The current frame is used to determine which image to load.

Usually used in conjunction with the `Primitive` node set to increment to load a sequence of images from a folder.
Use -1 to load all matching frames as a batch.

c                 2    SSS04SSSSS.4S	.S
SSS040S.$ )NSTRINGdefaultzvideos/####.pngINTr    r   minmax)pathcurrent_framerange )requiredoptional clss    B/home/wildlama/comfy/ComfyUI/custom_nodes/comfy_mtb/nodes/video.pyINPUT_TYPES!MTB_LoadImageSequence.INPUT_TYPES   sH     "I/@#AB !"W=" (YO4
 	
    mtb/IO
load_image)IMAGEMASKr   r   )imagemaskr   total_framesNc                    US:H  nSnU(       a\  U R                  X5      n[        S U 5       6 u  px[        R                  " USS9n	[        R                  " USS9n
[	        U5      nXSU4$ U(       a  [
        R                  " SU 35        [        U5      n[
        R                  " S[	        U5       S35        / n/ n[        S	 U 5       6 u  px[        R                  " USS9n	[        R                  " USS9n
[	        U5      nXSU4$ [
        R                  " S
U SU 35        [        X5      n[        R                  " U5      n[        U5      u  pXX%4$ )Nr      c              3   8   #    U  H  n[        U5      v   M     g 7fNimg_from_path.0frames     r   	<genexpr>3MTB_LoadImageSequence.load_image.<locals>.<genexpr><        I&e 4 4&   r   )dimzLoading all frames from Found z framesc              3   8   #    U  H  n[        U5      v   M     g 7fr,   r-   r/   s     r   r2   r3   J   r4   r5   zLoading image: , )get_frames_from_rangeziptorchcatlenr   debugresolve_all_framesresolve_pathfolder_pathsget_annotated_filepathr.   )selfr   r   r   load_allr(   framesimgsmasksout_imgout_maskresolved_path
image_pathr&   r'   s                  r   r#    MTB_LoadImageSequence.load_image6   sB    B&//<FI&IJKDii!,GyyA.Ht9Lr<88II078'-FIIs6{m734DEI&IJKDii!,GyyA.Ht9Lr<88		OD6M?;<$T9!88G
#J/]99r!   c                 t    [        [        UR                  S5      5      u  p4[	        U5      n[        U5      nUS:  d  XF:  a  [        SU SU 35      eSU;   a  [        R                  " U5      R                  SS	5      n[        R                  " U5      n/ n	U HZ  n
UR                  U
5      nU(       d  M  [        UR                  S
5      5      nX<s=::  a  U::  d  ME  O  MI  U	R                  U
5        M\     U	$ [        R                  " SU 35        XSUS
-    nU$ ! [         a    [        SU S35      ef = f)N-zInvalid range format: z!. Expected format is 'start-end'.r   zRange z+ is out of bounds. Total frames available: #\#(\d+)r*   zNWildcard pattern or directory will use indexes instead of frame numbers for : )mapintsplit
ValueErrorr@   r>   reescapereplacecompilesearchgroupappendr   warning)rD   r   	range_strstartendrF   r(   frame_regexframe_number_regexmatching_framesr1   matchframe_numberselected_framess                 r   r:   +MTB_LoadImageSequence.get_frames_from_rangeX   sC   	S)//#"67JE $D)6{19+#N|n]  $;))D/11%BK!#K!8 O*11%85#&u{{1~#6L333'..u5   #"KK`ae`fg %S1W5OE  	(3TU 	s   !D D7c                    [        SU  SU 35        U(       d  US:X  a  [        U 5      nU Vs/ s H6  n[        R                  R	                  [
        R                  " U5      5      PM8     nn[        R                  " SR                  [        [        U5      5      R                  5       5      nUR                  5       $ [        X5      n[
        R                  " U5      n[        R                  R                  U5      (       ag  [        R                  " 5       n	[!        US5       n
U	R#                  U
R%                  5       5        S S S 5        U	R'                  5       R)                  5       $ gs  snf ! , (       d  f       N2= f)NzChecking if changed: r9   r   r   rbNONE)printr@   osr   getmtimerB   rC   hashlibsha256joinrS   strencode	hexdigestrA   existsopenupdatereaddigesthex)r   r   r   resolved_pathsp
timestampscombined_hashrK   rL   mfs              r   
IS_CHANGED MTB_LoadImageSequence.IS_CHANGED   s%   %dV2m_=>MR'/5N ('A   !D!DQ!GH'   $NNC,-446M !**,,$T9!88G
77>>*%% Aj$'1" (88:>>## ('s   =E+$ E00
E>r   )Nr   r   )r   r   r   )__name__
__module____qualname____firstlineno____doc__classmethodr   CATEGORYFUNCTIONRETURN_TYPESRETURN_NAMESr#   r:   staticmethodr   __static_attributes__r   r!   r   r
   r
      sU     
 
 HHLL :D%N  r!   r
   c                 V   [         R                  " U 5      n[        R                  " U5      nUR	                  S5      n[
        R                  " U5      R                  [
        R                  5      S-  n[        R                  " U5      S   nSUR                  5       ;   ab  [
        R                  " UR                  S5      5      R                  [
        R                  5      S-  nS[        R                  " U5      -
  nUU4$ [        R                  " S[        R                  SS9nUU4$ )	NRGB     o@r,   Ag      ?)@   r   cpu)dtypedevice)r   rv   r   exif_transposeconvertnparrayastypefloat32r<   
from_numpygetbands
getchannelzeros)r   imgr&   r'   s       r   r.   r.      s    
**T
C

!
!#
&CKKEHHUO""2::.6EU#E*E
cllnxxs+,33BJJ?%GU%%d++ 	  {{85==G r!   r   c                 X   / nSU ;  a  [        U 5      nUR                  5       (       aI  UR                  5        H4  nUR                  S;   d  M  UR	                  UR                  5       5        M6     O(SU ;   a  [        R                  " U 5      nO[        S5      eUR                  5         U$ U n[        R                  R                  U5      u  pV[        R                  " SU 35        UR                  S5      n[        R                   " SSU5      n[        R                  " SU 35        [        R                  " [        R                  R#                  XX5      5      n	[        R                  " S[%        U	5       S	35        [        R&                  " U5      R)                  S
S5      n
[        R*                  " U
5      nU	 HX  nUR-                  U5      nU(       d  M  UR/                  S5      n[        R                  " SU 35        UR	                  U5        MZ     UR                  5         U$ )NrP   )z.jpg.png*z9The path doesn't contain a # or a * or is not a directoryzResolving all frames in #+zFound pattern: r7   z matching filesrQ   rR   r*   zFound frame number: )r   is_diriterdirsuffixr]   as_posixglobrV   sortrm   r   rU   r   r?   countrW   subrq   r>   rX   rY   rZ   r[   r\   )r   rF   pthr   patternfolder_pathfile_pattern
hash_countframe_patternmatching_filesrb   rc   filere   rf   s                  r   r@   r@      s   F
$4j::<<[[]88//MM!**,/ # D[YYt_FK  	G "g 6KII(67##C(JFF5#|4MII/0YYrww||KGHNIIs>*+?;<))L)11%BKK0"))$/5 ;;q>LII,\N;<MM$  KKMMr!   c                     U R                  S5      n[        U5      R                  U5      n[        R                  " SX05      $ )NrP   r   )r   rr   zfillrW   r   )r   r1   hashespadded_numbers       r   rA   rA      s4    ZZ_FJ$$V,M66$,,r!   c                   P    \ rS rSrSrS r\S 5       rSrSr	Sr
Sr    SS
 jrSrg	)MTB_SaveImageSequence   zSave an image sequence to a folder. The current frame is used to determine which image to save.

This is merely a wrapper around the `save_images` function with formatting for the output folder and filename.
c                 F    [         R                  " 5       U l        SU l        g )Noutput)rB   get_output_directory
output_dirtype)rD   s    r   __init__MTB_SaveImageSequence.__init__   s    &;;=	r!   c                 .    SSSS04SSSSS.4S	.S
SS.S.$ )N)r$   r   r   Sequencer   r   r   r   )imagesfilename_prefixr   PROMPTEXTRA_PNGINFO)promptextra_pnginfo)r   hiddenr   r   s    r   r   !MTB_SaveImageSequence.INPUT_TYPES   s?     %$,y*.E#F !!G<" "*OL

 
	
r!   r   save_imagesTr"   Nc                    [        U5      S:  a  [        S5      e[        U R                  5      U-  nUR	                  SSS9  Xb SUS S3-  nUS   R                  5       R                  5       n[        R                  " [        R                  " US	-  SS
5      R                  [        R                  5      5      n	[        5       n
Ub&  U
R                  S[        R                   " U5      5        Ub1  U H+  nU
R                  U[        R                   " X[   5      5        M-     U	R#                  XzSS9  SSUR$                  UR$                  U R&                  S./00$ )Nr*   z!Can only save one image at a timeT)parentsexist_ok_05r   r   r      r      )pnginfocompress_leveluir   )filename	subfolderr   )r>   rV   r   r   mkdirr   numpyr   	fromarrayr   clipr   uint8r   add_textjsondumpssavenamer   )rD   r   r   r   r   r   rK   resolved_imgoutput_imager   metadataxs               r   r   !MTB_SaveImageSequence.save_images  sI   : v;?@AAT__-?D48 /qr0B$GG 	 ay}},,.ooGGL5(!S188B
 9h

6(:;$"!!!TZZ0@%AB # 	B$0$5$5%2%7%7 $		

 
	
r!   )r   r   )r   r   NN)r   r   r   r   r   r   r   r   r   r   OUTPUT_NODEr   r   r   r   r!   r   r   r      sI    
 
 
 LHKH
 #=
r!   r   )ro   r   rm   rW   pathlibr   rB   r   r   r<   PILr   r   PIL.PngImagePluginr   r   r
   r   r.   rr   r@   rA   r   	__nodes__r   r!   r   <module>r      sk      	 	      & C C^ ",S ,^-\
 \
@ 	r!   