[PYTHON] Embed other images on the raster with ArcPy

Data example

--Image of the side to be embedded (satellite image in this example) - before.tif, - 7871 x 7751 pixels (row x column)

![2020-02-09-11-41-48.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/533950/e7990cdf-9576-61c6-3423-cc2040639824.png)

--Embedded image (credit notation on satellite image in this example) --Credit.bmp - 526 x 4300 pixels (row x column)

![2020-02-09-11-46-05.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/533950/1474dbbd-c6f6-a64c-361e-fc57c5776b7e.png)

Execution result

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

Python code

EmbedOtherImageToRaster.py


import arcpy
import numpy

inRasterLayer   = arcpy.GetParameterAsText(0)   #Raster (layer) on the embedded side
inOtherImage    = arcpy.GetParameterAsText(1)   #Image to embed
outRasterData   = arcpy.GetParameterAsText(2)   #Output destination raster
inOffsetRow     = arcpy.GetParameterAsText(3)   #Embedding destination coordinates (row direction)
inOffsetCol     = arcpy.GetParameterAsText(4)   #Embedding destination coordinates (column direction)

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()    #Output destination pixel array

#Overwrite the pixel value of the image to be embedded in the pixel array of the output destination
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]

#Save the output destination pixel array as a raster
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

reference

-Let's do image processing with ArcPy-Part 4: NumPyArray

reference

Recommended Posts

Embed other images on the raster with ArcPy
Save images on the web to Drive with Python (Colab)
Draw shapes on feature layers with ArcPy
With Django + Google Cloud Strage, ImageField images are displayed normally on the server
[Python] How to save images on the Web at once with Beautiful Soup
View images on S3 with API Gateway + Lambda
Drawing tips with matplotlib on the server side
I tried playing with the calculator on tkinter
Implement Sign In With Google on the backend side
Monitor the training model with TensorBord on Jupyter Notebook
Make a breakpoint on the c layer with python
Extract the band information of raster data with python
use something other than the default profile with boto3