allow reading and writing rgb-alpha images in Python (#2925)

This commit is contained in:
Adrià Arrufat 2024-03-06 10:17:36 +09:00 committed by GitHub
parent d45f534d68
commit d5909ed977
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 0 deletions

View File

@ -262,6 +262,7 @@ namespace dlib
else if (is_image<float>(src)) assign_image(dest, numpy_image<float>(src)); else if (is_image<float>(src)) assign_image(dest, numpy_image<float>(src));
else if (is_image<double>(src)) assign_image(dest, numpy_image<double>(src)); else if (is_image<double>(src)) assign_image(dest, numpy_image<double>(src));
else if (is_image<rgb_pixel>(src)) assign_image(dest, numpy_image<rgb_pixel>(src)); else if (is_image<rgb_pixel>(src)) assign_image(dest, numpy_image<rgb_pixel>(src));
else if (is_image<rgb_alpha_pixel>(src)) assign_image(dest, numpy_image<rgb_alpha_pixel>(src));
else DLIB_CASSERT(false, "Unsupported pixel type used in assign_image()."); else DLIB_CASSERT(false, "Unsupported pixel type used in assign_image().");
} }

View File

@ -20,6 +20,13 @@ numpy_image<rgb_pixel> load_rgb_image (const std::string &path)
return img; return img;
} }
numpy_image<rgb_pixel> load_rgb_alpha_image (const std::string &path)
{
numpy_image<rgb_alpha_pixel> img;
load_image(img, path);
return img;
}
numpy_image<unsigned char> load_grayscale_image (const std::string &path) numpy_image<unsigned char> load_grayscale_image (const std::string &path)
{ {
numpy_image<unsigned char> img; numpy_image<unsigned char> img;
@ -168,6 +175,10 @@ void bind_numpy_returns(py::module &m)
"Saves the given image to the specified path. Determines the file type from the file extension specified in the path", "Saves the given image to the specified path. Determines the file type from the file extension specified in the path",
py::arg("img"), py::arg("filename"), py::arg("quality") = 75 py::arg("img"), py::arg("filename"), py::arg("quality") = 75
); );
m.def("save_image", &save_image<rgb_alpha_pixel>,
"Saves the given image to the specified path. Determines the file type from the file extension specified in the path",
py::arg("img"), py::arg("filename"), py::arg("quality") = 75
);
m.def("save_image", &save_image<unsigned char>, m.def("save_image", &save_image<unsigned char>,
"Saves the given image to the specified path. Determines the file type from the file extension specified in the path", "Saves the given image to the specified path. Determines the file type from the file extension specified in the path",
py::arg("img"), py::arg("filename"), py::arg("quality") = 75 py::arg("img"), py::arg("filename"), py::arg("quality") = 75