python +oengcv2报错(error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize')

保存路径和获取路径都要到具体的图片名称或者是路径中有中文,如下

file_path=r'D:\Python\python3\picture processing\IMG_0016.JPG'
saved_path = r'D:\Python\python3\picture processing\1\IMG_0016.JPG'
import cv2

file_path=r'D:\Python\python3\picture processing\IMG_0016.JPG'#路径都要到具体的图片名称
# 修改之后的图片大小
size_ = (360, 360)
img = cv2.imread(file_path)
im2 = cv2.resize(img, size_, interpolation=cv2.INTER_CUBIC)

#saved_path = r'D:\Python\python3\picture processing\1\IMG_0016.JPG'  #saved_path 保存路径也要到具体的图片名称
cv2.imwrite(file_path, im2)  #file_path保存路径也要到具体的图片名称


路径不能有中文解决方法:

img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1) #路径不能为中文解决方法
cv2.imencode('.jpg', im2)[1].tofile(file_path) # 路径不能为中文解决方法
import numpy as np
import cv2
def Lower_resolution(file_path):#降低分辨率(360*360)
    # 修改之后的图片大小
    size_ = (360, 360)
    img = cv2.imdecode(np.fromfile(file_path,dtype=np.uint8),-1) #路径不能为中文解决方法
    im2 = cv2.resize(img, size_, interpolation=cv2.INTER_CUBIC)
    #saved_path = r'D:\Python\python3\picture processing\哈哈\kjfhkj_5453.JPG'
    #cv2.imwrite(saved_path, im2)  #保存
    cv2.imencode('.jpg', im2)[1].tofile(file_path) # #路径不能为中文解决方法
file_path=r'D:\Python\python3\picture processing\哈哈\IMG_2694.JPG'
Lower_resolution(file_path)

你可能感兴趣的:(python3)