a
    Ad{                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlmZ d dl	Z	d dl
Zd dlZd dl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Zd dlmZmZmZmZ d dlmZ d dlmZ e edrej!j"nej"Z"d	d
ddZ#dAddZ$edg dZ%dBddZ&dd Z'G dd dZ(dCddZ)dDddZ*dEddZ+d Z,d!Z-d"Z.e	/d#ej0 d$ Z1e	/d%Z2e	/d&Z3d'Z4e5 Z6dFd)d*Z7G d+d, d,Z8d-d. Z9dGd0d1Z:dHd5d6Z;h d7Z<d8d9d:d;d<Z=d=d> Z>d?d@ Z?dS )I    )annotationsN)
namedtuple)Image	ImageFont	ImageDraw
ImageColorPngImagePlugin)sd_samplerssharedscript_callbackserrors)roboto_ttf_file)opts
Resamplingintfontsizec                 C  s8   zt tjpt| W S  ty2   t t|  Y S 0 d S N)r   truetyper   fontr   	Exceptionr    r   6/var/www/html/stable-diffusion-webui/modules/images.pyget_font   s    r      c           
      C  s   |d u rvt jdkrt j}n\t jdkr*|}nLt jr`ttt| }t| | dkrv|d8 }qDntt| }t|}|t| krt| }tt| | }t	
| ||}t	| | d j\}}tjd|j| |j| fdd}t|jD ].\}}	|j|	||j | ||j | fd q|S )Nr   r   RGBblack)sizecolorbox)r   n_rowsgrid_prevent_empty_spotsmathfloorsqrtlenroundceilr   ImageGridLoopParamsimage_grid_callbackr   r   newcolsrows	enumerateimgspaste)
r/   
batch_sizer-   r,   paramswhgridiimgr   r   r   
image_grid"   s*    


 (r8   Grid)tilestile_wtile_himage_wimage_hoverlap   @   c              	   C  s*  | j }| j}|| }|| }t|| | }t|| | }	|dkrX|| |d  nd}
|	dkrt|| |	d  nd}tg |||||}t|	D ]}g }t|| }|| |kr|| }t|D ]N}t||
 }|| |kr|| }| |||| || f}||||g q|j	|||g q|S Nr   r   )
widthheightr#   r(   r9   ranger   cropappendr:   )imager;   r<   r?   r3   r4   Znon_overlap_widthZnon_overlap_heightr,   r-   dxdyr5   rowZ
row_imagesycolxtiler   r   r   
split_gridC   s,    rP   c              
     sz   fdd}|t j jt jdd jfj jdd}|t j jt jd jdfj jdd}t	d j j
f} jD ]\}}}t	d j|f}|D ]j\}	}
}|	dkr||d q|j|dd j|f|	df|d	 || jd|
|f|	 j df q|dkr&||d q|j|dd|j jfd|f|d	 ||d j|j|fd| j f q|S )
Nc                   s&   | d  j  } | tj} t| dS )N   L)r?   astypenpuint8r   	fromarray)rr5   r   r   make_mask_imagei   s    z%combine_grid.<locals>.make_mask_image)dtyper   r   )axisr   r   r   mask)rT   aranger?   float32reshaperepeatr<   r=   r   r+   r>   r:   r0   rF   rC   )r5   rY   Zmask_wZmask_hZcombined_imagerL   r4   rK   Zcombined_rowrN   r3   rO   r   rX   r   combine_gridh   s$    ..$(
&*rc   c                   @  s   e Zd ZdddZdS )GridAnnotation Tc                 C  s   || _ || _d | _d S r   )text	is_activer   )selfrf   rg   r   r   r   __init__   s    zGridAnnotation.__init__N)re   T)__name__
__module____qualname__ri   r   r   r   r   rd      s   rd   c              
     sR  t tjd t tjdt tjd}dd } fdd}|| d }	|	d t|	}
tdd	 |D d
krxd
n
|d d }| j| }| j	| }|t
|ksJ dt
| d| |t
|ksJ dt
| d| tdd|}t|}t|| |gt
| |gt
|  D ]\}}g | }|  |D ],||j|
|}|fdd	|D 7 }q:|D ]@|jdj|
d}|d |d
  |d |d  f_|_qlqfdd	|D }fdd	|D }t|d
krd
nt|d  }td| j| ||d   | j	| ||d   f|}t|D ]h}t|D ]X}| || || ||d  ||d  f}||||| |  ||| |  f qBq6t|}t|D ]H}||| |  |d  }|d || d  }|||||| |
|	 qt|D ]H}|d }||| |  |d  || d  }|||||| |
|	 q|S )Nr   c                 S  sT   dg}|  D ]@}|d  d|  }| j||d|krD||d< q|| q|S )Nre    r   )splitstrip
textlengthrG   )drawingrf   r   line_lengthlineswordliner   r   r   wrap   s    
z#draw_grid_annotations.<locals>.wrapc           	   	     s   |D ]}|}|}| j |j|dd |jkrD|dkrD|d8 }t|}q| j|||jd d  f|j||jrl nddd |js| j||jd d  ||jd d  ||jd d  ||jd d  fdd	 ||jd  7 }qd S )
Nro   r   r      mmcenter)r   fillanchoralign   )r|   rC   )multiline_textsizerf   allowed_widthr   multiline_textr   rg   rw   )	rs   Zdraw_xZdraw_yru   Zinitial_fntZinitial_fontsizerw   fntr   )color_activecolor_inactiveline_spacingr   r   
draw_texts   s    "
4Pz)draw_grid_annotations.<locals>.draw_texts   ry   c                 S  s   g | ]}t d d |D qS )c                 S  s   g | ]}t |jqS r   )r&   rf   .0rw   r   r   r   
<listcomp>       4draw_grid_annotations.<locals>.<listcomp>.<listcomp>sumr   ru   r   r   r   r      r   z)draw_grid_annotations.<locals>.<listcomp>r      r   z bad number of horizontal texts: z
; must be zbad number of vertical texts: )r   r   c                   s   g | ]}t | jqS r   )rd   rg   r   rN   )rw   r   r   r      r   r\   ro   r   c                   s&   g | ]}t  fd d|D   qS )c                   s   g | ]}|j d    qS r   r   r   r   r   r   r      r   r   r   r   r   r   r   r      r   c                   s.   g | ]&}t  fd d|D  t|  qS )c                   s   g | ]}|j d    qS r   r   r   r   r   r   r      r   r   )r   r&   r   r   r   r   r      r   )r   getcolorr   grid_text_active_colorgrid_text_inactive_colorgrid_background_colorr   r   rC   rD   r&   r   r+   r   Drawzipclearrf   multiline_textbboxr   r   maxrE   rF   r0   )imrC   rD   	hor_texts	ver_textsmarginZcolor_backgroundrx   r   r   r   pad_leftr,   r-   Zcalc_imgZcalc_dtextsr   itemswrappedbboxZhor_text_heightsZver_text_heightspad_topresultrK   rM   celldrN   rL   r   )r   r   rw   r   r   draw_grid_annotations   sV    
&

$$
.""6*0
$r   c           	        s   |dd  }t t|d }|d |  ||d   fddtdt > D }fddtdt> D }t| |||||S )Nr   ry   c                   s"   g | ]  fd dt D qS )c                   s(   g | ] \}}t | d |> @ dkdqS r   r   )rg   rd   r   r6   rN   posr   r   r      r   1draw_prompt_matrix.<locals>.<listcomp>.<listcomp>r.   r   )prompts_horizr   r   r      r   z&draw_prompt_matrix.<locals>.<listcomp>c                   s"   g | ]  fd dt D qS )c                   s(   g | ] \}}t | d |> @ dkdqS r   r   r   r   r   r   r      r   r   r   r   )prompts_vertr   r   r      r   )r#   r(   r&   rE   r   )	r   rC   rD   all_promptsr   promptsboundaryr   r   r   )r   r   r   draw_prompt_matrix   s    r   c                   sV   pt j  fdd}| dkr.||||}n$| dkr|| }|j|j }||krV|n|j| |j }	||krr|n|j| |j }
|||	|
}td||f}|j||d |	d  |d |
d  fd n|| }|j|j }||k r|n|j| |j }	||kr
|n|j| |j }
|||	|
}td||f}|j||d |	d  |d |
d  fd ||k r|d |
d  }|dkrR|j|j||fdd|dfddd |j|j||fd|j||jfdd||
 fd nx||krR|d |	d  }|dkrR|j|j||fddd|fddd |j|j||f|jd|j|fd||	 dfd |S )	a;  
    Resizes an image with the specified resize_mode, width, and height.

    Args:
        resize_mode: The mode to use when resizing the image.
            0: Resize the image to the specified width and height.
            1: Resize the image to fill the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess.
            2: Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image.
        im: The image to resize.
        width: The width to resize the image to.
        height: The height to resize the image to.
        upscaler_name: The name of the upscaler to use. If not provided, defaults to opts.upscaler_for_img2img.
    c                   s    d u s dks| j dkr,| j||ftdS t|| j || j }|dkr fddtjD }t|dkrtjd }t	d p~d	 d
|j
 d n|d }|j| ||j} | j|ks| j|kr| j||ftd} | S )NNonerR   )resampleg      ?c                   s   g | ]}|j  kr|qS r   )namer   upscaler_namer   r   r   
  r   z0resize_image.<locals>.resize.<locals>.<listcomp>r   zcould not find upscaler named z<empty string>z, using z as a fallback)moderesizeLANCZOSr   rC   rD   r
   sd_upscalersr&   printr   scalerupscale	data_path)r   r3   r4   scaleZ	upscalersupscalerr   r   r   r     s    
zresize_image.<locals>.resizer   r   r   ry   r   r\   )r   upscaler_for_img2imgrC   rD   r   r+   r0   r   )resize_moder   rC   rD   r   r   resratio	src_ratiosrc_wsrc_hresizedZfill_heightZ
fill_widthr   r   r   resize_image   s<    
.*

$2

$0r   z<>:"/\|?*
	rn   z .z[\sz]+z(.*?)(?:\[([^\[\]]+)\]|$)z(.*)<([^>]*)>$   Tc                 C  sP   | d u rd S |r|  dd} | dd tD } | td t } | t} | S )Nrn   _c                 S  s   i | ]}t |d qS )r   )ordr   r   r   r   
<dictcomp>P  r   z*sanitize_filename_part.<locals>.<dictcomp>)replace	translateinvalid_filename_charslstripinvalid_filename_prefixmax_filename_part_lengthrstripinvalid_filename_postfix)rf   replace_spacesr   r   r   sanitize_filename_partI  s    
r   c                    @  s  e Zd Zdd dd dd dd dd dd dd d	d d
d dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd dd d d d!Zd"Zd7d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Z	d.d/ Z
d0d1 Zd2d3 Zd4d5 Zd6S )8FilenameGeneratorc                 C  s   | j d ur| j S dS Nre   )seedrh   r   r   r   <lambda>X  r   zFilenameGenerator.<lambda>c                 C  s   | j jdkr| jS | j jd S rB   )pr1   r   	all_seedsr   r   r   r   r   Y  r   c                 C  s   | j jdkrtS | j jd S )Nr   rm   )r   r1   NOTHING_AND_SKIP_PREVIOUS_TEXTr   r   r   r   r   r   Z  r   c                 C  s   | j o| j jS r   )r   stepsr   r   r   r   r   [  r   c                 C  s   | j o| j jS r   )r   	cfg_scaler   r   r   r   r   \  r   c                 C  s   | j jS r   )rH   rC   r   r   r   r   r   ]  r   c                 C  s   | j jS r   )rH   rD   r   r   r   r   r   ^  r   c                 C  s*   | j o(tddd | j jD p"dddS )Nz, c                 S  s   g | ]}|d ks|qS )r   r   )r   styler   r   r   r   _  r   z.FilenameGenerator.<lambda>.<locals>.<listcomp>r   Fr   )r   r   joinstylesr   r   r   r   r   _  r   c                 C  s   | j ot| j jddS NFr   )r   r   sampler_namer   r   r   r   r   `  r   c                 C  s   t | jdtjjS )Nsd_model_hash)getattrr   r
   sd_modelr   r   r   r   r   r   a  r   c                 C  s   t tjjjddS r   )r   r
   r   sd_checkpoint_infoname_for_extrar   r   r   r   r   b  r   c                 C  s   t j  dS )Nz%Y-%m-%d)datetimenowstrftimer   r   r   r   r   c  r   c                 G  s
   | j | S r   )r   rh   argsr   r   r   r   d  r   c                 C  s   t | jdtjjS )Njob_timestamp)r   r   r
   stater   r   r   r   r   r   e  r   c                 G  s   | j | jg|R  S r   )string_hashpromptr   r   r   r   r   f  r   c                 G  s   | j | jjg|R  S r   )r   r   negative_promptr   r   r   r   r   g  r   c                 G  s$   | j | jj d| jj g|R  S )Nrn   )r   r   r   r   r   r   r   r   r   h  r   c                 C  s
   t | jS r   r   r   r   r   r   r   r   i  r   c                 C  s   |   S r   )prompt_no_styler   r   r   r   r   j  r   c                 C  s   t | jddS r   r   r   r   r   r   r   k  r   c                 C  s   |   S r   )prompt_wordsr   r   r   r   r   l  r   c                 C  s"   | j jdks| jrtS | j jd S Nr   )r   r1   r   r   batch_indexr   r   r   r   r   m  r   c                 C  s   | j jS r   )r   r1   r   r   r   r   r   n  r   c                 C  s>   | j jdkr| j jdks| jr"tS | j j| j j | j j d S r  )r   n_iterr1   r   r   	iterationr  r   r   r   r   r   o  r   c                 G  s
   | j | S r   )	haspromptr   r   r   r   r   p  r   c                 C  s
   t jd S )NCLIP_stop_at_last_layers)r   datar   r   r   r   r   q  r   c                 C  s   | j r| j jr| j jS tS r   )r   denoising_strengthr   r   r   r   r   r   r  r   c                 C  s   | j jS r   )r   userr   r   r   r   r   s  r   c                 C  s   |   S r   )get_vae_filenamer   r   r   r   r   t  r   c                 C  s   dS r   r   r   r   r   r   r   u  r   c                 G  s
   | j | S r   )
