Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inpaint workflow: add cv2 mask creator and args for --masked-image-path #134

Merged
merged 1 commit into from
Mar 2, 2025

Conversation

anthonywu
Copy link
Collaborator

@anthonywu anthonywu commented Mar 1, 2025

This PR bases on #129 and can be merged into @filipstrand's WIP branch to continue his work as-is without merge issues.

With the inpaint feature, we're going to expect the user or client GUIs to provide the complementary mask image.

In this PR I propose:

  1. --masked-image-path argparser addition
  2. with no additional python deps, use the already installed opencv-python/import cv2 library to display a basic window that lets the user draw a binary mask over their original image

For discussion but not implemented: maybe the --masked-image-path can be set to a value of literal interactive (or empty?) and the CLI can bring up the cv2 GUI to allow a live mask drawing. IMO should be pursued in a separate PR after the basic inpaint functionality is done, to not scope creep.

Also, if we move the cv2 tool inside the library, maybe there should be a mflux.pre_processing package for this and alike tools. (while reasonably restricting to existing library dependencies)

Usage

python tools/inpaint_mask_tool.py ~/Downloads/presidio-view.webp

GUI

image

the mask image can be seen in Preview.app as soon as you hit s for save.

image

Example

image

via mflux-generate -m dev-fill --init-image-path ~/Downloads/presidio-view.webp --masked-image-path ~/Downloads/presidio-view_mask.png --prompt "godzilla towering over the hills" --steps 8 --seed 1 (in my WIP workspace)

@anthonywu anthonywu requested a review from filipstrand March 1, 2025 23:35
@anthonywu anthonywu changed the title add cv2 mask creator and args for --masked-image-path Inpaint workflow: add cv2 mask creator and args for --masked-image-path Mar 1, 2025
@@ -68,6 +68,7 @@ def add_image_to_image_arguments(self, required=False) -> None:
self.supports_image_to_image = True
self.add_argument("--init-image-path", type=Path, required=required, default=None, help="Local path to init image")
self.add_argument("--init-image-strength", type=float, required=False, default=ui_defaults.INIT_IMAGE_STRENGTH, help=f"Controls how strongly the init image influences the output image. A value of 0.0 means no influence. (Default is {ui_defaults.INIT_IMAGE_STRENGTH})")
self.add_argument("--masked-image-path", type=Path, required=False, default=None, help="Local path to separate masked image as complement to --init-image-path")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filipstrand I'll leave it to you to remove the hard coding in #129's generate.py

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, I'll clean this up!

if self.original_image is None:
raise FileNotFoundError(f"Could not open or find the image: {image_path}")

self.mask_output_path = image_path.with_name(f"{image_path.stem}_mask").with_suffix(".png")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will save the mask image in the same parent dir as the source image (it does assume user has write access to the source image's parent directory)

trying to not over-complicate things for now, this IMO is the most obvious/friendly implementation

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make a lot of sense!

self.display_image,
text,
(10, 30),
cv2.FONT_HERSHEY_DUPLEX,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

font is funky but the Hershey font family is the only one that ships with opencv and therefore no additional dependencies

@filipstrand
Copy link
Owner

filipstrand commented Mar 2, 2025

@anthonywu Looks really good, thanks a lot for the help with the GUI!

I'll continue finishing up the last parts of the in-painting feature and include it in the release after v.0.6.0.

There is very little work left for the in-context lora feature now and I'm planning on releasing v.0.6.0 tomorrow. Currently, only the boring parts (release notes, readme, tests etc.) are left haha :). Though I have to say that cursor + sonnet 3.7 are now very helpful with this kind of work that I have a habit of putting off to the last minute!

@filipstrand filipstrand merged commit 8443006 into filipstrand:inpaint Mar 2, 2025
@anthonywu anthonywu deleted the inpaint branch March 2, 2025 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants