【发布时间】:2017-11-24 10:12:53
【问题描述】:
我有一张需要翻译的图像,左移 2500 像素,上移 1500 像素,我使用了以下代码:
from PIL import Image
img = Image.open('decoupe_test4.tif')
a = 1
b = 0
c = 2500 # +left/-right
d = 0
e = 1
f = 1500 # +up/-down
translate = img.transform(img.size, Image.AFFINE, (a, b, c, d, e, f))
translate.save('translated.tif')
图片的边框没有改变,但内容改变了,所以我最终得到了这张图片,在右边:
有谁知道我怎样才能正确翻译所有图像并去掉这个黑色部分?
谢谢!
这是完整的代码,可能更清楚:
import subprocess
gm = os.path.join('C:\\','OSGeo4W64','bin','gdal_merge.py')
merge_command = ["python", gm, "-o", "mergedB04.tif", "S2A_tile_20170410_31TFJ_0\B04.jp2", "S2A_tile_20170410_31TGH_0\B04.jp2", "S2A_tile_20170410_31TGJ_0\B04.jp2", "S2A_tile_20170420_31TFH_0\B04.jp2", "S2A_tile_20170420_31TFJ_0\B04.jp2", "S2A_tile_20170420_31TGH_0\B04.jp2"]
merge_command2 = ["python", gm, "-o", "mergedB08.tif", "S2A_tile_20170410_31TFJ_0\B08.jp2", "S2A_tile_20170410_31TGH_0\B08.jp2", "S2A_tile_20170410_31TGJ_0\B08.jp2", "S2A_tile_20170420_31TFH_0\B08.jp2", "S2A_tile_20170420_31TFJ_0\B08.jp2", "S2A_tile_20170420_31TGH_0\B08.jp2"]
subprocess.call(merge_command,shell=True)
subprocess.call(merge_command2,shell=True)
from PIL import Image
import rasterio
from rasterio import Affine
outfile = r'test4.tif'
Image.MAX_IMAGE_PIXELS = 1000000000
im = Image.open('mergedB08.tif')
half_the_width = im.size[0] / 2
half_the_height = im.size[1] / 2
decoupe = im.crop(
(
half_the_width - 2500,
half_the_height - 1500,
half_the_width + 2500,
half_the_height + 1500
)
)
decoupe.save('decoupe_test4.tif')
################### test de translation
img = Image.open('decoupe_test4.tif')
a = 1
b = 0
c = 2500 #left/right (i.e. 5/-5)
d = 0
e = 1
f = 1500 #up/down (i.e. 5/-5)
translate = img.transform(img.size, Image.AFFINE, (a, b, c, d, e, f))
translate.save('translated.tif')
# remise dans le bon scr de l'image
profile = {'driver': 'GTiff',
'dtype': 'float32',
'nodata': None,
'width': decoupe.size[0],
'height': decoupe.size[1],
'count': 1,
'crs': {'init': 'epsg:32631'},
'transform': Affine(10.0, 0.0, 704880.000,0.0, -10.0, 4795110.000)
}
profile.update(driver='GTiff')
profile.update(dtype=rasterio.float32)
with rasterio.open('translated.tif') as decoupe_RAS:
RAS = decoupe_RAS.read().astype(float)
with rasterio.open(outfile, 'w', **profile) as dst:
dst.write(RAS.astype(rasterio.float32))
【问题讨论】:
-
输入图像是什么样的?输出图像应该是什么样子?
-
输入图像是一个更大图像的剪切(通过合并几个卫星图像 sentinel2 获得),但剪切后,该图像不再被引用。我设法再次给它一个 srid,但仍然有一个偏移量。输入和输出需要是同一张图片,只是通过翻译来区分
-
我们需要的是输入图像、输出图像和预期的输出图像以及重现问题的最少代码量。
标签: python image transform translation python-imaging-library