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

UnicodeDecodeError: invalid continuation byte, using dwtDct #35

Open
Hayoung93 opened this issue May 9, 2024 · 0 comments
Open

UnicodeDecodeError: invalid continuation byte, using dwtDct #35

Hayoung93 opened this issue May 9, 2024 · 0 comments

Comments

@Hayoung93
Copy link

Hayoung93 commented May 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant