code: def img2img(
      id_task: str,
      mode: int,
      prompt: str,
      negative_prompt: str,
      prompt_styles,
      init_img,
      sketch,
      init_img_with_mask,
      inpaint_color_sketch,
      inpaint_color_sketch_orig,
      init_img_inpaint,
      init_mask_inpaint,
      steps: int,
      sampler_name: str,
      mask_blur: int,
      mask_alpha: float,
      inpainting_fill: int,
      n_iter: int,
      batch_size: int,
      cfg_scale: float,
      image_cfg_scale: float,
      denoising_strength: float,
      selected_scale_tab: int,
      height: int,
      width: int,
      scale_by: float,
      resize_mode: int,
      inpaint_full_res: bool,
      inpaint_full_res_padding: int,
      inpainting_mask_invert: int,
      img2img_batch_input_dir: str,
      img2img_batch_output_dir: str,
      img2img_batch_inpaint_mask_dir: str,
      override_settings_texts,
      img2img_batch_use_png_info: bool,
      img2img_batch_png_info_props: list,
      img2img_batch_png_info_dir: str,
      request: gr.Request,
      *args,
  ):
      override_settings = create_override_settings_dict(
          override_settings_texts
      )
  
      is_batch = mode == 5
  
      if mode == 0:  # img2img
          image = init_img
          mask = None
      elif mode == 1:  # img2img sketch
          image = sketch
          mask = None
      elif mode == 2:  # inpaint
          image, mask = (
              init_img_with_mask["image"],
              init_img_with_mask["mask"],
          )
          mask = processing.create_binary_mask(mask)
      elif mode == 3:  # inpaint sketch
          image = inpaint_color_sketch
          orig = inpaint_color_sketch_orig or inpaint_color_sketch
          pred = np.any(np.array(image) != np.array(orig), axis=-1)
          mask = Image.fromarray(pred.astype(np.uint8) * 255, "L")
          mask = ImageEnhance.Brightness(mask).enhance(
              1 - mask_alpha / 100
          )
          blur = ImageFilter.GaussianBlur(mask_blur)
          image = Image.composite(
              image.filter(blur), orig, mask.filter(blur)
          )
      elif mode == 4:  # inpaint upload mask
          image = init_img_inpaint
          mask = init_mask_inpaint
      else:
          image = None
          mask = None
  
      # Use the EXIF orientation of photos taken by smartphones.
      if image is not None:
          image = ImageOps.exif_transpose(image)
  
      if selected_scale_tab == 1 and not is_batch:
          assert image, "Can't scale by because no image is selected"
  
          width = int(image.width * scale_by)
          height = int(image.height * scale_by)
  
      assert (
          0.0 <= denoising_strength <= 1.0
      ), "can only work with strength in [0.0, 1.0]"
  
      p = StableDiffusionProcessingImg2Img(
          sd_model=shared.sd_model,
          outpath_samples=opts.outdir_samples
          or opts.outdir_img2img_samples,
          outpath_grids=opts.outdir_grids or opts.outdir_img2img_grids,
          prompt=prompt,
          negative_prompt=negative_prompt,
          styles=prompt_styles,
          sampler_name=sampler_name,
          batch_size=batch_size,
          n_iter=n_iter,
          steps=steps,
          cfg_scale=cfg_scale,
          width=width,
          height=height,
          init_images=[image],
          mask=mask,
          mask_blur=mask_blur,
          inpainting_fill=inpainting_fill,
          resize_mode=resize_mode,
          denoising_strength=denoising_strength,
          image_cfg_scale=image_cfg_scale,
          inpaint_full_res=inpaint_full_res,
          inpaint_full_res_padding=inpaint_full_res_padding,
          inpainting_mask_invert=inpainting_mask_invert,
          override_settings=override_settings,
      )
  
      p.scripts = modules.scripts.scripts_img2img
      p.script_args = args
  
      p.user = request.username
  
      if shared.opts.enable_console_prompts:
          print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
  
      if mask:
          p.extra_generation_params["Mask blur"] = mask_blur
  
      with closing(p):
          if is_batch:
              assert (
                  not shared.cmd_opts.hide_ui_dir_config
              ), "Launched with --hide-ui-dir-config, batch img2img disabled"
              processed = process_batch(
                  p,
                  img2img_batch_input_dir,
                  img2img_batch_output_dir,
                  img2img_batch_inpaint_mask_dir,
                  args,
                  to_scale=selected_scale_tab == 1,
                  scale_by=scale_by,
                  use_png_info=img2img_batch_use_png_info,
                  png_info_props=img2img_batch_png_info_props,
                  png_info_dir=img2img_batch_png_info_dir,
              )
  
              if processed is None:
                  processed = Processed(p, [], p.seed, "")
          else:
              processed = modules.scripts.scripts_img2img.run(p, *args)
              if processed is None:
                  processed = process_images(p)
  
      shared.total_tqdm.clear()
  
      generation_info_js = processed.js()
      if opts.samples_log_stdout:
          print(generation_info_js)
  
      if opts.do_not_show_images:
          processed.images = []
  
      return (
          processed.images,
          generation_info_js,
          plaintext_to_html(processed.info),
          plaintext_to_html(processed.comments, classname="comments"),
      )
  
file_path: stable-diffusion-webui/modules/img2img.py
imports: import os
  from contextlib import closing
  from pathlib import Path
  import numpy
  from PIL import Image, ImageOps, ImageFilter, ImageEnhance, UnidentifiedImageError
  import gradio
  from modules import images
  from modules.generation_parameters_copypaste import create_override_settings_dict, parse_generation_parameters
  from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
  from modules.shared import opts, state
  from modules.sd_models import get_closet_checkpoint_match
  import modules.shared
  import modules.processing
  from modules.ui import plaintext_to_html
  import modules.scripts
label_desc: Use image node_id 2 for calling a global function with example usage: img2img(id_task, mode, prompt, negative_prompt, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps, sampler_name, mask_blur, mask_alpha, inpainting_fill, n_iter, batch_size, cfg_scale, image_cfg_scale, denoising_strength, selected_scale_tab, height, width, scale_by, 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, override_settings_texts, img2img_batch_use_png_info, img2img_batch_png_info_props, img2img_batch_png_info_dir, request) and returns: processed,  generation_info_js,  plaintext_to_html,  plaintext_to_html