| 41 | } |
| 42 | |
| 43 | void InitPixelType(py::module_& m) |
| 44 | { |
| 45 | py::class_<PixelType>(m, "PixelType", |
| 46 | R"(Describes the per-pixel layout of an :py:class:`Image`. |
| 47 | |
| 48 | A ``PixelType`` combines a component scalar type (``uint8``, ``int8``, |
| 49 | ``uint16``, ``int16``, ``uint32``, ``int32``, ``float32``, ``float64``) |
| 50 | with a component count (1 for scalar pixels, 3 for RGB, 4 for RGBA, etc.). |
| 51 | |
| 52 | ``PixelType`` instances are usually constructed indirectly via |
| 53 | :py:func:`make_pixel_type` or by passing a NumPy dtype to |
| 54 | :py:meth:`Image.initialize`. |
| 55 | )") |
| 56 | .def("__repr__", [](const PixelType& pt) { |
| 57 | std::string repr = "<PixelType: "; |
| 58 | |
| 59 | const auto pixelTypeStr = pt.GetPixelTypeAsString(); |
| 60 | |
| 61 | if (!pixelTypeStr.empty()) |
| 62 | repr += pixelTypeStr + " ("; |
| 63 | |
| 64 | if (const auto numComponents = pt.GetNumberOfComponents(); numComponents > 1) |
| 65 | repr += std::to_string(numComponents) + " * "; |
| 66 | |
| 67 | repr += pt.GetComponentTypeAsString(); |
| 68 | |
| 69 | if (!pixelTypeStr.empty()) |
| 70 | repr += ")"; |
| 71 | |
| 72 | repr += ">"; |
| 73 | |
| 74 | return repr; |
| 75 | }); |
| 76 | |
| 77 | m.def("make_pixel_type", |
| 78 | [](const py::object& dtype, size_t components) { return MakePixelType(dtype, components); }, |
| 79 | py::arg("dtype"), |
| 80 | py::arg("components") = 1, |
| 81 | R"(Construct a :py:class:`PixelType` from a NumPy dtype. |
| 82 | |
| 83 | Args: |
| 84 | dtype: NumPy dtype or any value accepted by ``numpy.dtype()`` |
| 85 | (string names like ``"float32"``, scalar types like |
| 86 | ``numpy.float32``, or an existing dtype object). |
| 87 | components: Number of components per pixel. Defaults to 1. |
| 88 | |
| 89 | Returns: |
| 90 | A :py:class:`PixelType` matching the requested layout. |
| 91 | |
| 92 | Raises: |
| 93 | mitk.Exception: If the dtype is not supported by MITK. |
| 94 | |
| 95 | Examples: |
| 96 | >>> pt = mitk.make_pixel_type("float32") |
| 97 | >>> rgba = mitk.make_pixel_type("uint8", components=4) |
| 98 | )"); |
| 99 | } |
no test coverage detected