python如何将处理的图片保存


# imread()两个参数:
# 1、图片路径。
# 2、读取图片的形式(1:默认值,加载彩色图片。 0:加载灰度图片。 -1:加载原图片)
img = cv2.imread(r"0928.jpg")
# cv2.imshow('img', img)

ret, thresh1 = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)    # 阈值分割,黑白二值
ret, thresh2 = cv2.threshold(thresh1, 140, 255, cv2.THRESH_BINARY_INV)    # (黑白二值反转)

cv2.imshow('img1', thresh1)
cv2.imshow('img2', thresh2)
如何将处理图片保存下来,不显示窗口
# Canny算子是双阈值,所以需要指定两个阈值,阈值越小,边缘越丰富。
img3 = cv2.Canny(img, 60, 255)
# 对img3图像进行反转
img4 = cv2.bitwise_not(img3)
# cv2.imshow('img4', img4)

cv2.waitKey()
# 关闭窗口并取消分配任何相关的内存使用
cv2.destroyAllWindows()
  • 保存中文路径的图像
    imgFile = "../images/logoCV.png" # 读取文件的路径
    img3 = cv2.imread(imgFile, flags=1) # flags=1 读取彩色图像(BGR)
saveFile = "../images/测试图02.jpg"  # 带有中文的保存文件路径
# cv2.imwrite(saveFile, img3)  # imwrite 不支持中文路径和文件名,读取失败,但不会报错!
img_write = cv2.imencode(".jpg", img3)[1].tofile(saveFile)

cv2保存图片代码:

filepath="1.png"
cv2.imwrite(filepath, img)