image_hashr   r   r   r   r   v  r   )r   Z
seed_firstZ	seed_lastr   cfgrC   rD   r   sampler
model_hash
model_namedater   r   Zprompt_hashZnegative_prompt_hashZfull_prompt_hashr   Zprompt_no_stylesZprompt_spacesr  batch_numberr1   Zgeneration_numberr  	clip_skip	denoisingr  Zvae_filenamenoner  z%Y%m%d%H%M%SFc                 C  s"   || _ || _|| _|| _|| _d S r   )r   r   r   rH   r   )rh   r   r   r   rH   r   r   r   r   ri   z  s
    zFilenameGenerator.__init__c                 C  s^   ddl m} |jdu rdS tj|j}|d}t|dkrR|d dkrR|d S |d S dS )zGet the name of the VAE file.r   NNoneType.r   re   )modules.sd_vaesd_vaeloaded_vae_fileospathbasenamerp   r&   )rh   r  	file_namesplit_file_namer   r   r   r    s    

z"FilenameGenerator.get_vae_filenamec                 G  s   | j  }| jd u s| j d u r"d S d}|D ]n}|dkr*|d}|d  }t|dkr`|d nd}||dkr| | }q*|dkr|n
| | }q*t|S )Nre   |r   r   )r   lowerr   rp   r&   findr   )rh   r   r"  Zoutresargdivisionexpecteddefaultr   r   r   r    s    

zFilenameGenerator.haspromptc                 C  s   | j d u s| jd u rd S | j}tj| j jD ]P}|r.|dD ]"}||ddd d}q@||d d }q.t	|ddS )Nz{prompt}re   z, ,,Fr   )
r   r   r
   prompt_stylesget_style_promptsr   rp   r   rq   r   )rh   r  r   partr   r   r   r    s     z!FilenameGenerator.prompt_no_stylec                 C  sH   dd t | jpdD }t|dkr,dg}td|dtj ddS )	Nc                 S  s   g | ]}|r|qS r   r   r   r   r   r   r     r   z2FilenameGenerator.prompt_words.<locals>.<listcomp>re   r   emptyrn   Fr   )re_nonlettersrp   r   r&   r   r   r   directories_max_prompt_words)rh   wordsr   r   r   r    s    zFilenameGenerator.prompt_wordsc              	   G  s   t j  }|r"|d dkr"|d n| j}z"t|dkrDt|d nd }W n tjjyd   d }Y n0 ||}z|	|}W n" t
tfy   |	| j}Y n0 t|ddS )Nr   re   r   Fr   )r   r   default_time_formatr&   pytztimezone
exceptionsUnknownTimeZoneError
astimezoner   
ValueError	TypeErrorr   )rh   r   Ztime_datetimetime_formatZ	time_zoneZtime_zone_timeZformatted_timer   r   r   r     s    
"

zFilenameGenerator.datetimec                 G  s<   |r|d dkrt |d nd }t| j  d| S )Nr   re   )r   hashlibsha256rH   tobytes	hexdigest)rh   r   lengthr   r   r   r    s     zFilenameGenerator.image_hashc                 G  s:   |r|d dkrt |d nd}t|  d| S )Nr   re      )r   r9  r:  encoder<  )rh   rf   r   r=  r   r   r   r     s     zFilenameGenerator.string_hashc           
   	   C  s   d}t |D ]}| \}}|d u r0||7 }qg }t|}|d u rHqb| \}}|d| q4| j| }|d urz|| g|R  }	W n, t	y   d }	t
jd| ddd Y n0 |	tkrqn|	d ur||t|	 7 }q|| d| d7 }q|S )	Nre   r   zError adding [z] to filenameTexc_info[])
re_patternfinditergroupsre_pattern_argmatchinsertreplacementsgetr"  r   r   reportr   str)
rh   rN   r   mrf   patternZpattern_argsr$  funreplacementr   r   r   apply  s4    
zFilenameGenerator.applyN)F)rj   rk   rl   rJ  r0  ri   r  r  r  r  r   r  r   rR  r   r   r   r   r   V  sT   !
r   c              	   C  s   d}|dkr| d}t |}t| D ]V}||r(tj||d d d}ztt|d |}W q( t	y|   Y q(0 q(|d S )z
    Determines and returns the next sequence number to use when saving an image in the specified directory.

    The sequence starts at 0.
    rm   re   -Nr   r   )
r&   r  listdir
startswithr  splitextrp   r   r   r6  )r  r  r   Zprefix_lengthr   partsr   r   r   get_next_sequence_number  s    

rX  
parametersc                 C  s^  |du rt j|d }t | }| dkr|p6i }tjrF|||< tjr|t	 }|pZi 
 D ]\}}	||t|	 q`nd}| j||tj|d n| dv rH| jdkr| d} n.| jdkr| d	d
 | dkrdnd} | j||tjtjd tjrZ|durZtdtjjtjjj|p,dddii}
