
刚刚跑transformer,修改代码后还总是报错,是因为数据集里面不全是RGB格式的图片,用cv2的函数时没有解决问题,最后直接convert一下,解决了问题。具体的转换代码如下:
import os
from PIL import Image
from tqdm import tqdm
import numpy as np
img_path = '' #填入图片所在文件夹的路径
img_Topath = '' #填入图片转换后的文件夹路径
for item in tqdm(img_path):
arr=item.strip().split('*')
img_name=arr[0]
image_path=os.path.join(img_path,img_name)
img=Image.open(image_path)
if(img.mode!='RGB'):
img = img.convert("RGB")
img=np.array(img)
print(img_name)
print(img.shape)
img.save(img_Topath +'/'+img_name,img)
其中,tqdm模块是一个进度条配置,首次使用需先安装,
pip install tqdm
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)