繁体   English   中英

如何使用光栅将光栅图像 (TIFF) 大小调整为 800x600?

[英]How to resize a raster image (TIFF) to 800x600 using rasterio?

我想制作光栅图像的子集并将其放在 800x600 的尺寸中。 我正在浏览 Rasterio 食谱,但它似乎不允许我输入尺寸,例如 800x600。 这是我一直在看的内容: https : //mapbox.s3.amazonaws.com/playground/perrygeo/rasterio-docs/cookbook.html

另外,我看到了这个并认为它可能有效: https : //rasterio.readthedocs.io/en/latest/topics/windowed-rw.html

我使用了阅读器代码片段:

import rasterio
with rasterio.open('MyRasterImage.tif') as src:
    w = src.read(1, window=Window(0, 0, 800, 600))

print(w.shape)

但是,当我去运行它时,它给了我错误消息:

w = src.read(1, window = Window(0, 0, 800, 600))

NameError: name 'Window' is not defined

不确定是什么导致了这个错误。 我在想 Windows 是 rasterio 中的一个内置函数,我可以简单地调用它并调整图像大小以制作一个子集。

我还希望能够在屏幕上显示新的 800x600 图像(使用 Spyder),但不确定这是如何完成的。

任何和所有的帮助将不胜感激,并会赞成。

谢谢

import numpy
import rasterio
from matplotlib import pyplot
from rasterio.windows import Window

width = 800
height = 600

with rasterio.open('MyRasterImage.tif') as src:
    w = src.read(1, window=Window(0, 0, width, height))
    profile = src.profile
    profile['width'] = width
    profile['height'] = height
    # Create output
    result = numpy.full((width, height), dtype=profile['dtype'], fill_value=profile['nodata'])

#writting
with rasterio.open('/tmp/sampled_image.tif', 'w', **profile) as dataset:
    dataset.write_band(1, result)

#plotting
with rasterio.open('/tmp/sampled_image.tif') as src:
    pyplot.imshow(src.read(1), cmap='pink')
    pyplot.show()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM