Post

2 followers Follow
2
Avatar

EASI to Python

Hi,

At my company we have a few EASI scripts. I'm interested replacing some EASI scripts with python scripts. In my one EASI script I have a section of code. I'm not quite sure how I would do this in python.

if %1 > 0 then
%2 =1
else
%2 = 0
endif

Any help would be great!

Thanks,
Joey

Joey McCrae Answered

Please sign in to leave a comment.

1 comment

3
Avatar

An easy and efficient way to build a python equivalent to your EASI condition would be to use numpy's vectorize function.

Here is an example:

from numpy as np
from pci.api import gobs, datasource as ds
image_file = r'c:\my_file.pix
with ds.open_dataset(image_file) as dataset:
    my_reader = ds.BasicReader(dataset, [1])
    in_raster = my_reader.read_raster(0, 0, my_reader.width, my_reader.height)

my_chan = in_raster.data[:, :, 0]

@np.vectorize  # create decorator 
def my_func(in_chan):
    if in_chan > 0:
        return 1
    else:
        return 0

new_array = my_func(my_chan)

You can then convert new_array to your desired bit-depth using astype(), convert it to a raster using gobs.array_to_raster() and write the raster to disk.
I hope this helps!
-Shawn

Shawn Melamed 3 votes