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

Add node to read watermarks #6630

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion invokeai/app/invocations/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
WithBoard,
WithMetadata,
)
from invokeai.app.invocations.primitives import ImageOutput
from invokeai.app.invocations.primitives import ImageOutput, StringOutput
from invokeai.app.services.image_records.image_records_common import ImageCategory
from invokeai.app.services.shared.invocation_context import InvocationContext
from invokeai.backend.image_util.invisible_watermark import InvisibleWatermark
Expand Down Expand Up @@ -542,6 +542,25 @@ def invoke(self, context: InvocationContext) -> ImageOutput:
return ImageOutput.build(image_dto)


@invocation(
"retrieve_watermark",
title="Retrieve Invisible Watermark",
tags=["image", "watermark"],
category="image",
version="1.2.2",
)
class RetrieveWatermarkInvocation(BaseInvocation):
"""Read an invisible watermark from an image"""

image: ImageField = InputField(description="The image to read the watermark from")
watermark_len: int = InputField(default=8, description="length of watermark, in characters")

def invoke(self, context: InvocationContext) -> StringOutput:
image = context.images.get_pil(self.image.image_name)
watermark_text = InvisibleWatermark.read_watermark(image, self.watermark_len * 8)
return StringOutput(value=watermark_text)


@invocation(
"mask_edge",
title="Mask Edge",
Expand Down
9 changes: 8 additions & 1 deletion invokeai/backend/image_util/invisible_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import cv2
import numpy as np
from imwatermark import WatermarkEncoder
from imwatermark import WatermarkDecoder, WatermarkEncoder
from PIL import Image

import invokeai.backend.util.logging as logger
Expand All @@ -28,3 +28,10 @@ def add_watermark(cls, image: Image.Image, watermark_text: str) -> Image.Image:
encoder.set_watermark("bytes", watermark_text.encode("utf-8"))
bgr_encoded = encoder.encode(bgr, "dwtDct")
return Image.fromarray(cv2.cvtColor(bgr_encoded, cv2.COLOR_BGR2RGB)).convert("RGBA")

@classmethod
def read_watermark(cls, image: Image.Image, length: int = 64) -> str:
bgr = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
decoder = WatermarkDecoder("bytes", length)
watermark = decoder.decode(bgr, "dwtDct")
return watermark.decode("utf-8")