コンテンツへスキップ

ArcPyでラスター上に他の画像を埋め込む

データ例

  • 埋め込まれる側の画像(ここでの例では衛星画像)
    • before.tif,
    • 7871 x 7751 pixels (row x column)
  • 埋め込む画像(ここでの例では衛星画像に載せるクレジット表記)
    • クレジット.bmp
    • 526 x 4300 pixels (row x column)

※Landsat-8画像にクレジット表記が必要かどうかの規約や、必要な場合の表記フォーマットの情報がいまいちよく分からなかったため、ここではあくまでも例として。

実行結果

2020-02-09-11-42-53.png

Pythonコード

import arcpy
import numpy

inRasterLayer   = arcpy.GetParameterAsText(0)   # 埋め込まれる側のラスター(レイヤー)
inOtherImage    = arcpy.GetParameterAsText(1)   # 埋め込む画像
outRasterData   = arcpy.GetParameterAsText(2)   # 出力先ラスター
inOffsetRow     = arcpy.GetParameterAsText(3)   # 埋め込み先の座標(行方向)
inOffsetCol     = arcpy.GetParameterAsText(4)   # 埋め込み先の座標(列方向)

baseRaster = arcpy.Raster(inRasterLayer)
baseArray = arcpy.RasterToNumPyArray(baseRaster)

otherRaster = arcpy.Raster(inOtherImage)
otherArray = arcpy.RasterToNumPyArray(otherRaster)

offsetRow = int(inOffsetRow)
offsetCol = int(inOffsetCol)

outArray = baseArray.copy()    # 出力先の画素配列

# 出力先の画素配列に、埋め込む画像の画素値を上書き
for band in range(0, 3):
    for row in range(0, otherArray.shape[1]):
        for col in range(0, otherArray.shape[2]):
            outArray[band, offsetRow + row, offsetCol + col] = otherArray[band, row, col]

# 出力先の画素配列を、ラスターとして保存
outRaster = arcpy.NumPyArrayToRaster(
    outArray,
    arcpy.Point(baseRaster.extent.XMin, baseRaster.extent.YMin),
    baseRaster.meanCellWidth,
    baseRaster.meanCellHeight)
arcpy.DefineProjection_management(outRaster, baseRaster.spatialReference)

compressionTypes = {
    'LZ77'  : 'LZ77',
    'JPEG'  : 'JPEG',
    'JPEG2000'  : 'JPEG2000',
    'PACKBITS'  : 'PackBits',
    'LZW'   : 'LZW',
    'RLE'   : 'RLE',
    'CCITT GROUP 3' : 'CCITT_G3',
    'CCITT GROUP 4' : 'CCITT_G4',
    'CCITT (1D)'    : 'CCITT_1D',
    'None'  : 'NONE' }
arcpy.env.compression = compressionTypes[baseRaster.compressionType]

pixelTypes = {
    'U1'    : '1_BIT',
    'U2'    : '2_BIT',
    'U4'    : '4_BIT',
    'U8'    : '8_BIT_UNSIGNED',
    'S8'    : '8_BIT_SIGNED',
    'U16'   : '16_BIT_UNSIGNED',
    'S16'   : '16_BIT_SIGNED',
    'U32'   : '32_BIT_UNSIGNED',
    'S32'   : '32_BIT_SIGNED',
    'F32'   : '32_BIT_FLOAT',
    'F64'   : '64_BIT' }
formats = {
    'BIL'   : 'BIL',
    'BIP'   : 'BIP',
    'BMP'   : 'BMP',
    'BSQ'   : 'BSQ',
    'DAT'   : 'ENVI',
    'GIF'   : 'GIF',
    'Grid'  : 'GRID',
    'IMAGINE Image' : 'IMAGINE Image',
    'JP2000'        : 'JP2',
    'JPEG'          : 'JPEG',
    'PNG'           : 'PNG',
    'TIFF'          : 'TIFF' }
arcpy.CopyRaster_management(outRaster, outRasterData,
                            pixel_type = pixelTypes[baseRaster.pixelType],
                            format = formats[baseRaster.format],
                            nodata_value = baseRaster.noDataValue)

del baseRaster, baseArray, otherRaster, otherArray, outArray, outRaster

参考

リファレンス

コメントを残す

%d人のブロガーが「いいね」をつけました。