Skip to content

UnicodeDecodeError: invalid continuation byte, using dwtDct #35

Open
@Hayoung93

Description

Hi, currently testing on invisible watermarks, thanks for your work.
I met an error while running this code, modified some from given example:

import cv2
from imwatermark import WatermarkEncoder, WatermarkDecoder

if __name__ == "__main__":
    mode = "encode"  # [encode, decode]
    method = "dwtDct"  # [dwtDct, dwtDctSvd, rivaGan]
    wm = "test"

    if mode == "encode":
        bgr = cv2.imread("FaceShifter_765_867_frame00009_face000.png", -1)
        h, w, c = bgr.shape
        if h < 256 and w < 256:
            bgr = cv2.resize(bgr, (256, 256), interpolation=cv2.INTER_CUBIC)
        elif h < 256:
            bgr = cv2.resize(bgr, (w, 256), interpolation=cv2.INTER_CUBIC)
        elif w < 256:
            bgr = cv2.resize(bgr, (h, 256), interpolation=cv2.INTER_CUBIC)
        encoder = WatermarkEncoder()
        encoder.set_watermark("bytes", wm.encode("utf-8"))
        bgr_encoded = encoder.encode(bgr, method)
        cv2.imwrite("invisible_watermark_3_{}.png".format(method), bgr_encoded)
    else:  # decode
        bgr = cv2.imread("invisible_watermark_3_{}.png".format(method), -1)
        decoder = WatermarkDecoder("bytes", len(wm) * 8)
        watermark = decoder.decode(bgr, method)
        print(watermark)
        watermark_txt = watermark.decode("utf-8")
        with open("recon_watermark_3_{}.txt".format(method), "w") as f:
            f.write(watermark_txt + "\n")

And I get UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf4 in position 0: invalid continuation byte since the watermark after decoding shows b'\xf4\x7f\xfb\xff' while the encoded byte of test should be b'\x74\x65\x73\x74'.

Did I do something wrong? My original image size is (193, 193, 3), python version 3.8.18, cv2 version 4.9.0.80.

I also tried: b'\xf4\x7f\xfb\xff'.decode("utf-8", "replace") which gives '_\x7f__', not the valid watermark.

Many thanks for any help!

p.s. this issue is for using dwtDct, so please not recommend me to use other methods since it's not a solution.

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions