postprocessing.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import os
  2. from PIL import Image
  3. from modules import shared, images, devices, scripts, scripts_postprocessing, ui_common, infotext_utils
  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 = images.fix_image(img)
  14. fn = ''
  15. else:
  16. image = images.read(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. yield filename, filename
  25. else:
  26. assert image, 'image not selected'
  27. yield image, None
  28. if extras_mode == 2 and output_dir != '':
  29. outpath = output_dir
  30. else:
  31. outpath = opts.outdir_samples or opts.outdir_extras_samples
  32. infotext = ''
  33. data_to_process = list(get_images(extras_mode, image, image_folder, input_dir))
  34. shared.state.job_count = len(data_to_process)
  35. for image_placeholder, name in data_to_process:
  36. image_data: Image.Image
  37. shared.state.nextjob()
  38. shared.state.textinfo = name
  39. shared.state.skipped = False
  40. if shared.state.interrupted:
  41. break
  42. if isinstance(image_placeholder, str):
  43. try:
  44. image_data = images.read(image_placeholder)
  45. except Exception:
  46. continue
  47. else:
  48. image_data = image_placeholder
  49. parameters, existing_pnginfo = images.read_info_from_image(image_data)
  50. if parameters:
  51. existing_pnginfo["parameters"] = parameters
  52. initial_pp = scripts_postprocessing.PostprocessedImage(image_data.convert("RGB"))
  53. scripts.scripts_postproc.run(initial_pp, args)
  54. if shared.state.skipped:
  55. continue
  56. used_suffixes = {}
  57. for pp in [initial_pp, *initial_pp.extra_images]:
  58. suffix = pp.get_suffix(used_suffixes)
  59. if opts.use_original_name_batch and name is not None:
  60. basename = os.path.splitext(os.path.basename(name))[0]
  61. forced_filename = basename + suffix
  62. else:
  63. basename = ''
  64. forced_filename = None
  65. infotext = ", ".join([k if k == v else f'{k}: {infotext_utils.quote(v)}' for k, v in pp.info.items() if v is not None])
  66. if opts.enable_pnginfo:
  67. pp.image.info = existing_pnginfo
  68. pp.image.info["postprocessing"] = infotext
  69. shared.state.assign_current_image(pp.image)
  70. if save_output:
  71. fullfn, _ = images.save_image(pp.image, path=outpath, basename=basename, extension=opts.samples_format, info=infotext, short_filename=True, no_prompt=True, grid=False, pnginfo_section_name="extras", existing_info=existing_pnginfo, forced_filename=forced_filename, suffix=suffix)
  72. if pp.caption:
  73. caption_filename = os.path.splitext(fullfn)[0] + ".txt"
  74. existing_caption = ""
  75. try:
  76. with open(caption_filename, encoding="utf8") as file:
  77. existing_caption = file.read().strip()
  78. except FileNotFoundError:
  79. pass
  80. action = shared.opts.postprocessing_existing_caption_action
  81. if action == 'Prepend' and existing_caption:
  82. caption = f"{existing_caption} {pp.caption}"
  83. elif action == 'Append' and existing_caption:
  84. caption = f"{pp.caption} {existing_caption}"
  85. elif action == 'Keep' and existing_caption:
  86. caption = existing_caption
  87. else:
  88. caption = pp.caption
  89. caption = caption.strip()
  90. if caption:
  91. with open(caption_filename, "w", encoding="utf8") as file:
  92. file.write(caption)
  93. if extras_mode != 2 or show_extras_results:
  94. outputs.append(pp.image)
  95. image_data.close()
  96. devices.torch_gc()
  97. shared.state.end()
  98. return outputs, ui_common.plaintext_to_html(infotext), ''
  99. def run_postprocessing_webui(id_task, *args, **kwargs):
  100. return run_postprocessing(*args, **kwargs)
  101. 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):
  102. """old handler for API"""
  103. args = scripts.scripts_postproc.create_args_for_run({
  104. "Upscale": {
  105. "upscale_mode": resize_mode,
  106. "upscale_by": upscaling_resize,
  107. "upscale_to_width": upscaling_resize_w,
  108. "upscale_to_height": upscaling_resize_h,
  109. "upscale_crop": upscaling_crop,
  110. "upscaler_1_name": extras_upscaler_1,
  111. "upscaler_2_name": extras_upscaler_2,
  112. "upscaler_2_visibility": extras_upscaler_2_visibility,
  113. },
  114. "GFPGAN": {
  115. "enable": True,
  116. "gfpgan_visibility": gfpgan_visibility,
  117. },
  118. "CodeFormer": {
  119. "enable": True,
  120. "codeformer_visibility": codeformer_visibility,
  121. "codeformer_weight": codeformer_weight,
  122. },
  123. })
  124. return run_postprocessing(extras_mode, image, image_folder, input_dir, output_dir, show_extras_results, *args, save_output=save_output)