Skip to main content
 首页 » 编程设计

python之当我通过 python 的 PIL 将 .tif 合并到 RGB 波段时出错

2025年02月15日8birdshome

from PIL import Image 
band2 = Image.open('band2.tif') 
band3 = Image.open('band3.tif') 
band4 = Image.open('band4.tif') 
img = Image.merge("RGB",(band4,band3,band2)) 

band2.tif、band3.tif、band4.tif 在 USGS(https://earthexplorer.usgs.gov/) 中下载。 与正常的 .TIF 相比,它们可能有一些不同

错误信息为

/usr/bin/python3.5 /home/lixingang/workspace/20170405/main.py 
Traceback (most recent call last): 
  File "/home/lixingang/workspace/20170405/main.py", line 5, in <module> 
    img = Image.merge("RGB",(band4,band3,band2)) 
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 2388, in merge 
    raise ValueError("mode mismatch") 
ValueError: mode mismatch 
 
Process finished with exit code 1 

请您参考如下方法:

您需要将每个 channel 转换为亮度 channel 。所以不是这个:

band2 = Image.open('band2.tif') 

你需要这样做:

band2 = Image.open('band2.tif').convert('L') 

与其他 channel 一样,合并时也要考虑顺序。