PIL.JpegImagePlugin.JpegImageFile与numpy.ndarray的相互转换

PIL.JpegImagePlugin.JpegImageFile与numpy.ndarray的相互转换

PIL.JpegImagePlugin.JpegImageFile是PIL库中Image的数据类型


而numpy.ndarray是numpy中(plt.imread导入的图片)array的数据类型

PIL转numpy

from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
# 图片的相对路径
path="./image.jpg"
pil_img = Image.open(path)
# PIL转numpy
num_img = np.asarray(pil_img)
plt.imshow(num_img)
plt.show()

numpy转PIL

from PIL import Image
import numpy as np
from matplotlib import pyplot as plt
# 图片的相对路径
path="./image.jpg"
num_img = plt.imread(path)
# numpy转PIL
pil_img = Image.fromarray(np.uint8(num_img))
pil_img.show()

你可能感兴趣的:(问题解决,numpy,PIL)