diff --git a/invokeai/backend/image_util/imwatermark/vendor.py b/invokeai/backend/image_util/imwatermark/vendor.py index ef06274ff73..d9d2850c6a9 100644 --- a/invokeai/backend/image_util/imwatermark/vendor.py +++ b/invokeai/backend/image_util/imwatermark/vendor.py @@ -7,9 +7,10 @@ # `opencv-contrib-python`. It's easier to copy the code over than complicate the installation process by # requiring an extra post-install step of removing `opencv-python` and installing `opencv-contrib-python`. +import base64 import struct import uuid -import base64 + import cv2 import numpy as np import pywt @@ -96,20 +97,18 @@ def encode(self, cv2Image, method="dwtDct", **configs): class WatermarkDecoder(object): def __init__(self, wm_type="bytes", length=0): - self._wmType = wm_type if wm_type == "ipv4": - self._wmLen = 32 + wm_len = 32 elif wm_type == "uuid": - self._wmLen = 128 - elif wm_type == "bytes": - self._wmLen = length - elif wm_type == "bits": - self._wmLen = length - elif wm_type == "b16": - self._wmLen = length + wm_len = 128 + elif wm_type in ("bytes", "bits", "b16"): + wm_len = length else: raise NameError("%s is unsupported" % wm_type) + self._wmType = wm_type + self._wmLen = wm_len + def reconstruct_ipv4(self, bits): ips = [str(ip) for ip in list(np.packbits(bits))] return ".".join(ips) @@ -153,11 +152,11 @@ def reconstruct(self, bits): return self.reconstruct_bytes(bits) def decode(self, cv2Image, method="dwtDct", **configs): - (r, c, channels) = cv2Image.shape - if r * c < 256 * 256: + shape = cv2Image.shape + # Unpack once, avoid unpacking and creating 3 variables for big images + r, c = shape[0], shape[1] + if r * c < 65536: # 256*256 = 65536 raise RuntimeError("image too small, should be larger than 256x256") - - bits = [] if method == "dwtDct": embed = EmbedMaxDct(watermarks=[], wmLen=self._wmLen, **configs) bits = embed.decode(cv2Image)