Resizing and scaling image












0












$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.










share|cite|improve this question









$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
















0












$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.










share|cite|improve this question









$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














0












0








0





$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.










share|cite|improve this question









$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






share|cite|improve this question













share|cite|improve this question











share|cite|improve this question




share|cite|improve this question










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














  • 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










3 Answers
3






active

oldest

votes


















1












$begingroup$

The maths is quite simple




  1. Calculate the ratio between the width and height

  2. Set the width to be equal to the limit

  3. Calculate the height based on the ratio previously calculated

  4. 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.






share|cite|improve this answer











$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



















0












$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.






share|cite|improve this answer









$endgroup$





















    0












    $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'$.






    share|cite|improve this answer









    $endgroup$













      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
      });


      }
      });














      draft saved

      draft discarded


















      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









      1












      $begingroup$

      The maths is quite simple




      1. Calculate the ratio between the width and height

      2. Set the width to be equal to the limit

      3. Calculate the height based on the ratio previously calculated

      4. 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.






      share|cite|improve this answer











      $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
















      1












      $begingroup$

      The maths is quite simple




      1. Calculate the ratio between the width and height

      2. Set the width to be equal to the limit

      3. Calculate the height based on the ratio previously calculated

      4. 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.






      share|cite|improve this answer











      $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














      1












      1








      1





      $begingroup$

      The maths is quite simple




      1. Calculate the ratio between the width and height

      2. Set the width to be equal to the limit

      3. Calculate the height based on the ratio previously calculated

      4. 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.






      share|cite|improve this answer











      $endgroup$



      The maths is quite simple




      1. Calculate the ratio between the width and height

      2. Set the width to be equal to the limit

      3. Calculate the height based on the ratio previously calculated

      4. 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.







      share|cite|improve this answer














      share|cite|improve this answer



      share|cite|improve this answer








      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


















      • $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











      0












      $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.






      share|cite|improve this answer









      $endgroup$


















        0












        $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.






        share|cite|improve this answer









        $endgroup$
















          0












          0








          0





          $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.






          share|cite|improve this answer









          $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.







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Jan 18 at 11:33









          Matti P.Matti P.

          2,020414




          2,020414























              0












              $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'$.






              share|cite|improve this answer









              $endgroup$


















                0












                $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'$.






                share|cite|improve this answer









                $endgroup$
















                  0












                  0








                  0





                  $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'$.






                  share|cite|improve this answer









                  $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'$.







                  share|cite|improve this answer












                  share|cite|improve this answer



                  share|cite|improve this answer










                  answered Jan 18 at 11:40









                  lightxbulblightxbulb

                  900211




                  900211






























                      draft saved

                      draft discarded




















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Mario Kart Wii

                      Understanding the size os this class of aleatory events

                      Partial Derivative Guidance.