3种类型,默认是 numpy.array
numpy.array
PIL.Image
str
file path.
互相转换
# 从路径到 numpy.ndarray
import cv2
image_mask = cv2.imread(imagePath)
print(type(image_mask))# 从路径到 PIL.IMAGE
from PIL import Image
image_mask=Image.open(imagePath)
print(type(image_mask))# 转换 从 PIL.IMAGE 到 numpy.ndarray
np_mask = np.array(image_mask)
# 转换 从 numpy.ndarray 到 PIL.IMAGE
image_mask = Image.fromarray(np_mask)I = numpy.asarray(PIL.Image.open('test.jpg'))im = PIL.Image.fromarray(numpy.uint8(I))
参考:
https://medium.com/@HuggyMonkey/how-to-gradio-components-image-bb5e6a5b97d6