Python opencv 读取图片 和保存图片的几种方法
·
读取文件为bytes类型:
def readfile2img_bytes():
with open(img_path,'rb') as f:
img_bytes = f.read()
return img_bytes
读取bytes类型的图片转换为bgr:
def read_img_bytes2_cv2img(img_bytes):
arr1 = np.frombuffer(img_bytes,np.uint8)
cv2_img = cv2.imdecode(arr1,cv2.IMREAD_COLOR)
return cv2_img
读取bytes类型的图片转换为灰度图片:
def read_img_bytes2_cv2imggray(img_bytes):
arr1 = np.frombuffer(img_bytes,np.uint8)
cv2_imggray = cv2.imdecode(arr1,cv2.IMREAD_GRAYSCALE)
return cv2_imggray
读取bgr类型的图片转换为灰度图片:
def cv_img2gray(cv_img):
gray = cv2.cvtColor(cv_img,cv2.IMREAD_GRAYSCALE)
return gray
读取中文路径的图片转换为bgr图片:
def read_img_for_chinese_path(file_path):
cv_img = cv2.imdecode(np.fromfile(file_path, dtype=np.uint8), cv2.IMREAD_COLOR)
return cv_img
保存中文路径的图片:
def save_img_for_chinese(cv_img,save_path):
cv2.imencode('.jpg', cv_img)[1].tofile(save_path)
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)