When scaling an image, how can I determine the correct w / h so that I don't get decimals

Suppose I have a non-quadratic image.

If I increase the width and recalculate the height according to the increased width (ratio), sometimes I get xxx.5 (decimal places) for the width

example: width = 4, height = 2 I increase the width with a factor of 1.25, I get: width = 5 Then the height will be: heigth = 2.5

How to determine the closest image format with integers on both sides? (if possible more)

thanks

+4
source share
2 answers

reduce the fraction to the lower terms, and then multiply by integers. You reduce a/b to the lowest values ​​by dividing them by the total gcd. If d = gcd(a,b) , then (a/d) / (b/d) is in the lower terms. Now, if you want the next big whole fraction with the same ration, then multiply the numerator and denominator by d+1 . Thus,

(d+1) * (a/d) is the numerator, and (d+1) * (b/d) is the denominator.

+3
source

Let g be http://en.wikipedia.org/wiki/Greatest_common_divisor w and h. The next largest image has a width of w + w / g and a height of h + h / g. You can compute g using http://en.wikipedia.org/wiki/Euclidean_algorithm .

+4
source

Source: https://habr.com/ru/post/1303354/


All Articles