For simplicity, let's assume that the image origin is (0,0) (i.e., it's not a "crop" or "overscan" image). We can think about images that are wider than they are long ("landscape"), or longer than wide ("portrait").
I think you want something like the following, which uses the goal width for landscape and the goal height for portrait, and recomputes the proper size in the other direction:
goal_width = ...
goal_height = ...
buf = oiio.ImageBuf(file)
spec = buf.spec()
w = spec.width
h = spec.height
aspect = float(w) / float(h)
if aspect >= 1.0 :
# source image is landscape (or square)
resize_factor = float(goal_width) / w
goal_height = int(goal_height * resize_factor)
else :
# source image is portrait
resize_factor = float(goal_height) / h
goal_width = int(goal_width * resize_factor)
resized = oiio.ImageBuf(oiio.ImageSpec (goal_width, goal_height, spec.nchannels, spec.format))
oiio.ImageBufAlgo.resize(resized, buf)
resized.write(output)
That's off the top of my head, you should test it and adjust if I've made mistakes. But that's the gist.
Aside: note that when I create the resized buf, I used the number of channels and data format of the original file, which is a bit more robust than hard-coding it to 3 chans float as you did in the original.
Using python and OpenImageIO how can I resize an image given the maximum width and height for the output?
https://stackoverflow.com/questions/50914650/resize-image-using-python-openimageio-maintain-aspect-ratio <https://stackoverflow.com/questions/50914650/resize-image-using-python-openimageio-maintain-aspect-ratio>
I've been messing around with this for a few days now and i'm still completely without a solution.
_______________________________________________
Oiio-dev mailing list
http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org
--
Larry Gritz
***@larrygritz.com