The following example shows various ways of adding and multiplying images. The given example uses 3D images but the functionality generalizes for 2D images.

Adding and multiplying images

First I make different two example images that have the same dimensions

   1 [someone@localhost] e2.py
   2 
   3 Welcome to EMAN2
   4 Prompt provided by IPython
   5 Enter '?' for ipython help
   6 
   7 In [3]:  A = test_image(0)
   8 
   9 In [4]:  B = test_image(1)

or try a = test_image_3d(2), a = test_image(0) etc - see the test image gallery.

Add/multiply/subtract/divide images and store the result in a new image

   1 In [5]: C = A + B # add two images, the result is stored in C
   2 
   3 In [6]: display(C) # observe C
   4 
   5 In [7]: C = A * B # multiply the two images component wise, the result is stored in C
   6 
   7 In [7]: C = A / B # division
   8 
   9 In [8]: C = A - B # subtraction

Add/multiply B and A, store result in A

   1 In [9]: A.add(B) # Add the pixels of A to the pixels of B - result stored in A
   2 
   3 In [10]: A.mult(B) # Multiple the pixels in a by the pixels of B - result stored in A

Subtract B from A and store it in A

   1 In [11]: C = B*-1 # multiply B by negative one, store it C
   2 
   3 In [12]: A.add(C) # equivalent to subtracting B from A
   4 
   5 In [13]: A.add(B*-1) # equivalent to line above