t|
| n| j||tjd dS )a+  
    Saves image to filename, including geninfo as text information for generation info.
    For PNG images, geninfo is added to existing pnginfo dictionary using the pnginfo_section_name argument as key.
    For JPG images, there's no dictionary and geninfo just replaces the EXIF description.
    Nr   .png)formatqualitypnginfo).jpgz.jpeg.webpRGBAr   zI;16c                 S  s   | d S )Ng=o?r   )r   r   r   r   r   (  r   z)save_image_with_geninfo.<locals>.<lambda>r_  rR   )r[  r\  losslessExifre   unicodeencoding)r[  r\  )r  r  rV  r   registered_extensionsr"  r   enable_pnginfor   PngInfor   add_textrM  savejpeg_qualityr   convertpointwebp_losslesspiexifdumpExifIFDUserCommenthelperrI  )rH   geninfofilename	extensionexisting_pnginfopnginfo_section_nameZimage_formatZpnginfo_datakvZ
exif_bytesr   r   r   save_image_with_geninfo
  s6    

$r{  pngFre   c           "   
     s@  t |||| }| jdks"| jdkr.| dv sN| jdksB| jdkrZ| dkrZtd d}|du r~|	rltjp||	 o|tjo|| }|r|tj	pd
d	d
}tj||}tj|dd |du r|s|du rd}ntjrtjpd}n
tjpd}||| }tjp|dk}|dkr*|r*d| }|rt||}d}tdD ]`}|dkr`|| dn| d|| d}tj|| | d| }tj|sF qqFntj|| d| }ntj|| d| }|pi } dur |< t| |||t j} j}jd  fdd}tjj\}}ttdrt|j}|d|t dt!|  }|| _j}|| || || _"| jtj#kp| jtj#k}tj$r|st%|j&tj'd d kr| j| j }d}|r(|dkr(t(tj#t(| jtj# | j f}n&|rNt(| jtj# | j t(tj#f}|durz| )|t*} W n t+y   | )|} Y n0 z|| |d W n2 t+y } zt,-|d W Y d}~n
d}~0 0 tj.r* dur*| d} t/| ddd  }!|!0  d! W d   n1 s0    Y  nd} t1 || fS )"a!  Save an image.

    Args:
        image (`PIL.Image`):
            The image to be saved.
        path (`str`):
            The directory to save the image. Note, the option `save_to_dirs` will make the image to be saved into a sub directory.
        basename (`str`):
            The base filename which will be applied to `filename pattern`.
        seed, prompt, short_filename,
        extension (`str`):
            Image file extension, default is `png`.
        pngsectionname (`str`):
            Specify the name of the section which `info` will be saved in.
        info (`str` or `PngImagePlugin.iTXt`):
            PNG info chunks.
        existing_info (`dict`):
            Additional PNG info. `existing_info == {pngsectionname: info, ...}`
        no_prompt:
            TODO I don't know its meaning.
        p (`StableDiffusionProcessing`)
        forced_filename (`str`):
            If specified, `basename` and filename pattern will be ignored.
        save_to_dirs (bool):
            If true, the image will be saved into a subdirectory of `path`.

    Returns: (fullfn, txt_fullfn)
        fullfn (`str`):
            The full path of the saved imaged.
        txt_fullfn (`str` or None):
            If a text file is saved for this image, this will be its full path. Otherwise None.
    i  )jpgjpegi?  webpz)Image dimensions too large; saving as PNGrZ  Nz[prompt_words]rn   z\ /T)exist_okre   z[seed]z[seed]-[prompt_spaces]rS  i  0504r  c                   s4   | d}t |  ||jd t|||  dS )z
        save image with .tmp extension to avoid race condition when another process detects new image in the directory
        z.tmp)rw  rx  N)r{  r]  r  r   )Zimage_to_saveZfilename_without_extensionrv  temp_file_pathinfor2   rx  r   r   _atomically_save_image  s    
