2025-05-05 21:10:00 +08:00
|
|
|
import os
|
2025-05-05 20:59:44 +08:00
|
|
|
import cv2
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
# Define a new function that supports unicode characters in file paths
|
|
|
|
def imread_unicode(path, flags=cv2.IMREAD_COLOR):
|
|
|
|
return cv2.imdecode(np.fromfile(path, dtype=np.uint8), flags)
|
|
|
|
|
|
|
|
# Override the original `cv2.imread`
|
|
|
|
cv2.imread = imread_unicode
|
|
|
|
|
|
|
|
# Define a function to support unicode characters in file paths when saving
|
|
|
|
def imwrite_unicode(path, img, params=None):
|
|
|
|
# Encode the image
|
2025-05-05 21:10:00 +08:00
|
|
|
root, ext = os.path.splitext(path)
|
|
|
|
# If no extension is found, you can choose a default extension (e.g., ".png")
|
|
|
|
if not ext:
|
|
|
|
ext = ".png"
|
|
|
|
result, encoded_img = cv2.imencode(ext, img, params if params else [])
|
2025-05-05 20:59:44 +08:00
|
|
|
|
|
|
|
if result:
|
|
|
|
encoded_img.tofile(path) # Save image using numpy's `tofile()`
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
# Override `cv2.imwrite`
|
2025-05-05 21:10:00 +08:00
|
|
|
cv2.imwrite = imwrite_unicode
|