0

I have 20 Erdas imagine images. I want to read each of those images into variable "x1", "x2",... "x20" respectively, as matrices or arrays so I can query in the images. I need to read each pixel value and then reassign them. Kindly Help..

Akhil
  • 181
  • 1
  • 15

1 Answers1

1

You can use GDAL to load the images into arrays.

Example:

import numpy
import gdal
from gdalconst import *

dataset = gdal.Open("/path/image.x", GA_ReadOnly )
image_array = ds.ReadAsArray()
image_array[pix_y,pix_x,band] = 10

image_array is a numpy array, so you can acces (or process) each pixel individually:

I am not sure but I think you can do it simpler:

from osgeo import gdalnumeric

image_array = gdalnumeric.LoadFile(raster)

Edit------------------------------------------------------------------

Adding a loop for loading all the images within a directory:

from os import listdir
from osgeo import gdalnumeric

image_list = []
for file_path in os.listdir("somedirectory"):
    image_array = gdalnumeric.LoadFile(file_path)
    image_list.append(image_array)

#image_list[i] will access to each image array
phyrox
  • 2,423
  • 15
  • 23
  • Thank you so very much. This works. I just have to figure out the loop for dynamically creating variables. Thanks again :) – Akhil Jan 20 '14 at 08:38
  • It worked in terms of accepting the input. I had already figured that out. My query was about how to run a loop to automatically input each image into a single array. image_array1-->image1.img image_array2-->image2.img . . . I had figured out the input of the image into arrays. I needed to understand how to loop for dynamic variables.. – Akhil Jan 24 '14 at 09:17
  • @Akhil I am not sure if I understand... Are you trying to load 20 images into 20 arrays? If so, it is very simple (check the edit to my answer). In this link you have more information about obtaining the files within a folder: http://stackoverflow.com/questions/3207219/how-to-list-all-files-of-a-directory-in-python – phyrox Jan 24 '14 at 10:16