Select to view content in your preferred language

Export Rasters in RGB using Python

916
2
03-01-2012 06:36 AM
OsmanSABAN
Emerging Contributor
Hi,

I am using below code from Python scripts in ArcGIS 10 for exporting rasters from Landsat imagery with L1G extension. Each imagery has 7 bands. I want to export each image in true RGB color. With below code, I created a tool in toolbox with this script but I couldn't get it work and made a search online, people were referring to change the extension from L1G to bsq. I made the change and it worked perfectly in Black and white color. Now, my problem is that i am stuck here that I can't export them as RGB color. Can anyone help me to modify this code little bit to export Landsat imagery with 7 bands in RGB color? or how can i pass the RGB color into exported raster images?

Any help would be appreciated.

Thank you all.

Code:

import ConversionUtils, time

msgWorkspace=ConversionUtils.gp.GetIDMessage(86127) #"Output location does not exist: "
msgSuccess= ConversionUtils.gp.GetIDMessage(86128) #"Successfully converted: "
msgFail=ConversionUtils.gp.GetIDMessage(86129) #"Failed to convert: "
msgConverting = ConversionUtils.gp.GetIDMessage(86130) #"Converting "

# Argument 1 is the list of Rasters to be converted
inRasters = ConversionUtils.gp.GetParameterAsText(0)

# The list is split by semicolons ";"
inRasters = ConversionUtils.SplitMultiInputs(inRasters)

# The output workspace where the shapefiles are created
outWorkspace = ConversionUtils.gp.GetParameterAsText(1)

# Set the destination workspace parameter (which is the same value as the output workspace)
# the purpose of this parameter is to allow connectivity in Model Builder.
# ConversionUtils.gp.SetParameterAsText(2,outWorkspace)
ext = ConversionUtils.gp.GetParameterAsText(2)

# Get proper extension based on the format string
if (ext == "IMAGINE Image"):
    ext = ".img"
elif (ext == "TIFF"):
    ext = ".tif"
elif (ext == "BMP"):
    ext = ".bmp"
elif (ext == "PNG"):
    ext = ".png"
elif (ext == "JPEG"):
    ext = ".jpg"
elif (ext == "JP2000"):
    ext = ".jp2"
elif (ext == "GIF"):
    ext = ".gif"
elif (ext == "GRID"):
    ext = ""
elif (ext == "BIL"):
    ext = ".bil"
elif (ext == "BIP"):
    ext = ".bip"
elif (ext == "BSQ"):
    ext = ".bsq"
elif (ext == "ENVI DAT"):
    ext = ".dat"
elif (ext == "HDF4"):
    ext = ".L1G"

# Add progressor
rastercnt = len(inRasters)
ConversionUtils.gp.SetProgressor("step", msgConverting, 0, rastercnt, 1)
currentloc = 1

# Loop through the list of input Rasters and convert/copy each to the output geodatabase or folder
for raster in inRasters:
    try:
        ConversionUtils.gp.SetProgressorLabel(msgConverting + "%s (%d/%d)" % (raster, currentloc, rastercnt))
        raster = ConversionUtils.ValidateInputRaster(raster)
       
        outRaster = ConversionUtils.GenerateRasterName(raster, outWorkspace, ext)

        # Copy/Convert the inRaster to the outRaster
        ConversionUtils.CopyRasters(raster, outRaster, "")
       
        # If the Copy/Convert was successfull add a message stating this
        ConversionUtils.gp.AddMessage(msgSuccess + "%s To %s" % (raster, outRaster))

        currentloc += 1

    except Exception, ErrorDesc:
        # Except block for the loop. If the tool fails to convert one of the Rasters, it will come into this block
        #  and add warnings to the messages, then proceed to attempt to convert the next input Raster.
        msgWarning = msgFail + "%s" % input
        msgStr = ConversionUtils.gp.GetMessages(2)
        ConversionUtils.gp.AddWarning(ConversionUtils.ExceptionMessages(msgWarning, msgStr, ErrorDesc))
       
    ConversionUtils.gp.SetProgressorPosition()
   
time.sleep(0.5)
0 Kudos
2 Replies
OsmanSABAN
Emerging Contributor
Hi again,

I urgently need help. please help me about this.

Thank you
0 Kudos
OsmanSABAN
Emerging Contributor
Hi all again,

Please help me about this problem.

Help, help, help.

Thank you all
0 Kudos