Discussion:
[Oiio-dev] Resize image with aspect ratio python
John Martini
2018-06-19 12:28:14 UTC
Permalink
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

I've been messing around with this for a few days now and i'm still
completely without a solution.
Larry Gritz
2018-06-19 17:09:03 UTC
Permalink
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
Larry Gritz
2018-06-19 17:15:58 UTC
Permalink
By the way, I apologize for the fact that resize() doesn't do this for you automatically. In retrospect, it seems like there's some missing logic that would be useful for people. I'll add it for future releases.
Post by Larry Gritz
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").
goal_width = ...
goal_height = ...
buf = oiio.ImageBuf(file)
spec = buf.spec()
w = spec.width
h = spec.height
aspect = float(w) / float(h)
# source image is landscape (or square)
resize_factor = float(goal_width) / w
goal_height = int(goal_height * resize_factor)
# 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
_______________________________________________
Oiio-dev mailing list
http://lists.openimageio.org/listinfo.cgi/oiio-dev-openimageio.org
--
Larry Gritz
***@larrygritz.com

Continue reading on narkive:
Search results for '[Oiio-dev] Resize image with aspect ratio python' (Questions and Answers)
18
replies
can u name ALL the 6 letter words you can think of?
started 2007-05-10 08:53:14 UTC
words & wordplay
Loading...