image - rasterio:中心裁剪 geotiff、profiles 和 window_transform
问题描述
我正在尝试使用 rasterio 做一个简单的方形 geotiff 中心裁剪。
我使用 numpy 裁剪图像,它按预期工作,然后更新图像配置文件的高度和宽度。
我还使用 更新仿射变换src.window_transform
,但我做错了。仿射变换最终是不正确的。
我的代码如下。谁能告诉我我在哪里搞砸了?谢谢。
代码:
import rasterio as rio
#
# FUNCTIONS
#
def get_image_and_profile(path):
with rio.open(path) as src:
profile=src.profile
image=src.read()
return image, profile
def image_write(im,path,profile):
with rio.open(path,'w',**profile) as dst:
dst.write(im)
def crop_image(image,crp):
return image[:,crp:-crp,crp:-crp]
def crop_profile(profile,out_size,crp):
win=(
(crp,out_size+crp),
(crp,out_size+crp))
profile=profile.copy()
profile.pop('transform',None)
profile['width']=out_size
profile['height']=out_size
profile['affine']=src.window_transform(win)
return profile
#
# CROP GEOTIFF
#
path='input.tif'
out_path='output.tif'
crp=92
im,profile=get_image_and_profile(path)
im=crop_image(im,crp)
profile=crop_profile(profile,im.shape[1],crp)
image_write(im,out_path,profile)
解决方案
推荐阅读
- vector - 一个一个地访问向量元素 opencl
- java - 带有 pdfbox 1.8.8 的数字可见标志
- python - 如何修复网络输入形状
- jquery - Laravel - 返回所有帖子,除了一些有条件的帖子
- windows - 如何在 Windows 上使用 OpenGL ES 和 GLFW?
- graphql - NestJS + GraphQL 联合和模式优先 GraphQLDefinitionsFactory
- python - 在 pandas DataFrame 列中使用字符串格式
- flutter - 按下后图标不改变 - Flutter
- python - python redis更新复杂对象(嵌套)(HMSET)
- reactjs - GA 没有看到动态标题标签