- Notifications
You must be signed in to change notification settings - Fork29.5k
Developing custom scripts
AUTOMATIC1111 edited this pageSep 30, 2022 ·1 revision
The Script class definition can be found inmodules/scripts.py. To create your own custom script, create a python script that implements the class and drop it into thescripts folder, using the below example or other scripts already in the folder as a guide.
The Script class has four primary methods, described in further detail below with a simple example script that rotates and/or flips generated images.
importmodules.scriptsasscriptsimportgradioasgrimportosfrommodulesimportimagesfrommodules.processingimportprocess_images,Processedfrommodules.processingimportProcessedfrommodules.sharedimportopts,cmd_opts,stateclassScript(scripts.Script):# The title of the script. This is what will be displayed in the dropdown menu. deftitle(self): return"Flip/Rotate Output"# Determines when the script should be shown in the dropdown menu via the# returned value. As an example:# is_img2img is True if the current tab is img2img, and False if it is txt2img.# Thus, return is_img2img to only show the script on the img2img tab. defshow(self,is_img2img): returnis_img2img# How the script's is displayed in the UI. See https://gradio.app/docs/#components# for the different UI components you can use and how to create them.# Most UI components can return a value, such as a boolean for a checkbox.# The returned values are passed to the run method as parameters. defui(self,is_img2img): angle=gr.Slider(minimum=0.0,maximum=360.0,step=1,value=0, label="Angle") hflip=gr.Checkbox(False,label="Horizontal flip") vflip=gr.Checkbox(False,label="Vertical flip") overwrite=gr.Checkbox(False,label="Overwrite existing files") return [angle,hflip,vflip,overwrite]# This is where the additional processing is implemented. The parameters include# self, the model object "p" (a StableDiffusionProcessing class, see# processing.py), and the parameters returned by the ui method.# Custom functions can be defined here, and additional libraries can be imported# to be used in processing. The return value should be a Processed object, which is# what is returned by the process_images method. defrun(self,p,angle,hflip,vflip,overwrite): # function which takes an image from the Processed object,# and the angle and two booleans indicating horizontal and # vertical flips from the UI, then returns the # image rotated and flipped accordingly defrotate_and_flip(im,angle,hflip,vflip): fromPILimportImage raf=im ifangle!=0: raf=raf.rotate(angle,expand=True) ifhflip: raf=raf.transpose(Image.FLIP_LEFT_RIGHT) ifvflip: raf=raf.transpose(Image.FLIP_TOP_BOTTOM) returnraf # If overwrite is false, append the rotation information to the filename # using the "basename" parameter and save it in the same directory. # If overwrite is true, stop the model from saving its outputs and # save the rotated and flipped images instead. basename="" if(notoverwrite): ifangle!=0: basename+="rotated_"+str(angle) ifhflip: basename+="_hflip" ifvflip: basename+="_vflip" else: p.do_not_save_samples=True proc=process_images(p) # rotate and flip each image in the processed images# use the save_images method from images.py to save# them. foriinrange(len(proc.images)): proc.images[i]=rotate_and_flip(proc.images[i],angle,hflip,vflip) images.save_image(proc.images[i],p.outpath_samples,basename, proc.seed+i,proc.prompt,opts.samples_format,info=proc.info,p=p) returnproc
This is theStable Diffusion web UI wiki.Wiki Home
Setup
- Install and run on NVidia GPUs
- Install and run on AMD GPUs
- Install and run on Apple Silicon
- Install and run on Intel Silicon (external wiki page)
- Install and run via container (i.e. Docker)
- Run via online services
Reproducing images / troubleshooting
Usage
- Features
- Command Line Arguments and Settings
- Optimizations
- Custom Filename Name and Subdirectory
- Change model folder location e.g. external disk
- User Interface Customizations
- Guides and Tutorials
Developers