Resizing and scaling image
$begingroup$
I'm trying to resize image bitmap in software development, in that my requirement is as following,
- Get the original height of image
- Get the original width of image
- Set a max threshold, output height and width should be below that threshold only
My requirement is,
- output height and width should be below set threshold
- output values should be maximum possible
- The ratio of final height and width should be same as original.
I am new to this community, so pardon me if anything is incorrect, any help will be appreciate.
image-processing programming
$endgroup$
add a comment |
$begingroup$
I'm trying to resize image bitmap in software development, in that my requirement is as following,
- Get the original height of image
- Get the original width of image
- Set a max threshold, output height and width should be below that threshold only
My requirement is,
- output height and width should be below set threshold
- output values should be maximum possible
- The ratio of final height and width should be same as original.
I am new to this community, so pardon me if anything is incorrect, any help will be appreciate.
image-processing programming
$endgroup$
1
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32
add a comment |
$begingroup$
I'm trying to resize image bitmap in software development, in that my requirement is as following,
- Get the original height of image
- Get the original width of image
- Set a max threshold, output height and width should be below that threshold only
My requirement is,
- output height and width should be below set threshold
- output values should be maximum possible
- The ratio of final height and width should be same as original.
I am new to this community, so pardon me if anything is incorrect, any help will be appreciate.
image-processing programming
$endgroup$
I'm trying to resize image bitmap in software development, in that my requirement is as following,
- Get the original height of image
- Get the original width of image
- Set a max threshold, output height and width should be below that threshold only
My requirement is,
- output height and width should be below set threshold
- output values should be maximum possible
- The ratio of final height and width should be same as original.
I am new to this community, so pardon me if anything is incorrect, any help will be appreciate.
image-processing programming
image-processing programming
asked Jan 18 at 11:24
Kiran MalviKiran Malvi
1033
1033
1
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32
add a comment |
1
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32
1
1
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32
add a comment |
3 Answers
3
active
oldest
votes
$begingroup$
The maths is quite simple
- Calculate the ratio between the width and height
- Set the width to be equal to the limit
- Calculate the height based on the ratio previously calculated
- If the height exceeds the limit, then repeat steps (2) and (3) using the height instead
For example, say you have an image with $text{width}=6$ and $text{height}=9$ and your threshold is $15$
The ratio between width and height is $2:3$, in other words, $$text{width}=frac 23times text{height}$$
If we increase the width to be equal to the limit, $15$, then our height becomes $$15times frac32 = 22.5$$
As this is above the limit of $15$, we instead set the height to be $15$ which gives us a width of $$15times frac 23 = 10$$
This is inside our limit so we have a new image with $text{width}=10$ and $text{height}=15$, which still obeys our $2:3$ ratio and is within the threshold.
$endgroup$
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
add a comment |
$begingroup$
So, essentially you have numbers $(w, h)$ and you want to scale them so that $max{(w_{text{new}}, h_{text{new}})}=M$, some maximum value. You simply have to calculate
$$
(w_{text{new}}, h_{text{new}}) = frac{M}{max{(w,h)}} (w,h)
$$
You can see that doing so replaces the bigger value by $M$ and scales the other by the same ratio. Done.
$endgroup$
add a comment |
$begingroup$
Let your image have size $w times h$, $t$ be the threshold, and $r=frac{w}{h}$ be the ratio. If $t>max(w,h)$ do nothing. Let $wgeq h$. You want to find a linear mapping $f : [0,w] rightarrow [0,t]$, that is $f(x) = ax + b, f(0) = 0, f(w) = t$, from the first condition $b= 0$, from the second $a=frac{t}{w}$. The new width is $w'= t$, since we want to preserve the ratio: $r=frac{w'}{h'}$, then $h' = frac{w'}{r}$. The mapping for $y$ can similarly be computed as: $g(y) = frac{h'y}{h}$. Your new image has dimensions $w' times h'$. Then for every point in the image $(x,y)$, the new transformed coordinates are $(x',y') = (frac{w'x}{w}, frac{h'y}{h})$. In case $w<h$, $h'=t, w' = rh'$.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3078121%2fresizing-and-scaling-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
The maths is quite simple
- Calculate the ratio between the width and height
- Set the width to be equal to the limit
- Calculate the height based on the ratio previously calculated
- If the height exceeds the limit, then repeat steps (2) and (3) using the height instead
For example, say you have an image with $text{width}=6$ and $text{height}=9$ and your threshold is $15$
The ratio between width and height is $2:3$, in other words, $$text{width}=frac 23times text{height}$$
If we increase the width to be equal to the limit, $15$, then our height becomes $$15times frac32 = 22.5$$
As this is above the limit of $15$, we instead set the height to be $15$ which gives us a width of $$15times frac 23 = 10$$
This is inside our limit so we have a new image with $text{width}=10$ and $text{height}=15$, which still obeys our $2:3$ ratio and is within the threshold.
$endgroup$
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
add a comment |
$begingroup$
The maths is quite simple
- Calculate the ratio between the width and height
- Set the width to be equal to the limit
- Calculate the height based on the ratio previously calculated
- If the height exceeds the limit, then repeat steps (2) and (3) using the height instead
For example, say you have an image with $text{width}=6$ and $text{height}=9$ and your threshold is $15$
The ratio between width and height is $2:3$, in other words, $$text{width}=frac 23times text{height}$$
If we increase the width to be equal to the limit, $15$, then our height becomes $$15times frac32 = 22.5$$
As this is above the limit of $15$, we instead set the height to be $15$ which gives us a width of $$15times frac 23 = 10$$
This is inside our limit so we have a new image with $text{width}=10$ and $text{height}=15$, which still obeys our $2:3$ ratio and is within the threshold.
$endgroup$
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
add a comment |
$begingroup$
The maths is quite simple
- Calculate the ratio between the width and height
- Set the width to be equal to the limit
- Calculate the height based on the ratio previously calculated
- If the height exceeds the limit, then repeat steps (2) and (3) using the height instead
For example, say you have an image with $text{width}=6$ and $text{height}=9$ and your threshold is $15$
The ratio between width and height is $2:3$, in other words, $$text{width}=frac 23times text{height}$$
If we increase the width to be equal to the limit, $15$, then our height becomes $$15times frac32 = 22.5$$
As this is above the limit of $15$, we instead set the height to be $15$ which gives us a width of $$15times frac 23 = 10$$
This is inside our limit so we have a new image with $text{width}=10$ and $text{height}=15$, which still obeys our $2:3$ ratio and is within the threshold.
$endgroup$
The maths is quite simple
- Calculate the ratio between the width and height
- Set the width to be equal to the limit
- Calculate the height based on the ratio previously calculated
- If the height exceeds the limit, then repeat steps (2) and (3) using the height instead
For example, say you have an image with $text{width}=6$ and $text{height}=9$ and your threshold is $15$
The ratio between width and height is $2:3$, in other words, $$text{width}=frac 23times text{height}$$
If we increase the width to be equal to the limit, $15$, then our height becomes $$15times frac32 = 22.5$$
As this is above the limit of $15$, we instead set the height to be $15$ which gives us a width of $$15times frac 23 = 10$$
This is inside our limit so we have a new image with $text{width}=10$ and $text{height}=15$, which still obeys our $2:3$ ratio and is within the threshold.
edited Jan 18 at 11:41
answered Jan 18 at 11:34
lioness99alioness99a
3,7492727
3,7492727
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
add a comment |
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
Thank you for the simplest explanation. Solution worked like a charm.
$endgroup$
– Kiran Malvi
Jan 18 at 12:01
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
$begingroup$
No worries. It has also just occurred to me that you could just take the larger of width and height in step 2 and skip the whole checking step as you know the other will be smaller than your threshold (unless you have different width and height thresholds, in which case you will need to check both)
$endgroup$
– lioness99a
Jan 18 at 12:04
add a comment |
$begingroup$
So, essentially you have numbers $(w, h)$ and you want to scale them so that $max{(w_{text{new}}, h_{text{new}})}=M$, some maximum value. You simply have to calculate
$$
(w_{text{new}}, h_{text{new}}) = frac{M}{max{(w,h)}} (w,h)
$$
You can see that doing so replaces the bigger value by $M$ and scales the other by the same ratio. Done.
$endgroup$
add a comment |
$begingroup$
So, essentially you have numbers $(w, h)$ and you want to scale them so that $max{(w_{text{new}}, h_{text{new}})}=M$, some maximum value. You simply have to calculate
$$
(w_{text{new}}, h_{text{new}}) = frac{M}{max{(w,h)}} (w,h)
$$
You can see that doing so replaces the bigger value by $M$ and scales the other by the same ratio. Done.
$endgroup$
add a comment |
$begingroup$
So, essentially you have numbers $(w, h)$ and you want to scale them so that $max{(w_{text{new}}, h_{text{new}})}=M$, some maximum value. You simply have to calculate
$$
(w_{text{new}}, h_{text{new}}) = frac{M}{max{(w,h)}} (w,h)
$$
You can see that doing so replaces the bigger value by $M$ and scales the other by the same ratio. Done.
$endgroup$
So, essentially you have numbers $(w, h)$ and you want to scale them so that $max{(w_{text{new}}, h_{text{new}})}=M$, some maximum value. You simply have to calculate
$$
(w_{text{new}}, h_{text{new}}) = frac{M}{max{(w,h)}} (w,h)
$$
You can see that doing so replaces the bigger value by $M$ and scales the other by the same ratio. Done.
answered Jan 18 at 11:33
Matti P.Matti P.
2,020414
2,020414
add a comment |
add a comment |
$begingroup$
Let your image have size $w times h$, $t$ be the threshold, and $r=frac{w}{h}$ be the ratio. If $t>max(w,h)$ do nothing. Let $wgeq h$. You want to find a linear mapping $f : [0,w] rightarrow [0,t]$, that is $f(x) = ax + b, f(0) = 0, f(w) = t$, from the first condition $b= 0$, from the second $a=frac{t}{w}$. The new width is $w'= t$, since we want to preserve the ratio: $r=frac{w'}{h'}$, then $h' = frac{w'}{r}$. The mapping for $y$ can similarly be computed as: $g(y) = frac{h'y}{h}$. Your new image has dimensions $w' times h'$. Then for every point in the image $(x,y)$, the new transformed coordinates are $(x',y') = (frac{w'x}{w}, frac{h'y}{h})$. In case $w<h$, $h'=t, w' = rh'$.
$endgroup$
add a comment |
$begingroup$
Let your image have size $w times h$, $t$ be the threshold, and $r=frac{w}{h}$ be the ratio. If $t>max(w,h)$ do nothing. Let $wgeq h$. You want to find a linear mapping $f : [0,w] rightarrow [0,t]$, that is $f(x) = ax + b, f(0) = 0, f(w) = t$, from the first condition $b= 0$, from the second $a=frac{t}{w}$. The new width is $w'= t$, since we want to preserve the ratio: $r=frac{w'}{h'}$, then $h' = frac{w'}{r}$. The mapping for $y$ can similarly be computed as: $g(y) = frac{h'y}{h}$. Your new image has dimensions $w' times h'$. Then for every point in the image $(x,y)$, the new transformed coordinates are $(x',y') = (frac{w'x}{w}, frac{h'y}{h})$. In case $w<h$, $h'=t, w' = rh'$.
$endgroup$
add a comment |
$begingroup$
Let your image have size $w times h$, $t$ be the threshold, and $r=frac{w}{h}$ be the ratio. If $t>max(w,h)$ do nothing. Let $wgeq h$. You want to find a linear mapping $f : [0,w] rightarrow [0,t]$, that is $f(x) = ax + b, f(0) = 0, f(w) = t$, from the first condition $b= 0$, from the second $a=frac{t}{w}$. The new width is $w'= t$, since we want to preserve the ratio: $r=frac{w'}{h'}$, then $h' = frac{w'}{r}$. The mapping for $y$ can similarly be computed as: $g(y) = frac{h'y}{h}$. Your new image has dimensions $w' times h'$. Then for every point in the image $(x,y)$, the new transformed coordinates are $(x',y') = (frac{w'x}{w}, frac{h'y}{h})$. In case $w<h$, $h'=t, w' = rh'$.
$endgroup$
Let your image have size $w times h$, $t$ be the threshold, and $r=frac{w}{h}$ be the ratio. If $t>max(w,h)$ do nothing. Let $wgeq h$. You want to find a linear mapping $f : [0,w] rightarrow [0,t]$, that is $f(x) = ax + b, f(0) = 0, f(w) = t$, from the first condition $b= 0$, from the second $a=frac{t}{w}$. The new width is $w'= t$, since we want to preserve the ratio: $r=frac{w'}{h'}$, then $h' = frac{w'}{r}$. The mapping for $y$ can similarly be computed as: $g(y) = frac{h'y}{h}$. Your new image has dimensions $w' times h'$. Then for every point in the image $(x,y)$, the new transformed coordinates are $(x',y') = (frac{w'x}{w}, frac{h'y}{h})$. In case $w<h$, $h'=t, w' = rh'$.
answered Jan 18 at 11:40
lightxbulblightxbulb
900211
900211
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3078121%2fresizing-and-scaling-image%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
$begingroup$
Hi Kiran, welcome to the MSE community! In my opinion, your question might be answered if posted in the Stackoverflow community if it's about software implementation rather than mathematical concepts?
$endgroup$
– Macrophage
Jan 18 at 11:28
$begingroup$
Hi @Macrophage, thank you for the quick reply, I do not want software implantation, I need mathematical approach and formula for that. So I guess, this question will be answered here in better way.
$endgroup$
– Kiran Malvi
Jan 18 at 11:32
$begingroup$
I'm voting to close this question as off-topic because it suits Stackoverflow better as it is about implementing something using software
$endgroup$
– lioness99a
Jan 18 at 11:32