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


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))

波段2.tif,波段3.tif,波段4.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

您需要将每个通道转换为亮度通道。所以取而代之的是:

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

您需要这样做:

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

与其他渠道相同,对于合并顺序也应考虑。

最新更新