【问题标题】:How to resize a raster image (TIFF) to 800x600 using rasterio?如何使用 rasterio 将光栅图像 (TIFF) 的大小调整为 800x600?
【发布时间】:2020-07-18 06:36:05
【问题描述】:

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

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

我使用了 Reader 代码 sn-p:

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),但不知道这是怎么做到的。

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

谢谢

【问题讨论】:

  • @Rishav 谢谢,就错误而言,我没有看到任何突出的东西。这里可能出了什么问题?
  • @Rishav 在我的 Spyder IDE 中告诉我“窗口”是一个未定义的名称。
  • 我的意思是,先做from rasterio.windows import Window
  • @Rishav 谢谢。我把 from rasterio.windows import Window 然后导入 rasterio 。它给了我错误信息: ImportError: cannot import name 'Window'

标签: python geospatial rasterio


【解决方案1】:
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()

【讨论】:

    猜你喜欢
    • 2014-03-15
    • 2014-09-01
    • 2022-06-16
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多