Calculate new height from width and aspect ratio - formula

I have an image that is 1368*1026
I have calculated the aspect ratio by doing 1368 / 1026 = 0.8746355685131195
I want the image to be 500 pixels wide, how do I use the aspect ratio to calculate the height?

w=1368 and h=1026, so r=w/h. Let's say w2 and h2 is what you're looking for, with w2=500. We also have w2/h2=r, so, 500/h2=r. We can re-arrange and have h2=500/r. With that I get h2=375.
PS: 1368/1026 is 1,33... not 0.87....

Related

How to use pnmscale to scale the longer side of an image and still keep the ratio?

I'm writing a GNU Makefile to do some processing on images.
One task is to scale the image (.ppm format) by a SIZE parameter using the pnmscale command. The image should be scaled by the longer side without loosing the ratio and should be saved under .scaled .
I've read the man page of pnmscale but couldn't seem to find the right option.
I've tried the following:
pnmscale -pixels 100 example.ppm > example.scaled
When example.ppm has the size 200 x 100 pixels and I run the pnmscale command with the size of 100 pixels, example.scaled should have the size of 100 x 50 pixels. With my solution the image gets very small.
As the manpage of pnmscale states, the option pixels
specifies a maximum total number of output pixels. pnmscale scales the image down to that number of pixels. If the input image is already no more than that many pixels, pnmscale just copies it as output; pnmscale does not scale up with -pixels.
In other words, by specifying -pixels 100, you're actually scaling down your image to a maximum number of 100 pixels. What you're trying to achieve is to scale down your input image to a size of 100 x 50 pixels = 5000 pixels.
Looking again at the manpage of pnmscale yields the following:
pnmscale [{-xsize=cols | -width=cols | -xscale=factor}] [{-ysize=rows | -height=rows | -yscale=factor}] [pnmfile]
[...]
If you specify one dimension as a pixel size and don't specify the other dimension, pnmscale scales the unspecified dimension to preserve the aspect ratio.
In your case, using
pnmscale -xsize 100 example.ppm > example.scaled
should shrink your input image to a width of 100 pixels.

How would I calculate two given pixels to obtain its aspect ratio?

I've looked online on how to get the aspect ratio to write proper media queries for a website I'm trying to make but some of these numbers don't make sense.
What I'm trying to do is take two pixels, say, 667 x 325. I put those two numbers inside the website below, but result I'm getting is 667 : 325. I don't think that's correct, or is it?
https://aspectratiocalculator.com/
I've also tried looking for a mathematical formula to get these so I can just manually do these but there are so many out there that don't fit into the context of what I'm trying to obtain.
How can I get the aspect ratio of two given pixels?
Aspect ratio is width / height, e.g. 600 by 800 = 600/800 = 0.75.
The calculator is correct because the "Ratio" by conventional meaning of the word is 600:800 or 0.75:1 or 0.75 (as we programmers use it)

Calculate Max Width and Height

I need to calculate the width and height of X number of elements based on the overall width and height of the screen in pixels so I can write a function. The individual elements x and y should be the same for each element.
I know the height and width of the screen but the number of elements could vary.
For example, if I have 20 elements/buttons, I want them to take up the whole screen and likewise, if I have 10 elements/buttons, I want them to take up the whole screen.
Example: Screen is 628x1289 and I have 20 elements. How can I calculate the size of each element?
This is similar to web responsive but it's not for a web application so I need to figure out how to calculate it using the pixels.
I hope that makes sense but please let me know if it doesn't.
Steve
P.S. This seems relevant but I'm no math expert so have no idea exactly what they're saying...
https://en.wikipedia.org/wiki/Knapsack_problem
Seems like they already know the size of the objects which is the opposite to my needs. I have a space and want to know what x and y I need to set each object to fit the space.
There are too many free parameters. Make some restrictions to solve the task.
Example: Let's element proportions (width/height ratio) are equal to screen proportions
C = ScreenWidth / ScreenHeight
Now let's unknown element height is h, so width is w = C * h
So every row may contain ScreenWidth div (C * h), every column ScreenHeight div h, where div is integer division.
Now you can find solution (max h value) of inequality
numberX <= (ScreenWidth div (C * h)) * (ScreenHeight div h)
and get height and width of numberX elements filling the screen (last row might be incomplete)

How to calculate the intrinsic ratio for a given image dimension

I am somehow head blocked to figure this out but is there an easy mathematical way to determine the intrinsic ratio for a given image lets say e.g. w 580 h 650 to get to a ratio like 3/4, 4/3, 5/6, 16/9 etc pp. Best regard Ralf

Calculating Largest Possible Rectangle

I'm uploading some images to website. They're all sorts of different dimensions. How can I determine the largest 4:3 rectangle that I could get out of that particular image without rotating the image?
If the aspect ratio is less than 4:3, keep the original width and use a height of width*3/4. If the aspect ratio is greater than 4:3, keep the original height and use a width of height*4/3.
IIUC this problem is very similar to the common problem of maximizing an image in a given rectangular area.
Supposing your screen is W * H pixels (with W = 4 * H / 3) and that the image is dx * dy then you can stretch the image to maximize it inside the screen using a scale factor of
sf = min(W / dx, H / dy)
because W / dx would be a scale factor that makes the image the same width of your screen and H / dy would be the scale factor that make it the same height instead.
Taking the minimum of the two will ensure that the image can entirely fit and that no pixel will get outside the screen... taking the maximum instead will ensure that the screen will be completely covered with (part of) the image and could be useful if you are trying to get a wallpaper out of an image.
Once you have the scale factor the formulas needed for centering are easy:
x0 = (W - dx * sf) / 2
y0 = (H - dy * sf) / 2
and you simply need to draw the image scaled by sf starting at position (x0, y0).
First rotate your image so that its aspect ratio is at least 1. Now if your aspect ratio is larger than 4/3, keep the height and crop the width; if the aspect ratio is smaller, keep the width and crop the height.

Resources