-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpatch_editor.py
More file actions
59 lines (43 loc) · 1.53 KB
/
patch_editor.py
File metadata and controls
59 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from PIL import Image
from dataclasses import dataclass
from functools import cached_property
from pathlib import Path
from typing import Optional
from .editor_ctx import EditorCtx
from .patch_detector import PatchDetector
from .patch_merger import PatchMerger
@dataclass
class PatchEditor:
"""
Orchestrates the patch detection and merging process.
This class acts as a high-level container for the patch editing workflow.
It initializes the PatchDetector and PatchMerger, and provides a convenient
factory method to be used from the ComfyUI node.
"""
@cached_property
def ctx(self) -> EditorCtx:
return EditorCtx()
@cached_property
def patch_detector(self) -> PatchDetector:
return PatchDetector(self.ctx)
@cached_property
def patch_merger(self) -> PatchMerger:
return PatchMerger(self.patch_detector)
def main() -> None:
work_dir = Path('/tmp/work')
original_path = work_dir / 'a.png'
edited_path = work_dir / 'a-patch.png'
output_path = work_dir / 'a-patched.png'
original_img = Image.open(original_path)
edited_img = Image.open(edited_path)
editor = PatchEditor()
editor.ctx.original_img = original_img
editor.ctx.edited_img = edited_img
merger = editor.patch_merger
print(f"Processing {original_path} and {edited_path}...")
merger.save_merged(output_path)
print(f"Saved merged image to {output_path}")
merger.save_debug(work_dir)
print(f"Saved debug images to {work_dir}")
if __name__ == '__main__':
main()