Array operations: converting Spider files to Numeric arrays

Back

Numerical Python, or Numpy, offers an extension that permits operations on arrays of any dimension. Implemented in the C language, it is fast, with many scientific functions for array manipulation, support for FFT and linear algebraic operations. The Spider Python Library lets you convert SPIDER images and volumes to 2D and 3D arrays, respectively, as well as to create SPIDER files from Numeric arrays. The module Spiderarray.py provides access to this large collection of array operations.

Spider/Spiderarray.py

spider2array (filename)
Reads a SPIDER file, returns a Python Numeric array.
Images are converted to 2D arrays, volumes to 3D arrays.

array2spider (arr, filename)
Writes a Python Numeric array out to a file in SPIDER format.

In addition, the following functions convert PIL Images <--> Numeric arrays

image2array (im)
Takes a PIL image as input, returns a Numeric array.

array2image (arr)
Takes a 2D Numeric array as input, returns a PIL image.


Example: Create a test image in Python Numeric, save it as a SPIDER image.
import numpy
from Spider.Spiderarray import array2spider

size = 65

I = numpy.zeros((size,size))   # create a blank array

for j in range(size):
    for i in range(size):
        I[j][i] = i              # Array[row][column]

array2spider(I, 'tmp001.dat')