Iterating through the pixels in the EMData object

In your Python script you can use commands like this to iterate through the pixels in an EMData object

   1 a = test_image()
   2 for k in a.get_zsize():
   3     for j in a.get_ysize():
   4         for i in a.get_xsize():
   5             pixel_value = a.get(i,j,k) # get the pixel value
   6             a.set(i,j,k,pixel_value*3) # set the pixel value

Note that if you know the dimensionality of the image, say for example if the image is 2D, you can ignore the 3rd dimension when you use the get and set functions, for example like this:

   1 a = test_image()
   2 for j in a.get_ysize():
   3     for i in a.get_xsize():
   4         pixel_value = a.get(i,j) # get the pixel value using 2D syntax
   5         a.set(i,j,pixel_value*3) # set the pixel value using 2D syntax

Also, there is a generic 1D approach one can take using 1D syntax and the get_size function, which returns the total number of pixels. For example:

   1 a = test_image() # This image is 2D
   2 for i in a.get_size(): # Note get_size, not get_xsize etc.
   3     pixel_value = a.get(i) # get the pixel value using 1D syntax
   4     a.set(i,pixel_value*3) # set the pixel value using 1D syntax
   5 
   6 a = EMData(10,1,1) # a 1D EMData object
   7 for i in a.get_size(): # In the 1D case this is the same as get_xsize
   8     # use 1D get and set functions as above