OSError: cannot write mode F as BMP

python 保存图片是会遇到如下问题:

Traceback (most recent call last):
  File "C:\Users\zhichengpc\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\BmpImagePlugin.py", line 231, in _save
    rawmode, bits, colors = SAVE[im.mode]
KeyError: 'F'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/shibie/makeowndata.py", line 109, in
    new_im.save('C:/Users/zhichengpc/Desktop/owndata/1.bmp')
  File "C:\Users\zhichengpc\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 1950, in save
    save_handler(self, fp, filename)
  File "C:\Users\zhichengpc\AppData\Local\Programs\Python\Python36\lib\site-packages\PIL\BmpImagePlugin.py", line 233, in _save
    raise IOError("cannot write mode %s as BMP" % im.mode)
OSError: cannot write mode F as BMP

 

源代码如下:

x=np.reshape(Xtest[220],(5,8))
new_im = Image.fromarray(x)
new_im=new_im.resize((500,800))
#dst= transform.resize(new_im,(50,80))
new_im.save('C:/Users/zhichengpc/Desktop/owndata/1.bmp')

建议修改代码如下:

x=np.reshape(Xtest[220],(5,8))
new_im = Image.fromarray(x)
new_im=new_im.resize((500,800))
new_im=new_im.convert("L")#转换成灰度图
new_im.save('C:/Users/zhichengpc/Desktop/owndata/1.bmp')

将其先转换为灰度图

错误解决

 

你可能感兴趣的:(python,image)