2023-07-25 02:02:57 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
"""
|
2024-02-29 23:04:59 +00:00
|
|
|
|
2023-07-25 02:02:57 +00:00
|
|
|
import cv2
|
2023-08-18 15:13:28 +00:00
|
|
|
import numpy as np
|
2023-07-25 02:02:57 +00:00
|
|
|
from imwatermark import WatermarkEncoder
|
2023-08-18 15:13:28 +00:00
|
|
|
from PIL import Image
|
|
|
|
|
2023-07-25 02:02:57 +00:00
|
|
|
import invokeai.backend.util.logging as logger
|
2024-03-11 11:55:02 +00:00
|
|
|
from invokeai.app.services.config.config_default import get_config
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2024-03-11 11:55:02 +00:00
|
|
|
config = get_config()
|
2023-07-25 02:02:57 +00:00
|
|
|
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-07-25 02:02:57 +00:00
|
|
|
class InvisibleWatermark:
|
|
|
|
"""
|
|
|
|
Wrapper around InvisibleWatermark module.
|
|
|
|
"""
|
2023-07-27 14:54:01 +00:00
|
|
|
|
2023-07-25 02:02:57 +00:00
|
|
|
@classmethod
|
2023-10-17 05:50:52 +00:00
|
|
|
def add_watermark(cls, image: Image.Image, watermark_text: str) -> Image.Image:
|
2023-07-25 02:02:57 +00:00
|
|
|
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")
|