Skip to content
Snippets Groups Projects
Commit 5be402cc authored by Christof Kaufmann's avatar Christof Kaufmann
Browse files

python: Use `std::optional` to accept `None` for empty images

parent b783ee6f
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,7 @@ namespace py = pybind11;
#include <string>
#include <cstdint>
#include <memory>
#include <optional>
imfu::Type getTypeFromNumpyFormat(std::string const& fmt, int channels = 1) {
......@@ -332,11 +333,13 @@ PYBIND11_MODULE(_imagefusion, module) {
py::class_<imfu::Image, imfu::ConstImage> image(module, "Image", py::buffer_protocol());
image.def(py::init());
image.def(py::init([](py::none /*empty*/) { return new imfu::Image(); }));
image.def(py::init<std::string const&, std::vector<int>, imfu::Rectangle, bool, bool, bool>());
image.def(py::init([](py::buffer b) -> imfu::Image* { // converts from np.array to imfu::Image
image.def(py::init([](std::optional<py::buffer> b) -> imfu::Image* { // converts from np.array to imfu::Image
if (!b.has_value()) // allows Image(numpy_img), where numpy_img may be None
return new imfu::Image();
/* Request a buffer descriptor from Python */
auto info = std::make_unique<py::buffer_info>(b.request());
auto info = std::make_unique<py::buffer_info>(b->request());
if (info->ndim != 2 && info->ndim != 3)
throw std::runtime_error("Incompatible buffer dimension: " + std::to_string(info->ndim) + "!");
......@@ -393,7 +396,8 @@ PYBIND11_MODULE(_imagefusion, module) {
}
return img;
}
}));
}), py::arg("b") = py::none()
);
image.def_buffer([](imfu::Image& img) -> py::buffer_info { // converts from imfu::Image to np.array
return py::buffer_info(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment