使用python中的图像处理库PIL来实现不同图像格式的转换,有转换公式。
Image模块的convert()函数,用于不同模式图像之间的转换,分别为1,L,P,RGB,RGBA,CMYK,YCbCr,I,F。
另:array和fromarray
img = Image.open(img_path) # 读入
im_arr = np.array(img.covert('L')) # 转为灰度模式,转为数组
im = np.asarray(im_arr, dtype="float32") # 数组元素转为浮点型
img = Image.fromarray(im) # 再转回图像,与array相反操作