scallop dome pyformex logo

Previous topic

29. imageViewer — A general image viewer

Next topic

31. appMenu — Menu with pyFormex apps.

[FSF Associate Member]

Valid XHTML 1.0 Transitional

30. imagearray — Convert bitmap images into numpy arrays.

This module contains functions to convert bitmap images into numpy arrays and vice versa.

This code was based on ideas found on the PyQwt mailing list.

Classes defined in module imagearray

Functions defined in module imagearray

imagearray.resizeImage(image, w=0, h=0)

Load and optionally resize an image.

Parameters:

  • image: a QImage, or any data that can be converted to a QImage, e.g. the name of a raster image file.
  • w, h: requested size in pixels of the image. A value <= 0 will be replaced with the corresponding actual size of the image.

Returns a QImage with the requested size.

imagearray.image2numpy(image, resize=(0, 0), order='RGBA', flip=True, indexed=None, expand=None)

Transform an image to a Numpy array.

Parameters:

  • image: a QImage or any data that can be converted to a QImage, e.g. the name of an image file, in any of the formats supported by Qt. The image can be a full color image or an indexed type. Only 32bit and 8bit images are currently supported.
  • resize: a tuple of two integers (width,height). Positive value will force the image to be resized to this value.
  • order: string with a permutation of the characters ‘RGBA’, defining the order in which the colors are returned. Default is RGBA, so that result[...,0] gives the red component. Note however that QImage stores in ARGB order. You may also specify a subset of the ‘RGBA’ characters, in which case you will only get some of the color components. An often used value is ‘RGB’ to get the colors without the alpha value.
  • flip: boolean: if True, the image scanlines are flipped upside down. This is practical because image files are usually stored in top down order, while OpenGL uses an upwards positive direction, requiring a flip to show the image upright.
  • indexed: True, False or None.
    • If True, the result will be an indexed image where each pixel color is an index into a color table. Non-indexed image data will be converted.
    • If False, the result will be a full color array specifying the color of each pixel. Indexed images will be converted.
    • If None (default), no conversion is done and the resulting data are dependent on the image format. In all cases both a color and a colortable will be returned, but the latter will be None for non-indexed images.
  • expand: deprecated, retained for compatibility

Returns:

  • if indexed is False: an int8 array with shape (height,width,4), holding the 4 components of the color of each pixel. Order of the components is as specified by the order argument. Indexed image formats will be expanded to a full color array.
  • if indexed is True: a tuple (colors,colortable) where colors is an (height,width) shaped int array of indices into the colortable, which is an int8 array with shape (ncolors,4).
  • if indexed is None (default), a tuple (colors,colortable) is returned, the type of which depend on the original image format:
    • for indexed formats, colors is an int (height,width) array of indices into the colortable, which is an int8 array with shape (ncolors,4).
    • for non-indexed formats, colors is a full (height,width,4) array and colortable is None.
imagearray.gray2qimage(gray)

Convert the 2D numpy array gray into a 8-bit QImage with a gray colormap. The first dimension represents the vertical image axis.

imagearray.rgb2qimage(rgb)

Convert the 3D numpy array into a 32-bit QImage.

Parameters:

  • rgb : (height,width,nchannels) integer array specifying the pixels of an image. There can be 3 (RGB) or 4 (RGBA) channels.

Returns a QImage with size (height,width) in the format RGB32 (3channel) or ARGB32 (4channel).

imagearray.image2glcolor(image, resize=(0, 0))

Convert a bitmap image to corresponding OpenGL colors.

Parameters:

  • image: a QImage or any data that can be converted to a QImage, e.g. the name of an image file, in any of the formats supported by Qt. The image can be a full color image or an indexed type. Only 32bit and 8bit images are currently supported.
  • resize: a tuple of two integers (width,height). Positive value will force the image to be resized to this value.

Returns a (w,h,3) shaped array of float values in the range 0.0 to 1.0, containing the OpenGL colors corresponding to the image RGB colors. By default the image is flipped upside-down because the vertical OpenGL axis points upwards, while bitmap images are stored downwards.

imagearray.loadImage_dicom(filename)

Load a DICOM image into a numpy array.

This function uses the python-dicom module to load a DICOM image into a numpy array. See also loadImage_gdcm() for an equivalent using python-gdcm.

Parameters:

  • file: the name of a DICOM image file
Returns a 3D array with the pixel data of all the images. The first
axis is the z value, the last the x.

As a side effect, this function sets the global variable _dicom_spacing to a (3,) array with the pixel/slice spacing factors, in order (x,y,z).

imagearray.loadImage_gdcm(filename)

Load a DICOM image into a numpy array.

This function uses the python-gdcm module to load a DICOM image into a numpy array. See also loadImage_dicom() for an equivalent using python-dicom.

Parameters:

  • file: the name of a DICOM image file
Returns a 3D array with the pixel data of all the images. The first
axis is the z value, the last the x.

As a side effect, this function sets the global variable _dicom_spacing to a (3,) array with the pixel/slice spacing factors, in order (x,y,z).

imagearray.readDicom(filename)

Load a DICOM image into a numpy array.

This function uses the python-gdcm module to load a DICOM image into a numpy array. See also loadImage_dicom() for an equivalent using python-dicom.

Parameters:

  • file: the name of a DICOM image file
Returns a 3D array with the pixel data of all the images. The first
axis is the z value, the last the x.

As a side effect, this function sets the global variable _dicom_spacing to a (3,) array with the pixel/slice spacing factors, in order (x,y,z).

imagearray.dicom2numpy(files)

Read a set of DICOM image files.

Parameters:

  • files: a list of file names of dicom images of the same size, or a directory containing such images. In the latter case, all the DICOM images in the directory will be read.

Returns a tuple of:

  • pixar: a 3D array with the pixel data of all the images. The first axis is the z value, the last the x.
  • scale: a (3,) array with the scaling factors, in order (x,y,z).