import arcpy, glob, os
from arcpy import env
from arcpy.sa import *
# Set the input workspace
arcpy.env.workspace = r"F:\madhavi\images_to_clip"
arcpy.CheckOutExtension("Spatial")
# Absolute path to your mask layer
mask = "F:\madhavi\shapefile\shp_gang_only_final.shp"
# Copying all the input rasters in an array
rasters = arcpy.ListRasters()
# Loop through rasters, append names and save files
for raster in rasters:
output_raster = raster.replace(".TIF", "_clip.TIF")
rasterObject = arcpy.gp.ExtractByMask_sa(raster, mask, output_raster)
rasterObject.save(r"F:\madhavi\clipped_images")
There are two problems that I am encountering with this Python script and they are as follows:
Firstly, the script is clipping only the first raster in the folder: F:\madhavi\images_to_clip and not for all the rasters in that folder. Therefore, the for loop is not working properly.
By the way, the folder on which the for loop is working contains only two .TIF images.
Secondly, the script is not saving the outputs in the desired folder.
I am still getting "Parsing Error SyntaxError: EOL while scanning string literal (line 18)". Therefore, there is a problem in "output_raster = raster.replace(".TIF", "_clip.TIF")".