postprocessing.py 4.1 KB

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