Hi Steven,

Here's the SO question:

https://stackoverflow.com/questions/73307400/extend-inner-array-in-3d-numpy-array

My initial code was this:

copy_channel_indices = [1, 3]
out_data = np.empty((height, width, 5), dest_data.dtype)
for y in range(height):
    for x in range(width):
        out_data[y][x][:3] = dest_data[y][x]
        out_data[y][x][3] = src_data[y][x][1]
        out_data[y][x][4] = src_data[y][x][3]

I got a 100x speed up by changing this code to this:

out_data = np.concatenate((dest_data, src_data[:, :, copy_channel_indices]), 
axis=2)


Thanks
Hugh

On 18/08/2022 09:05, Steven Parker wrote:
Hi Hugh,

Perhaps you can share how you achieved the speedup from a tip on SO? I'm doing something similar to you for a project, and hitting similar performance issues.

    I was initially doing the concatenating by hand in Python, which
    was horrendously slow (10s for a UHD image), but was pointed in
    the right direction by SO on that, which reduced it to <1% of the
    time.



On Mon, Aug 15, 2022 at 12:54 PM Hugh Macdonald <hugh.macdon...@scanlinevfx.com <mailto:hugh.macdon...@scanlinevfx.com>> wrote:

    Thanks Larry,

    It's good to know that I wasn't missing anything OIIO-specific in
    there. I was initially doing the concatenating by hand in Python,
    which was horrendously slow (10s for a UHD image), but was pointed
    in the right direction by SO on that, which reduced it to <1% of
    the time.

    That's helpful to see how I'd do it with ImageBuf. I think I'll
    probably stick with what I've got for now, though, if there's
    nothing majorly wrong with it, as it certainly works.

    Thanks
    Hugh

    On 15/08/2022 19:36, Larry Gritz wrote:
    Yeah, that looks like it should work.

    If you really needed it done with ImageBuf specifically for some
    reason, I think it might look like

    # read old images
    src_img = ImageBuf(src_image_file)
    dst_img = ImageBuf(dst_image_file)

    # make spec to describe the new image and allocate it
    spec = src_img.spec.copy()
    spec.nchannels = spec.nchannels + 1
    spec.channel_names.append("new_layer.new_channel")
    new_img = ImageBuf(spec)

    # copy the right parts to the right places
    # ... the first three channels get pasted from dst
    ImageBufAlgo.paste(new_img, 0, 0, 0, 0, dst_img)
    # ... the last channel gets pasted from src.G
    roi = src.roi.copy()
    roi.chbegin = 1
    roi.chend = 2
    ImageBufAlgo.paste(new_img, 0, 0, 0, spec.nchannels - 1, src_img,
    roi)

    # write results
    new_img.write(dest_image_file)


    (I haven't tried this, it's off the top of my head, but it's
    probably close to correct.)

    I don't necessarily think this is any better or more efficient
    than what you did, I just thought it might be helpful to compare
    how it would be done with ImageBuf alone, no ImageInput/ImageOutput.

    -- lg


    On Aug 15, 2022, at 9:14 AM, Hugh Macdonald
    <hugh.macdon...@scanlinevfx.com
    <mailto:hugh.macdon...@scanlinevfx.com>> wrote:

    Hi Larry,

    (b) is the closest to what I'm trying to do.

    Since sending my initial email, I'm now doing this. This keeps
    RGB in dest_img, and copies G from src_img into
    new_layer.new_channel

    src_img = oiio.ImageInput.open(src_image_file)
    dest_img = oiio.ImageInput.open(dest_image_file)

    src_data = src_img.read_image()
    dest_data = dest_img.read_image()

    src_spec = src_img.spec()
    dest_spec = dest_img.spec()

    src_img.close()
    dest_img.close()

    width = dest_spec.width
    height = dest_spec.height

    new_channels = list(dest_spec.channelnames)
    new_channels.append("new_layer.new_channel")
    copy_channel_indices = [1]

    out_data = np.concatenate((dest_data, src_data[:, :, 
copy_channel_indices]), axis=2)

    out_img = oiio.ImageOutput.create(dest_image_file)
    out_spec = oiio.ImageSpec(width, height, len(new_channels), "float32")
    out_spec.channelnames = tuple(new_channels)
    out_img.open(dest_image_file, out_spec)

    out_img.write_image(out_data)
    out_img.close()

    Thanks
    Hugh

    On 14/08/2022 23:02, Larry Gritz wrote:
    Hi, Hugh. Can you clarify whether you want to

    (a) make a new RGB image R from B but G and B from A?
    (b) make a new image with an additional channel, yielding {R,
    G, B, and New}, where R,G,B came from A and New is the R
    channel from B?

    ?


    On Aug 8, 2022, at 8:14 AM, Hugh Macdonald
    <hugh.macdon...@scanlinevfx.com
    <mailto:hugh.macdon...@scanlinevfx.com>> wrote:

    Hey all,


    I'm not overly familiar with the OIIO python API, so apologies
    if this is quite a simple question! I can't find any
    information online on how to do this with the Python API...

    I've got 2 EXRs, both with RGB channels, and I'd like to take
    the R channel from one of them and put it into another channel
    in the other.

    I've been looking at doing a 2-step process. The first step
    being to rename the channels in one ImageBuf, and to remove
    the unneeded channels, and the second being to use
    ImageBufAlgo.channel_append() to combine the two images together.


    This feels like it is unnecessarily 2 steps. Could someone
    point me at some examples for how to do this in a single step?


    I'm sure that this is pretty simple, but all the examples that
    I can find are for how to do this kind of thing using
    oiiotool, rather than doing it directly using the API.


    Thanks

-- Hugh

    _______________________________________________
    Oiio-dev mailing list
    Oiio-dev@lists.openimageio.org
    <mailto:Oiio-dev@lists.openimageio.org>
    http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org


    --
    Larry Gritz
    l...@larrygritz.com <mailto:l...@larrygritz.com>






    _______________________________________________
    Oiio-dev mailing list
    Oiio-dev@lists.openimageio.org  <mailto:Oiio-dev@lists.openimageio.org>
    http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org

-- Hugh Macdonald
    Product Owner, Volumetric Capture
    Eyeline Studios
    Ext: 4165
    www.scanlinevfx.com  <http://www.scanlinevfx.com/>
    _______________________________________________
    Oiio-dev mailing list
    Oiio-dev@lists.openimageio.org
    <mailto:Oiio-dev@lists.openimageio.org>
    http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org

    --
    Larry Gritz
    l...@larrygritz.com <mailto:l...@larrygritz.com>






    _______________________________________________
    Oiio-dev mailing list
    Oiio-dev@lists.openimageio.org  <mailto:Oiio-dev@lists.openimageio.org>
    http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org

-- Hugh Macdonald
    Product Owner, Volumetric Capture
    Eyeline Studios
    Ext: 4165
    www.scanlinevfx.com  <http://www.scanlinevfx.com>

    _______________________________________________
    Oiio-dev mailing list
    Oiio-dev@lists.openimageio.org <mailto:Oiio-dev@lists.openimageio.org>
    http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org


_______________________________________________
Oiio-dev mailing list
Oiio-dev@lists.openimageio.org
http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org

--
Hugh Macdonald
Product Owner, Volumetric Capture
Eyeline Studios
Ext: 4165
www.scanlinevfx.com

_______________________________________________
Oiio-dev mailing list
Oiio-dev@lists.openimageio.org
http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org

Reply via email to