[GIS] Does gdal_calc.py not calculate arithmetic with in the same file

gdal

I have one GeoTIFF file with 2 bands. The data type is Byte. I want to calculate (Band2-Band1)/(Band2+Band1).

But the following command provided a dark image:

gdal_calc.py -A infile --A_band 1 -B infile, --B_band 0 --outfile outfile --calc "(A-B)/(A+B)" 

Does gdal_calc.py not calculate arithmetic with in the same file?

EDIT:

I run the following code using python:

import subprocess
gcalc = 'C:\\Users\\cleo\\Documents\\gdalpys\\gdal_calc.py'

a = 'D:\\a.tif'
b = 'D:\\b.tif'
outfile = 'D:\\result.tif'
expr = '(A-B)/(A+B)'
subprocess.call([sys.executable,gcalc,'--type','Float32','-A',a,'--A_band','1','-B',b,'--B_band','0','--outfile',outfile,\
    '--calc',expr],shell=True)  
a,b,outfile = None, None,None

gdalinfo -stats is as follows:

Driver: GTiff/GeoTIFF

Files: D:\tt.tif

Size is 36001, 36001

Coordinate System is:

GEOGCS["WGS 84",

    DATUM["WGS_1984",

        SPHEROID["WGS 84",6378137,298.257223563,

            AUTHORITY["EPSG","7030"]],

        AUTHORITY["EPSG","6326"]],

    PRIMEM["Greenwich",0],

    UNIT["degree",0.0174532925199433],

    AUTHORITY["EPSG","4326"]]

Origin = (-0.000138888888898,0.000138888888884)

Pixel Size = (0.000277777777778,-0.000277777777778)

Metadata:

  AREA_OR_POINT=Area

Image Structure Metadata:

  INTERLEAVE=BAND

Corner Coordinates:

Upper Left  (  -0.0001389,   0.0001389) (  0d 0' 0.50"W,  0d 0' 0.50"N)

Lower Left  (  -0.0001389, -10.0001389) (  0d 0' 0.50"W, 10d 0' 0.50"S)

Upper Right (  10.0001389,   0.0001389) ( 10d 0' 0.50"E,  0d 0' 0.50"N)

Lower Right (  10.0001389, -10.0001389) ( 10d 0' 0.50"E, 10d 0' 0.50"S)

Center      (   5.0000000,  -5.0000000) (  5d 0' 0.00"E,  5d 0' 0.00"S)

Band 1 Block=36001x1 Type=Float32, ColorInterp=Gray

  NoData Value=1.175494351e-038

Best Answer

This is an old question, but I've just run into what I think is a similar problem dividing two integer grids to get a percentage. The key for me was to convert them to float grids in the calculation. Just setting the output type didn't help. I am running GDAL 1.11.2. Example code is:

gdal_calc.py \
-A "_t2.tiff" \
-B "_t3.tiff" \
--calc="(A.astype(float)/B.astype(float))*100.0" \
--overwrite \
--type Float32 \
--outfile="_t4.tiff" 

A.astype(float) is numpy syntax to cast the integer grids to floats.

Related Question