# pydicom.pixel_data_handlers.numpy_handler

Use the numpy package to convert supported pixel data to a `numpy.ndarray`.

Supported transfer syntaxes

- 1.2.840.10008.1.2 : Implicit VR Little Endian
- 1.2.840.10008.1.2.1 : Explicit VR Little Endian
- 1.2.840.10008.1.2.1.99 : Deflated Explicit VR Little Endian
- 1.2.840.10008.1.2.2 : Explicit VR Big Endian

Supported data

The numpy handler supports the conversion of data in the (7FE0,0008) Float Pixel Data, (7FE0,0009) Double Float Pixel Data and (7FE0,0010) Pixel Data elements to a `ndarray` provided the related Image Pixel, Floating Point Image Pixel or Double Floating Point Image Pixel module elements have values given in the table below.

Element | Supported values |
--- | --- | ---
Tag | Keyword | Type
(0028,0002) | SamplesPerPixel | 1 | N | Required
(0028,0004) | PhotometricInterpretation | 1 | MONOCHROME1, MONOCHROME2, RGB, YBR_FULL, YBR_FULL_422 | Required
(0028,0006) | PlanarConfiguration | 1C | 0, 1 | Optional
(0028,0008) | NumberOfFrames | 1C | N | Optional
(0028,0010) | Rows | 1 | N | Required
(0028,0011) | Columns | 1 | N | Required
(0028,0100) | BitsAllocated | 1 | 1, 8, 16, 32, 64 | Required
(0028,0103) | PixelRepresentation | 1 | 0, 1 | Required

Functions

`get_pixeldata`(ds[, read_only]) | Return a `numpy.ndarray` of the pixel data.
`is_available`() | Return `True` if the handler has its dependencies met.
`needs_to_convert_to_RGB`(ds) | Return `True` if the Pixel Data should to be converted from YCbCr to RGB.
`pack_bits`(arr) | Pack a binary `numpy.ndarray` for use with Pixel Data.
`should_change_PhotometricInterpretation_to_RGB`(ds) | Return `True` if the Photometric Interpretation should be changed to RGB.
`supports_transfer_syntax`(transfer_syntax) | Return `True` if the handler supports the transfer_syntax.
`unpack_bits`(bytestream) | Unpack bit packed Pixel Data or Overlay Data into a `numpy.ndarray`.

`pydicom.pixel_data_handlers.numpy_handler.``get_pixeldata`(ds, read_only=False)

  Return a `numpy.ndarray` of the pixel data.

  Changed in version 1.4:

  - Added support for uncompressed pixel data with a Photometric Interpretation of `YBR_FULL_422`.

  - Added support for Float Pixel Data and Double Float Pixel Data

  Parameters:

  - ds (Dataset) – The `Dataset` containing an Image Pixel, Floating Point Image Pixel or Double Floating Point Image Pixel module and the Pixel Data, Float Pixel Data or Double Float Pixel Data to be converted. If (0028,0004) Photometric Interpretation is ‘YBR_FULL_422’ then the pixel data will be resampled to 3 channel data as per Part 3, Annex C.7.6.3.1.2 of the DICOM Standard.
  - read_only (bool, optional) – If `False` (default) then returns a writeable array that no longer uses the original memory. If `True` and the value of (0028,0100) Bits Allocated > 1 then returns a read-only array that uses the original memory buffer of the pixel data. If Bits Allocated = 1 then always returns a writeable array.

  Returns: | The contents of (7FE0,0010) Pixel Data, (7FE0,0008) Float Pixel Data or (7FE0,0009) Double Float Pixel Data as a 1D array.
  Return type: | np.ndarray

  Raises:

  - `AttributeError` – If ds is missing a required element.
  - `NotImplementedError` – If ds contains pixel data in an unsupported format.
  - `ValueError` – If the actual length of the pixel data doesn’t match the expected length.

`pydicom.pixel_data_handlers.numpy_handler.``is_available`()

  Return `True` if the handler has its dependencies met.

`pydicom.pixel_data_handlers.numpy_handler.``needs_to_convert_to_RGB`(ds)

  Return `True` if the Pixel Data should to be converted from YCbCr to RGB.

  This affects JPEG transfer syntaxes.

`pydicom.pixel_data_handlers.numpy_handler.``pack_bits`(arr)

  Pack a binary `numpy.ndarray` for use with Pixel Data.

  New in version 1.2.

  Should be used in conjunction with (0028,0100) Bits Allocated = 1.

  Parameters: | arr (numpy.ndarray) – The `numpy.ndarray` containing 1-bit data as ints. arr must only contain integer values of 0 and 1 and must have an ‘uint’ or ‘int’ `numpy.dtype`. For the sake of efficiency it’s recommended that the length of arr be a multiple of 8 (i.e. that any empty bit-padding to round out the byte has already been added).
  Returns: | The bit packed data.
  Return type: | bytes
  Raises: | `ValueError` – If arr contains anything other than 0 or 1.

  References

  DICOM Standard, Part 5, Section 8.1.1 and Annex D

`pydicom.pixel_data_handlers.numpy_handler.``should_change_PhotometricInterpretation_to_RGB`(ds)

  Return `True` if the Photometric Interpretation should be changed to RGB.

  This affects JPEG transfer syntaxes.

`pydicom.pixel_data_handlers.numpy_handler.``supports_transfer_syntax`(transfer_syntax)

  Return `True` if the handler supports the transfer_syntax.

  Parameters: | transfer_syntax (uid.UID) – The Transfer Syntax UID of the Pixel Data that is to be used with the handler.

`pydicom.pixel_data_handlers.numpy_handler.``unpack_bits`(bytestream)

  Unpack bit packed Pixel Data or Overlay Data into a `numpy.ndarray`.

  Suitable for use when (0028,0011) Bits Allocated or (60xx,0100) Overlay Bits Allocated is 1.

  Parameters: | bytestream (bytes) – The bit packed pixel data.
  Returns: | The unpacked Pixel Data as a 1D array.
  Return type: | numpy.ndarray

  References

  DICOM Standard, Part 5, Section 8.1.1 and Annex D