Select to view content in your preferred language

How to multiply of two rasters in Python

4479
2
05-03-2011 04:47 PM
LukaszB_
Emerging Contributor
How to multiply two rasters in Python?

I wrote script:
import arcpy
from arcpy import env
arcpy.CheckOutExtension('Spatial')
#folder with pixels: 0-bad and 1-good value
env.workspace = "B:\mask"
masks = arcpy.ListRasters("*", "")
#folder with  value of pixels from 0 to 255
env.workspace = "B:\raster"
rastlist = arcpy.ListRasters("*", "")
for mask in masks:
    maskname = str(mask)
    for raster in rastlist:
        rastname = str(raster)
        if maskname[:25]==rastname[:25]:
          r= mask*raster
          env.workspace = "B:\MODIS\MOD15A2\converted\n"
          r.save(rastname)

But it doesn't multiply rasters. It is some problem with :
r= mask*raster
In Python window shows :

"Runtime error <type 'exceptions.TypeError'>: can't multiply sequence by non-int of type 'unicode'"
Tags (2)
0 Kudos
2 Replies
JakeSkinner
Esri Esteemed Contributor
The code above is attempting to multiply two strings together.  I believe that is why you are receiving the error.

When multiplying rasters, try using the spatial analyst Times function.  This will successfully multiply two rasters together.
0 Kudos
LukaszB_
Emerging Contributor
One more time thanks 🙂
0 Kudos