pwem.emlib.image.image_handler module

class pwem.emlib.image.image_handler.ImageHandler[source]

Bases: object

Class to provide several Image manipulation utilities.

addNoise(inputFile, outputFile, std=1.0, avg=0.0)[source]

Add Gaussian noise to an input image (or stack) to produce noisy images. Params:

inputFile: the filename of the input images outputFile: the filename of the output noisy images noiseStd: standard deviation for the Gaussian noise.

static applyTransform(inputFile, outputFile, transformMatrix, shape, fillValue=None, doWrap=False)[source]

Apply the transformation matrix over the input image and return the transformed image in a given shape. Parameters:

param inputFile

path location of the input image

param outputFile

path location of the output image

param transformMatrix

transformation matrix to be applied to the image. It should be a numpy array data type.

param shape

dimensions of the output image given as a tuple (yDim, xDim)

param fillValue

value with which the empty regions due to wrapping will be filled.

param doWrap

if True the empty regions will be filled with the wrapped values of the input image.

compareData(locationObj1, locationObj2, tolerance=0.0001)[source]

Compare if two locations have the same binary data.


Compute the average image either from filename or set. If inputSet is a filename, we will read the whole stack and compute the average from all images. If inputSet is a SetOfImages subclass, we will iterate and compute the average from all images.

computeThumbnail(inputFn, outputFn, scaleFactor=6, flipOnY=False, flipOnX=False)[source]

Compute a thumbnail of inputFn, save to ouptutFn. Optionally choose a scale factor eg scaleFactor=6 will make a thumbnail 6 times smaller.

convert(inputObj, outputObj, dataType=None, transform=None)[source]

Convert from one image to another. inputObj and outputObj can be: tuple, string, or Image subclass (see self._convertToLocation) transform: if not None, apply this transformation

convertStack(inputFn, outputFn, firstImg=None, lastImg=None, inFormat=None, outFormat=None)[source]

Convert an input stack file into another. It is possible to only use a subset of frames to be written in the

output stack.

If outFormat/inFomat=None then there will be inferred from extension.If firstFrame/lastFrame are not None, the output stack will be a subset of input stack. If it are none, the conversion is over the whole stack. If the input format is “.dm4” or “.img” only is allowed the conversion of the whole stack.

createCircularMask(radius, refImage, outputFile)[source]

Create a circular mask with the given radius (pixels) and with the same dimensions of the refImage. The radius should be less or equal dim(refImage)/2 The mask will be stored in ‘outputFile’

classmethod createEmptyImage(fnOut, xDim=1, yDim=1, zDim=1, nDim=1, dataType=None)[source]
classmethod existsLocation(locationObj)[source]

Return True if a given location exists. Location have the same meaning than in _convertToLocation.

classmethod fixXmippVolumeFileName(image)[source]

This method will add :mrc to .mrc volumes because for mrc format is not possible to distinguish between 3D volumes and 2D stacks.

classmethod getDimensions(locationObj)[source]

It will return a tuple with the images dimensions. The tuple will contains:

(x, y, z, n) where x, y, z are image dimensions (z=1 for 2D) and n is the number of elements if is a stack.

classmethod getSupportedDataType(inDataType, outputFilename)[source]

Returns the most similar data type supported by the output format

static getThumbnailFn(inputFn)[source]

Replace the extension in inputFn with thumb.png

classmethod getVolFileName(location)[source]
invert(inputObj, outputObj)[source]

invert the pixels. inputObj and outputObj can be: tuple, string, or Image subclass (see self._convertToLocation)

invertStack(inputFn, outputFn)[source]
classmethod isImageFile(imgFn)[source]

Check if imgFn has an image extension. The function is implemented in the Xmipp binding.

classmethod locationToXmipp(location)[source]

Convert an index and filename location to a string with @ as expected in Xmipp.


Create a new Image class from inputObj (inputObj can be tuple, str or Image subclass).

classmethod removeFileType(fileName)[source]
rotateVolume(inputFile, outputFile, transformation)[source]

Apply geometric transformations to images. You can shift, rotate and scale a group of images/volumes.

classmethod scale2DStack(inputFn, outputFn, scaleFactor=None, finalDimension=None)[source]

Scale a 2D images stack using PIL.

classmethod scaleFourier(inputFn, outputFn, scaleFactor)[source]

Scale an image by cropping in Fourier space.

classmethod scaleSplines(inputFn, outputFn, scaleFactor, finalDimension=None, forceVolume=False)[source]

Scale an image using splines.

truncateMask(inputFile, outputFile, newDim=None)[source]

Forces the values of a mask to be between 0 and 1. Additionally, the output mask can be scaled to a new dimension.


inputFile: the filename of the input either image or volume outputFile: the filename of the output either image or volume newDim: scale the output Mask to a new dimension if not None

write(image, outputObj)[source]

Write to disk an image from outputObj (outputObj can be tuple, str or Image subclass).