Search code examples
imageqtluawindowtorch

Preserving aspect ratio when using torch's image.display


I have the following very simple script written in lua. I am running it with qlua.

require "image" input_image = image.load(arg[1]) image.display{image = input_image}

If the image is large the qt window simply takes the whole screen, which also stretches the image to fit the screen.

I can't figure out a way to keep this from happening.

Thanks!


Solution

  • If the image is large, resize it down to what you can configure as "Max height/Max width", while preserving the aspect ratio.

    Sample code:

    maxSize = 480
    -- find the smaller dimension, and resize it to maxSize (while keeping aspect ratio)
    local iW = input:size(3)
    local iH = input:size(2)
    if iW < iH then
       input = image.scale(input, maxSize, maxSize * iH / iW)
    else
       input = image.scale(input, maxSize * iW / iH, maxSize)
    end