Skip to content
Merged
27 changes: 27 additions & 0 deletions src/python/py_imagebuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <memory>

#include <OpenImageIO/filesystem.h>
#include <OpenImageIO/platform.h>


Expand Down Expand Up @@ -250,6 +251,31 @@ ImageBuf_set_write_format(ImageBuf& self, const py::object& py_channelformats)



py::bytes
ImageBuf_repr_png(const ImageBuf& self)
{
ImageSpec original_spec = self.spec();

if (original_spec.width < 1 || original_spec.height < 1) {
return py::bytes();
}

std::vector<unsigned char> file_buffer; // bytes will go here
Filesystem::IOVecOutput file_vec(file_buffer); // I/O proxy object

std::unique_ptr<ImageOutput> out = ImageOutput::create("temp.png",
&file_vec);
out->open("temp.png", original_spec);
self.write(out.get());
out->close();

// Cast to const char* and return as python bytes
const char* char_ptr = reinterpret_cast<const char*>(file_buffer.data());
return py::bytes(char_ptr, file_buffer.size());
}



void
declare_imagebuf(py::module& m)
{
Expand Down Expand Up @@ -491,6 +517,7 @@ declare_imagebuf(py::module& m)
.def(
"deepdata", [](ImageBuf& self) { return *self.deepdata(); },
py::return_value_policy::reference_internal)
.def("_repr_png_", &ImageBuf_repr_png)

// FIXME -- do we want to provide pixel iterators?
;
Expand Down
Loading