postprocessing.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import os
  2. from PIL import Image
  3. from modules import shared, images, devices, scripts, scripts_postprocessing, ui_common, generation_parameters_copypaste
  4. from modules.shared import opts
  5. def run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output: bool = True):
  6. devices.torch_gc()
  7. shared.state.begin()
  8. shared.state.job = 'extras'
  9. image_data = []
  10. image_names = []
  11. outputs = []
  12. if extras_mode == 1:
  13. for img in image_folder:
  14. if isinstance(img, Image.Image):
  15. image = img
  16. fn = ''
  17. else:
  18. image = Image.open(os.path.abspath(img.name))
  19. fn = os.path.splitext(img.orig_name)[0]
  20. image_data.append(image)
  21. image_names.append(fn)
  22. elif extras_mode == 2:
  23. assert not shared.cmd_opts.hide_ui_dir_config, '--hide-ui-dir-config option must be disabled'
  24. assert input_dir, 'input directory not selected'
  25. image_list = shared.listfiles(input_dir)
  26. for filename in image_list:
  27. try:
  28. image = Image.open(filename)
  29. except Exception:
  30. continue
  31. image_data.append(image)
  32. image_names.append(filename)
  33. else:
  34. assert image, 'image not selected'
  35. image_data.append(image)
  36. image_names.append(None)
  37. if extras_mode == 2 and output_dir != '':
  38. outpath = output_dir
  39. else:
  40. outpath = opts.outdir_samples or opts.outdir_extras_samples
  41. infotext = ''
  42. for image, name in zip(image_data, image_names):
  43. shared.state.textinfo = name
  44. existing_pnginfo = image.info or {}
  45. pp = scripts_postprocessing.PostprocessedImage(image.convert("RGB"))
  46. scripts.scripts_postproc.run(pp, args)
  47. if opts.use_original_name_batch and name is not None:
  48. basename = os.path.splitext(os.path.basename(name))[0]
  49. else:
  50. basename = ''
  51. infotext = ", ".join([k if k == v else f'{k}: {generation_parameters_copypaste.quote(v)}' for k, v in pp.info.items() if v is not None])
  52. if opts.enable_pnginfo:
  53. pp.image.info = existing_pnginfo
  54. pp.image.info["postprocessing"] = infotext
  55. if save_output:
  56. images.save_image(pp.image, path=outpath, basename=basename, seed=None, prompt=None, extension=opts.samples_format, info=infotext, short_filename=True, no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=existing_pnginfo, forced_filename=None)
  57. if extras_mode != 2 or show_extras_results:
  58. outputs.append(pp.image)
  59. devices.torch_gc()
  60. return outputs, ui_common.plaintext_to_html(infotext), ''
  61. def run_extras(extras_mode, resize_mode, image, image_folder, input_dir, output_dir, show_extras_results, gfpgan_visibility, codeformer_visibility, codeformer_weight, upscaling_resize, upscaling_resize_w, upscaling_resize_h, upscaling_crop, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility, upscale_first: bool, save_output: bool = True):
  62. """old handler for API"""
  63. args = scripts.scripts_postproc.create_args_for_run({
  64. "Upscale": {
  65. "upscale_mode": resize_mode,
  66. "upscale_by": upscaling_resize,
  67. "upscale_to_width": upscaling_resize_w,
  68. "upscale_to_height": upscaling_resize_h,
  69. "upscale_crop": upscaling_crop,
  70. "upscaler_1_name": extras_upscaler_1,
  71. "upscaler_2_name": extras_upscaler_2,
  72. "upscaler_2_visibility": extras_upscaler_2_visibility,
  73. },
  74. "GFPGAN": {
  75. "gfpgan_visibility": gfpgan_visibility,
  76. },
  77. "CodeFormer": {
  78. "codeformer_visibility": codeformer_visibility,
  79. "codeformer_weight": codeformer_weight,
  80. },
  81. })
  82. return run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output=save_output)