pwem.emlib.image.image_readers module

class pwem.emlib.image.image_readers.EMANImageReader[source]

Bases: ImageReader

Image reader for eman file formats

static getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

class pwem.emlib.image.image_readers.ImageReader[source]

Bases: object

classmethod canOpenSlices()[source]

Returns True if the reader can open slices optimally. Without loading the whole file. If so, openSlice method should be implemented

static getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

classmethod open(path)[source]

Opens the image in the path and returns a numpy array with the whole file (all slices included)

classmethod openSlice(path, slice)[source]

Opens a specific slice

static write(images: ImageStack, fileName: str, isStack: bool) None[source]

Generate a stack of images or a volume from a list of PIL images. :param images: An ImageStack instance with one or more images :param fileName: Path of the new stack :param isStack: Specifies whether to generate a volume or an image stack

class pwem.emlib.image.image_readers.ImageReadersRegistry[source]

Bases: object

Class to register image readers to provide basic information about an image like dimensions or getting an image

classmethod addReader(imageReader: ImageReader)[source]
classmethod getAvailableExtensions()[source]

Returns all the extensions it can handle

classmethod getReader(filePath) ImageReader[source]

Returns the reader or None able to deal with filePath based on the extension.

classmethod open(filePath) ImageStack[source]

“Opens the file and returns and ImageStack. Accepts formats like 1@path/to/my/image.ext. In this case there can be a performance penalty since readers are reading all the stack but we are only taking one slice. This may be unavoidable in cases when you want to read a single image but not optimal when you are going to go through all the stack.

classmethod write(imgStack: ImageStack, fileName: str, isStack=False) None[source]

Generate a stack of images from a list of PIL images.

class pwem.emlib.image.image_readers.ImageStack(images=None, properties=None)[source]

Bases: object

Class to hold image stacks. A single image is considered a stack of one image

Parameters
  • images – either None, an image as returned by the readers or a list of them. Images are numpy arrays

  • properties – optional: dictionary of key value pairs for header information for those files tha may need it

append(imgStack)[source]

Appends to its local list of images the images inside the imgStack passed as parameter

classmethod asPilImage(npArray, normalize=True)[source]

Returns the npArray a numpy image :param npArray: 2d numpy array (image) :param normalize: by default it has to be normalized. Cancel this is you are sure it hase been normalized before

flip(vertically=True)[source]

Flip all images of an ImageStack horizontally or vertically. Vertically is up-down, horizontally is left-right.

flipH()[source]

Flips this stack horizontally: left to right

classmethod flipSlice(npImage: ndarray, vertically=True)[source]
flipV()[source]

Flips this stack vertically: up to down

getCentralImage(pilImage=False)[source]

Returns the central image

getImage(index=0, pilImage=False)[source]
getImages()[source]

Returns all the images

getProperties()[source]

Returns the properties dictionary

getProperty(property)[source]

Returns the property passed

invert()[source]
multiply(factor: float)[source]

Multiplies the image stack by a factor :param: factor: to multiply values by it

normalize()[source]
classmethod normalizeSlice(npImage)[source]
rotate(angle, mode=ROT_MODE.FIXED, bg=None)[source]

rotates all its images the angle (deg) passed and returns a new ImageStack rotated

classmethod rotateSlice(npArray: ndarray, angle: float, mode=ROT_MODE.FIXED, bg=None) ndarray[source]

Rotates a numpy array

scale(factors, anti_aliasing=True)[source]

Scales the stack by the factors :param: factors: Scale factors for spatial dimensions.

classmethod scaleSlice(npImage, factors, anti_aliasing=True)[source]

Scales the npImage by the factor/s :param npImage: 2d numpy array :param factors: float or sequence

The zoom factor along the axes. If a float, zoom is the same for each axis. If a sequence, zoom should contain one value for each axis.

Parameters

anti_aliasing

shift(shifts)[source]

Shifts the whole stack x and y returning a new stack.

Parameters

shift – The shift along the axes. If a float, shift is the same for each axis. If a sequence, shift should contain one value for each axis.

classmethod shiftSlice(image: ndarray, shifts: float, bg=None) ndarray[source]

Shifts a numpy array :param shifts = float or sequence. If a sequence, first value should be X shift and second Y shift

