Error:cannot write mode RGBA as JPEG

我在做图像数据的预处理时,出现了报错:cannot write mode RGBA as JPEG

原代码

dst=img.transpose(Image.FLIP_LEFT_RIGHT)#左右互换
newname='../'+name+'/'+name+'_'+str(count)+'.jpg'
dst.save(newname)

原因:RGBA意思是红色,绿色,蓝色,Alpha的色彩空间,Alpha指透明度。而JPG不支持透明度,所以要么丢弃Alpha,要么保存为.png文件

所以我选择做一个异常处理

try:
    dst.save(newname)
except:
    newname='../'+name+'/'+name+'_'+str(count)+'.png'
    dst.save(newname)

问题解决

当然,如果想舍弃Alpha的话,做一个强制转换就可以了

dst=dst.convert('RGB')
dst.save('code.jpg')

你可能感兴趣的:(图像识别)