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..
Asked
Active
Viewed 1,106 times
0
-
Do you have an example of such an image? – M4rtini Jan 18 '14 at 11:05
-
What kind of information are you looking for? – Akhil Jan 18 '14 at 11:10
-
The format of the images. If they're standard images it's fairly simple. If it's a special\proprietary format it may not be so simple. – M4rtini Jan 18 '14 at 11:12
-
They are standard Unsigned 8 Bit, 1000columnsX1500rows, single band images. – Akhil Jan 18 '14 at 11:14
-
any special file format? or simple png\tiff\jpg etc? – M4rtini Jan 18 '14 at 11:20
-
They are ERDAS Imagine ".img" format images. Not simple ones. They are outputs from ERDAS Image processing software – Akhil Jan 18 '14 at 11:28
-
I believe the GDAL package has to be used... – Akhil Jan 18 '14 at 11:29
-
yeah looks like gdal can read those files. So i suggest you go try that and come back if you have any specific problems. – M4rtini Jan 18 '14 at 11:41
1 Answers
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