classmethod thumbnailSlice(npImage, width, height, normalize=True)[source]

Calls PIl thumbnail. It is less precise but faster than scaleSlice

transform(shifts, angle, mode=ROT_MODE.FIXED)[source]

rotates all its images the angle (deg) passed and returns a new ImageStack rotated

classmethod transformSlice(npImage: ndarray, shifts: float, angle: float, mode=ROT_MODE.FIXED, bg=None)[source]

Apply the rotation and the shift to the npImage passed

write(path)[source]
class pwem.emlib.image.image_readers.MRCImageReader[source]

Bases: ImageReader

Image reader for MRC files

classmethod canOpenSlices()[source]

Returns True if the reader can open slices optimally. Without loading the whole file. If so, openSlice method should be implemented

classmethod getArray(filename)[source]
static getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

classmethod getMrcImage(fileName)[source]
classmethod isMrcStack(mrcImg)[source]
classmethod isMrcVolume(mrcImg)[source]
classmethod open(path: str)[source]

Opens the image in the path and returns a numpy array with the whole file (all slices included)

classmethod openSlice(path, slice)[source]
Reads a given image

:param path (str) –> Image to be read

classmethod write(imageStack: ImageStack, fileName: str, isStack: bool = False, samplingRate: Optional[float] = None) None[source]

Generate a stack of images or a volume from a list of images.

class pwem.emlib.image.image_readers.PILImageReader[source]

Bases: ImageReader

PIL image reader

static getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

classmethod open(filePath: str)[source]

Opens the image in the path and returns a numpy array with the whole file (all slices included)

classmethod write(imgStack: ImageStack, fileName: str, isStack=False) None[source]

Generate a stack of images or a volume from a list of PIL images. :param images: An ImageStack instance with one or more images :param fileName: Path of the new stack :param isStack: Specifies whether to generate a volume or an image stack

class pwem.emlib.image.image_readers.ROT_MODE(value)[source]

Bases: Enum

An enumeration.

CONDITIONAL = 3
FIXED = 1
NATURAL = 2
class pwem.emlib.image.image_readers.STKImageReader(fileName)[source]

Bases: ImageReader

FLOAT32_BYTES = 4
HEADER_OFFSET = 1024
IMG_BYTES = None
TYPE = None
classmethod canOpenSlices()[source]

Returns True if the reader can open slices optimally. Without loading the whole file. If so, openSlice method should be implemented

classmethod getArray(filename)[source]
classmethod getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

header_info = None
classmethod open(path)[source]

Opens the full stack

classmethod openSlice(path, slice)[source]
Reads a given image

:param filename (str) –> Image to be read

classmethod readAll(filename)[source]
classmethod readBinary(start, end)[source]
Read bytes between start and end

:param start (int) –> Start byte :param end (int) –> End byte :returns the bytes read

classmethod readHeader()[source]
Reads the header of the current file as a dictionary

:returns The current header as a dictionary

classmethod readImage(iid)[source]
Reads a given image in the stack according to its ID

:param iid (int) –> Image id to be read :returns Image as Numpy array

classmethod readNumpy(start, end)[source]
Read bytes between start and end as a Numpy array

:param start (int) –> Start byte :param end (int) –> End byte :returns decoded bytes as Numpy array

classmethod seek(pos)[source]
Move file pointer to a given position

:param pos (int) –> Byte to move the pointer to

stk_handler = None
class pwem.emlib.image.image_readers.TiffImageReader[source]

Bases: ImageReader

Tiff image reader

static getCompatibleExtensions() list[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

classmethod open(path: str)[source]

Opens the image in the path and returns a numpy array with the whole file (all slices included)

classmethod write(imgStack: ImageStack, fileName: str, isStack=False) None[source]

Generate a stack of images or a volume from a list of PIL images. :param images: An ImageStack instance with one or more images :param fileName: Path of the new stack :param isStack: Specifies whether to generate a volume or an image stack

class pwem.emlib.image.image_readers.XMIPPImageReader[source]

Bases: ImageReader

static getCompatibleExtensions()[source]

Returns a list of the compatible extensions the reader can handle

static getDimensions(filePath)[source]

Returns the dimensions [X,Y,Z,N] of the file

classmethod open(path)[source]

Opens the image in the path and returns a numpy array with the whole file (all slices included)