Differences between revisions 6 and 7
Revision 6 as of 2012-06-18 04:49:44
Size: 572
Editor: SteveLudtke
Comment:
Revision 7 as of 2015-08-14 22:11:14
Size: 596
Editor: SteveLudtke
Comment:
Deletions are marked like this. Additions are marked like this.
Line 7: Line 7:
for k in a["nz"]:
    for j in a["ny"]:
        for i in a["nx"]:
for k in xrange(a["nz"]):
    for j in xrange(a["ny"]):
        for i in xrange(a["nx"]):

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 xrange(a["nz"]):
   3     for j in xrange(a["ny"]):
   4         for i in xrange(a["nx"]):
   5             pixel_value = a[i,j,k] # get the pixel value
   6             a[i,j,k]=pixel_value*3 # set the pixel value

Note that you can do any of:

   1 a[x]
   2 a[x,y]
   3 a[x,y,z]
   4 a[attr_name]

The fourth form allows getting and setting attributes equivalent to set_attr() and get_attr().

EMAN2/Tutorials/iter_pixels (last edited 2015-08-14 22:11:14 by SteveLudtke)