a
    Ad%                     @   s  d dl Z d dlmZ d dlm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mZ d dlmZmZ d dlmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZ d dl ZdddZ!e"e#e"e"e#e"e#e$e#e#e#e$e$e$e#e#e#e$e#e%e#e#e"e"e"e%e&e"ej'dddZ(dS )    N)closing)Path)ImageImageOpsImageFilterImageEnhanceUnidentifiedImageError)images)create_override_settings_dictparse_generation_parameters)	Processed StableDiffusionProcessingImg2Imgprocess_images)optsstate)plaintext_to_htmlF      ?c
           "         sr  |  }t|  ttj|dd}
d}|rVt|}t|}|rVtdt	| d tdt	|
 d| j
| j  d t	|
| j
 t_| j}| j}| j}| j}| j}| j}t|
D ]\}}|d	  d
t	|
 t_tjrdt_tjr qnzt|}W n: ty6 } z t| W Y d }~qW Y d }~n
d }~0 0 t|}|rht|j| | _t|j| | _|g| j | _ t!|}|rt	|d	kr|d }nLt!|}t|"|j# d}t	|dkrtd| d| d q|d }t|}|| _$|rzZ|}|	r*t%j&'|	t%j&(|}t|}t)*|\}}t+|}  fdd| , D } W n t-yp   i } Y n0 |d| v rd| d  nd | _|d| v rd| d  nd | _t| .d|| _t/| .d|| _| .d|| _t| .d|| _t0j1j2j3| g|R  }!|!d u r|rd|| _4d| j5d< | j
d	ksB| jd	krV|j# d| j5d< n|j# | j5d< t6|  qd S )N)z.pngz.jpgz.jpegz.webpz.tifz.tiff)allowed_extensionsFz
Inpaint batch is enabled. z masks found.zWill process z images, creating z new images for each.   z out of r   z.*zWarning: mask is not found for z in z. Skipping it.c                    s"   i | ]\}}| pi v r||qS  r   ).0kvpng_info_propsr   7/var/www/html/stable-diffusion-webui/modules/img2img.py
<dictcomp>`       z!process_batch.<locals>.<dictcomp>Prompt  zNegative promptSeedz	CFG scaleSamplerStepssave_to_dirsz-[generation_number]samples_filename_pattern)7strip
processingfix_seedlistshared
walk_files	listfilesboolprintlenn_iter
batch_sizer   	job_countpromptnegative_promptseed	cfg_scalesampler_namesteps	enumeratejobskippedinterruptedr   openr   r   exif_transposeintwidthheightinit_imagesr   globstem
image_maskospathjoinbasenameimgutilread_info_from_imager   items	Exceptiongetfloatmodulesscriptsscripts_img2imgrunoutpath_samplesoverride_settingsr   )"p	input_dir
output_dirZinpaint_mask_dirargsto_scalescale_byuse_png_infor   png_info_dirr	   Zis_inpaint_batchZinpaint_masksr3   r4   r5   r6   r7   r8   iimageimge
image_pathZmask_image_pathZmask_image_dirZmasks_foundZ
mask_imageZinfo_imgZinfo_img_pathgeninfo_Zparsed_parametersprocr   r   r   process_batch   s    

""




  
rf   )id_taskmoder3   r4   r8   r7   	mask_blur
mask_alphainpainting_fillr0   r1   r6   image_cfg_scaledenoising_strengthselected_scale_tabrA   r@   r[   resize_modeinpaint_full_resinpaint_full_res_paddinginpainting_mask_invertimg2img_batch_input_dirimg2img_batch_output_dirimg2img_batch_inpaint_mask_dirimg2img_batch_use_png_infoimg2img_batch_png_info_propsimg2img_batch_png_info_dirrequestc&           1      G   s  t |!}'|dk}(|dkr"|})d }*n|dkr4|})d }*n|dkrZ|d |d  })}*t|*}*n|dkr|})|	pl|}+tjt|)t|+kdd	},t|,tj	d
 d}*t
|*d|d  }*t|}-t|)|-|+|*|-})n|dkr|
})|}*nd })d }*|)d urt|)})|dkrJ|(sJ|)s.J dt|)j| }t|)j| }d|  krbdksln J dttjtjp~tjtjptj|||||||||||)g|*|||||||||'d}.tjj |._|&|._!|%j"|._#tj$j%rt&d| tj'd |*r||.j(d< t)|. |(rVtj$j*r$J dt+|.||| |&|dk||"|#|$d
 t,|.g |.j-d}/n(tjj j.|.g|&R  }/|/d u r~t/|.}/W d    n1 s0    Y  tj01  |/2 }0tj3rt&|0 tj4rg |/_5|/j5|0t6|/j7t6|/j8ddfS )N   r   r      r_   mask   )axis   Ld      z+Can't scale by because no image is selectedg        r   z)can only work with strength in [0.0, 1.0])sd_modelrT   outpath_gridsr3   r4   stylesr7   r1   r0   r8   r6   r@   rA   rB   r|   ri   rk   ro   rm   rl   rp   rq   rr   rU   z

img2img: )filez	Mask blurz:Launched with --hide-ui-dir-config, batch img2img disabled)rZ   r[   r\   r   r]   r    comments)	classname)9r
   r'   create_binary_masknpanyarrayr   	fromarrayastypeuint8r   
Brightnessenhancer   GaussianBlur	compositefilterr   r>   r?   r@   rA   r   r*   r   r   outdir_samplesoutdir_img2img_samplesoutdir_gridsoutdir_img2img_gridsrP   rQ   rR   script_argsusernameusercmd_optsenable_console_promptsr.   progress_print_outextra_generation_paramsr   hide_ui_dir_configrf   r   r5   rS   r   
total_tqdmclearjssamples_log_stdoutdo_not_show_imagesr	   r   infor   )1rg   rh   r3   r4   prompt_stylesinit_imgsketchinit_img_with_maskinpaint_color_sketchinpaint_color_sketch_originit_img_inpaintinit_mask_inpaintr8   r7   ri   rj   rk   r0   r1   r6   rl   rm   rn   rA   r@   r[   ro   rp   rq   rr   rs   rt   ru   Zoverride_settings_textsrv   rw   rx   ry   rY   rU   Zis_batchr_   r|   origpredblurrV   	processedZgeneration_info_jsr   r   r   img2imgw   s    


"



 
(
r   )Fr   FNN))rF   
contextlibr   pathlibr   numpyr   PILr   r   r   r   r   gradiogrrP   r	   rJ   Z'modules.generation_parameters_copypaster
   r   Zmodules.processingr   r   r   Zmodules.sharedr   r   r*   r'   
modules.uir   modules.scriptsrf   strr?   rO   r-   r)   Requestr   r   r   r   r   <module>   s   
d