PIL使用过程中的异常处理

本文介绍了Python Imaging Library (PIL)中常见的图像处理错误及其解决办法,包括无法识别的图像文件、图片尺寸不匹配等问题,并提供了详细的解决方案及API使用说明。
[list]
[*][color=red]IOError: cannot identify image file[/color]:表示路径对应的文件不是有效的图片
[*][color=red]ValueError: images do not match[/color]:表示图片大小和box对应的宽度不一致,参考API说明:Pastes another image into this image. The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted image must match the size of the region.使用2纬的box避免上述问题
[*][color=red]IOError: image file is truncated[/color] (40 bytes not processed) :表示路径对应的图片文件已损坏,请确认
[*][color=red]IOError: Unsupported BMP header type[/color] (56):发现还是图片已损坏
[/list]


The Python Imaging Library supports a wide variety of raster file formats. Nearly 30 different file formats can be identified and read by the library. Write support is less extensive, but most common interchange and presentation formats are supported.

The open function identifies files from their contents, not their names, but the save method looks at the name to determine which format to use, unless the format is given explicitly.


PIL根据文件内容而不是文件名后缀来读取图片,但是保存的时候可以指定某种格式(文件名后缀和格式不匹配也可以),如下
srcImg.save(targetFullFile)
srcImg.save(targetFullFile, 'jpeg')
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值