2022-12-01 05:33:20 +00:00
|
|
|
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
|
|
|
|
import cv2 as cv
|
2022-12-01 05:33:20 +00:00
|
|
|
import numpy
|
|
|
|
from PIL import Image, ImageOps
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2024-07-03 16:20:35 +00:00
|
|
|
from invokeai.app.invocations.baseinvocation import BaseInvocation, invocation
|
|
|
|
from invokeai.app.invocations.fields import ImageField, InputField, WithBoard, WithMetadata
|
2024-01-13 12:23:16 +00:00
|
|
|
from invokeai.app.invocations.primitives import ImageOutput
|
2024-02-05 06:16:35 +00:00
|
|
|
from invokeai.app.services.shared.invocation_context import InvocationContext
|
2023-09-11 13:57:41 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2024-03-19 11:08:16 +00:00
|
|
|
@invocation("cv_inpaint", title="OpenCV Inpaint", tags=["opencv", "inpaint"], category="inpaint", version="1.3.1")
|
2024-02-07 05:33:55 +00:00
|
|
|
class CvInpaintInvocation(BaseInvocation, WithMetadata, WithBoard):
|
2022-12-01 05:33:20 +00:00
|
|
|
"""Simple inpaint using opencv."""
|
2023-05-24 05:50:55 +00:00
|
|
|
|
2023-08-14 03:23:09 +00:00
|
|
|
image: ImageField = InputField(description="The image to inpaint")
|
|
|
|
mask: ImageField = InputField(description="The mask to use when inpainting")
|
2023-07-18 14:26:45 +00:00
|
|
|
|
2024-02-05 06:16:35 +00:00
|
|
|
def invoke(self, context: InvocationContext) -> ImageOutput:
|
2024-01-13 12:23:16 +00:00
|
|
|
image = context.images.get_pil(self.image.image_name)
|
|
|
|
mask = context.images.get_pil(self.mask.image_name)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Convert to cv image/mask
|
|
|
|
# TODO: consider making these utility functions
|
2023-03-03 06:02:00 +00:00
|
|
|
cv_image = cv.cvtColor(numpy.array(image.convert("RGB")), cv.COLOR_RGB2BGR)
|
2023-05-24 05:50:55 +00:00
|
|
|
cv_mask = numpy.array(ImageOps.invert(mask.convert("L")))
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
# Inpaint
|
|
|
|
cv_inpainted = cv.inpaint(cv_image, cv_mask, 3, cv.INPAINT_TELEA)
|
|
|
|
|
|
|
|
# Convert back to Pillow
|
|
|
|
# TODO: consider making a utility function
|
|
|
|
image_inpainted = Image.fromarray(cv.cvtColor(cv_inpainted, cv.COLOR_BGR2RGB))
|
|
|
|
|
2024-01-13 12:23:16 +00:00
|
|
|
image_dto = context.images.save(image=image_inpainted)
|
|
|
|
|
|
|
|
return ImageOutput.build(image_dto)
|