使用PIL读取多页TIFF时遭遇‘Unrecognized image mode’错误
多页TIFF读取时"Unrecognized image mode"问题分析与根因
问题场景
使用PIL逐帧读取多页TIFF文件,目标文件大部分页面为位深度1、Group4压缩的二值图像,加载过程中抛出ValueError: unrecognized image mode错误。
错误栈信息
self._ensure_mutable() File "****\env\lib\site-packages\PIL\Image.py", line 599, in _ensure_mutable self._copy() File "***\env\lib\site-packages\PIL\Image.py", line 592, in _copy self.load() File "***\env\lib\site-packages\PIL\TiffImagePlugin.py", line 1181, in load return self._load_libtiff() File "***\env\lib\site-packages\PIL\TiffImagePlugin.py", line 1219, in _load_libtiff Image.Image.load(self) File "***\env\lib\site-packages\PIL\Image.py", line 820, in load self.im.putpalette(mode, arr) ValueError: unrecognized image mode
根因排查
- 文件属性检测:该多页TIFF存在模式不一致的页面——多数为单通道二值模式,其中一页为调色板模式(
P)且采用TIFF_LZW压缩 - PIL加载逻辑问题:加载多页TIFF时,会共享调色板相关属性。当识别到某一页带有调色板后,会对所有页面统一调用
self.im.putpalette(mode, arr)方法,非调色板模式的页面无法兼容该操作,最终触发错误。
内容的提问来源于stack exchange,提问作者Pradheep Raaj
相关产品推荐
相关产品推荐

