InvokeAI/invokeai/backend/image_util/invisible_watermark.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

36 lines
1.2 KiB
Python
Raw Normal View History

"""
This module defines a singleton object, "invisible_watermark" that
wraps the invisible watermark model. It respects the global "invisible_watermark"
configuration variable, that allows the watermarking to be supressed.
"""
import cv2
2023-08-18 15:13:28 +00:00
import numpy as np
from imwatermark import WatermarkEncoder
2023-08-18 15:13:28 +00:00
from PIL import Image
import invokeai.backend.util.logging as logger
2023-08-18 15:13:28 +00:00
from invokeai.app.services.config import InvokeAIAppConfig
2023-07-27 14:54:01 +00:00
config = InvokeAIAppConfig.get_config()
2023-07-27 14:54:01 +00:00
class InvisibleWatermark:
"""
Wrapper around InvisibleWatermark module.
"""
2023-07-27 14:54:01 +00:00
@classmethod
def invisible_watermark_available(self) -> bool:
return config.invisible_watermark
@classmethod
def add_watermark(self, image: Image, watermark_text: str) -> Image:
if not self.invisible_watermark_available():
return image
logger.debug(f'Applying invisible watermark "{watermark_text}"')
bgr = cv2.cvtColor(np.array(image.convert("RGB")), cv2.COLOR_RGB2BGR)
encoder = WatermarkEncoder()
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")