我想堆叠多个栅格,但是其中一些栅格具有float32,而某些栅格具有uint8作为dtype。这给了我这个错误:
ValueError:数组的dtype'uint8'与文件的dtype'float32'不匹配
我使用的代码:
import os
import earthpy.spatial as es
from earthpy.io import path_to_example
band_fnames = ["red.tif", "green.tif", "blue.tif"]
band_paths = [path_to_example(fname) for fname in band_fnames]
destfile = "./stack_result.tif"
arr, arr_meta = es.stack(band_paths, destfile)
有人知道如何解决这个问题吗?
您可以使用astype
numpy数组的方法:
import numpy as np
a = np.array([1, 2], dtype=np.unit8)
a.dtype
>>> dtype('uint8')
b = a.astype(np.float32)
b.dtype
>>> dtype('float32')
# values are the same but now floats
b
>>> array([1., 2.], dtype=float32)
我建议dtype
使用上述类似的方法将要堆叠的所有数组转换为相同的数组。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句