OPENCV FOR PYTHON 学习笔记 - 图像读取显示

# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

#判断字符串是否是中文
def check_contain_chinese(check_str):
    for ch in check_str:
        if '\u4e00' <= ch <= '\u9fff':
            return True
        return False


img_path = "宸宸.jpg";
if check_contain_chinese(img_path):
    img = cv.imdecode(np.fromfile(img_path,dtype=np.uint8),cv.IMREAD_COLOR)
else:
    img = cv.imread(img_path,cv.IMREAD_COLOR)
#显示图像
cv.namedWindow("img",cv.WINDOW_NORMAL)
cv.imshow("img",img)
#使用Matplotlib绘图库显示图像
plt.imshow(img,cmap = plt.cm.gray_r)
plt.xticks([]),plt.yticks([])
plt.show()
key = cv.waitKey(0)&0xFF
if key == ord('q'):
    cv.destroyAllWindows()
elif key == ord('s'):
    #保存图像
    cv.imwrite("new.jpg",img)
    cv.destroyAllWindows()

  

posted @ 2021-08-18 10:48  HappyChen2016  阅读(45)  评论(0编辑  收藏  举报