a
    Adu                     @   sr   d dl Z d dlmZ d dlZd dlmZ d dlmZm	Z	m
Z
mZ d dlmZ d dlmZmZ G dd dejZdS )    N)Image)
processingsharedimagesdevices)	Processed)optsstatec                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Scriptc                 C   s   dS )Nz
SD upscale )selfr   r   :/var/www/html/stable-diffusion-webui/scripts/sd_upscale.pytitle   s    zScript.titlec                 C   s   |S )Nr   )r   
is_img2imgr   r   r   show   s    zScript.showc              	   C   s|   t d}t jddddd| dd}t jd	d
ddd| dd}t jddd tjD tjd jd| dd}||||gS )Nz<p style="margin-bottom:0.75em">Will upscale the image by the selected scale factor; use width and height sliders to set tile size</p>r         zTile overlap@   overlap)minimummaximumsteplabelvalueelem_idg      ?g      @g?zScale Factorg       @scale_factorUpscalerc                 S   s   g | ]
}|j qS r   )name.0xr   r   r   
<listcomp>       zScript.ui.<locals>.<listcomp>indexupscaler_index)r   choicesr   typer   )grHTMLSliderr   Radior   sd_upscalersr   )r   r   infor   r   r$   r   r   r   ui   s
    
.z	Script.uic                 C   s  t |tr$dd tjD | }t| tj| }||jd< |j	|jd< d }|j
}|jd }	t|	tj}	|j	dkr|j|	||j}
n|	}
t  tj|
|j|j|d}|j}|j}d|_d	|_d	|_g }|jD ]"\}}}|D ]}||d
  qqtt || }|| t!_"t#dt | dt |jd d
  dt |j dt!j" d	 g }t$|D ].}|| }||_
g }t$|D ]v}||_||| |d |  |_d|d ||   dt!j" t!_%t&|}|d u r|j'}|j
d |_
||j7 }qd}|jD ]N\}}}|D ]<}|t |k r*|| nt()d|j|jf|d
< |d7 }qqt*|}|| tj+r`tj,||j-d||j.tj/||d q`t0||||}|S )Nc                 S   s   g | ]}|j  qS r   )r   lowerr   r   r   r   r!      r"   zScript.run.<locals>.<listcomp>zSD upscale overlapzSD upscale upscalerr   None)tile_wtile_hr      T   z%SD upscaling will process a total of z images tiled as r    z per upscale in a total of z	 batches.zBatch z out of RGB )r,   p)1
isinstancestrr   r+   r#   r.   r   fix_seedextra_generation_paramsr   seedinit_imagesr   flattenr   img2img_background_colorscalerupscale	data_pathr   torch_gc
split_gridwidthheight
batch_sizen_iterdo_not_save_griddo_not_save_samplestilesappendmathceillenr	   	job_countprintrangejobprocess_imagesr,   r   newcombine_gridsamples_save
save_imageoutpath_samplespromptsamples_formatr   )r   r6   _r   r$   r   upscalerinitial_infor;   init_imgimggridrF   Zupscale_countwork_y_hrowtiledatabatch_countresult_imagesnZ
start_seedwork_resultsi	processedimage_indexcombined_imager   r   r   run   sh    






< 

.

$z
Script.runN)__name__
__module____qualname__r   r   r-   rn   r   r   r   r   r
      s   r
   )rL   modules.scriptsscriptsgradior'   PILr   modulesr   r   r   r   Zmodules.processingr   Zmodules.sharedr   r	   r
   r   r   r   r   <module>   s   