1. 当路径全为英文时,采用的图像读取和保存方式
import cv2
# 读取图像
img = cv2.imread("./raw.jpg", 0) # 0: gray image;1: bgr 图像; -1: 图像原格式输出
# 保存图像
cv2.imwrite("./save.jpg", img)
2. 当路径不全为英文时,即路径中可能存在中文,采用的图像读取和保存方式
# 读文件, 这种方式能读取路径中含有中文的图像文件
def imread(path):
# image_array = cv2.cvtColor(cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1), cv2.COLOR_BGR2RGB)
image_array = cv2.imdecode(np.fromfile(path, dtype=np.uint8), -1) #
return image_array
# 存文件
def imsave(path, image):
# image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.imencode('.png', image)[1].tofile(path)