z*save_image.<locals>._atomically_save_imagestatvfsr   i   r   r^  zsaving image as downscaled JPGz.txtr3   utf8rd  
)2r   rD   rC   r"  r   r   grid_save_to_dirssave_to_dirsrR  directories_filename_patternr   r   r  r  r   makedirssamples_filename_patternsave_images_add_numberrX  rE   existsr   ImageSaveParamsbefore_image_saved_callbackrH   ru  r]  rK  rV  hasattrr  	f_namemaxr   r&   Zalready_saved_astarget_side_lengthexport_for_4chanstatst_sizeimg_downscale_thresholdr'   r   r   r   r   displaysave_txtopenwriteimage_saved_callback)"rH   r  r  r   r   rv  r  short_filenameZ	no_promptr5   rx  r   Zexisting_infoforced_filenamesuffixr  ZnamegendirnameZfile_decorationZ
add_numberZ	basecountZfullfnr6   fnr]  r  Zfullfn_without_extensionmax_name_lenZoversizer   	resize_toeZ
txt_fullfnfiler   r  r   
save_image8  s    !@



*




*" 
"
2
r  >   loopexifjfif_versionjfif_density
backgroundprogressiveprogression	timestamp	jfif_uniticc_profiledpichromaticitydurationjfif	photoshopzImage.Imageztuple[str | None, dict])rH   returnc                 C  sT  | j pi  }|dd }d|v rt|d }|p6i di tjjd}ztjj|}W n  t	y~   |j
ddd}Y n0 |r||d< |}tD ]}||d  q|d	d d
krLzpt|d }tj|d d}|d  d|d  d|d  d| d|d  d|d  d| j d| j d}W n" tyJ   tjddd Y n0 ||fS )NrY  r  rb  r   r  ignore)r   zexif commentSoftwareZNovelAICommentr  zEuler aDescriptionz
Negative prompt: ucz
Steps: r   z, Sampler: z, CFG scale: r   z, Seed: r   z, Size: rN   z, Clip skip: 2, ENSD: 31337z1Error parsing NovelAI image generation parametersTr@  )r  copypopro  loadrK  rq  rr  rs  r6  decodeIGNORED_INFO_KEYSjsonloadsr	   samplers_maprC   rD   r   r   rL  )rH   r   rt  r  Zexif_commentfieldZ	json_infor  r   r   r   read_info_from_image  sH    
r  c                 C  s   dd l }z&tt| }t|\}}|d fW S  ty@   Y n0 z$| d}t|dk s^J |d fW S  tyx   Y n0 |	 d fS )Nr   r  i'  )
gradior   r  ioBytesIOr  r   r  r&   update)r	  grrH   textinfor   rf   r   r   r   
image_data  s    


r  c                 C  s6   | j dkr,td| j|}|j| | d |} | dS )zireplaces transparency with bgcolor (example: "#ffffff"), returning an RGB mode image with no transparencyr`  r]   r   )r   r   r+   r   r0   rl  )r7   bgcolorr  r   r   r   flatten  s
    
r  )r   N)r@   r@   rA   )r   )r   )N)T)NNrY  )NNr|  NFFFrY  NNNre   N)@
__future__r   r   r1  r  r#   r  collectionsr   renumpyrT   ro  Zpiexif.helperPILr   r   r   r   r   stringr  r9  modulesr	   r
   r   r   modules.paths_internalr   modules.sharedr   r  r   r   r   r8   r9   rP   rc   rd   r   r   r   r   r   r   compilepunctuationr-  rD  rG  r   objectr   r   r   rX  r{  r  r  r  r  r  r   r   r   r   <module>   s\   

%
X

M


 
.
 "