pythonmayaopenimageio

Is there a way to extract pixel color information from an image with OpenImageIO's python bindings


I want to ask the more experienced people how to get the RGB values of a pixel in an image using oiio's python bindings.

I have only just begun using oiio and am unfamiliar with the library as well as image manipulation on code level.

I poked around the documentation and I don't quite understand how the parameters work. It doesn't seem to work the same as python and I'm having a hard time trying to figure out, as I don't know C.

A) What command to even use to get pixel information (seems like get_pixel could work)

and

B) How to get it to work. I'm not understanding the parameters requirements exactly.

Edit:

I'm trying to convert the C example of visiting all pixels to get an average color in the documentation into something pythonic, but am getting nowhere.

Would appreciate any help, thank you.

Edit: adding the code

buffer = oiio.ImageBuf('image.tif')

array = buffer.read(0, 0, True)

print buffer.get_pixels(array)

the error message I get is:

# Error: Python argument types in
#     ImageBuf.get_pixels(ImageBuf, bool)
# did not match C++ signature:
#     get_pixels(class OpenImageIO::v1_5::ImageBuf, enum OpenImageIO::v1_5::TypeDesc::BASETYPE)
#     get_pixels(class OpenImageIO::v1_5::ImageBuf, enum OpenImageIO::v1_5::TypeDesc::BASETYPE, struct OpenImageIO::v1_5::ROI)
#     get_pixels(class OpenImageIO::v1_5::ImageBuf, struct OpenImageIO::v1_5::TypeDesc)
#     get_pixels(class OpenImageIO::v1_5::ImageBuf, struct OpenImageIO::v1_5::TypeDesc, struct OpenImageIO::v1_5::ROI)

Solution

  • OpenImageIO has several classes for dealing with images, with different levels of abstraction. Assuming that you are interested in the ImageBuf class, I think the simplest way to access individual pixels from Python (with OpenImageIO 2.x) would look like this:

    import OpenImageIO as oiio
    buf = ImageBuf ("foo.jpg")
    p = buf.getpixel (50, 50)   # x, y
    print (p)
    

    p will be a numpy array, so the this will produce output like

    (0.01148223876953125, 0.0030574798583984375, 0.0180511474609375)