TypeError: img is not a numpy array, neither a scalar

代码如下:

# coding: utf-8

import cv2
import numpy as np
from PIL import Image


if __name__ == '__main__':
    image = Image.open('1.jpg')
    cv2.imwrite('1_.jpg', image)

出错:

Error: img is not a numpy array, neither a scalar

原来image = Image.open('1.jpg')获得的image是

无法直接写,需要转为数据,如下:

# coding: utf-8

import cv2
import numpy as np
from PIL import Image


if __name__ == '__main__':
    image = Image.open('1.jpg')
    print(type(image))
    image_data = np.asarray(image)
    cv2.imwrite('1_.jpg', image_data)

如果是用cv2.imread()读取的图片,类型为,可以直接写,若要转为,如下:

# coding: utf-8

import cv2
import numpy as np
from PIL import Image


if __name__ == '__main__':
    # image = Image.open('1.jpg')
    # print(type(image))
    # image_data = np.asarray(image)
    # cv2.imwrite('1_.jpg', image)
    #

    img = cv2.imread('1.jpg')
    print(type(img))
    im = Image.fromarray(img)
    print(type(im))

你可能感兴趣的:(计算机视觉)