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