1
$\begingroup$

I cannot come up with idiomatic Mathematica code to solve this simple problem:

  1. take: image and desired aspect ratio
    1. get image dimensions
    2. upsize the dimensions to desired aspect ratio
  2. return{{left, right}, {bottom, up}} for ImagePad

https://www.instagram.com/p/CMxh05OH4Ss/ (⬅️ needed the answer for these)

$\endgroup$
2
  • 2
    $\begingroup$ for (1) you can use ImageAspectRatio $\endgroup$
    – kglr
    Mar 23 at 22:48
  • $\begingroup$ @kglr I know but I am struggling to get the second argument to ImagePad calculated o__O $\endgroup$
    – Cetin Sert
    Mar 23 at 23:02
2
$\begingroup$

I have come up with this:

pad // ClearAll
pad[i_Image, r_] := pad[ImageDimensions@i, r]
pad[d_, r_] := Module[{n, w, h},
  n = Max[d]/Max[r]*Min[r];
  {w, h} = Clip[(n - d)/2, {0, \[Infinity]}];
  {w, h} {{1, 1}, {1, 1}} // Round
  ]

To answer the request for an example photo:

i = "https://i.stack.imgur.com/3GmI2.png" // Import
r = {4, 5} (* {w, h} = instagram portrait aspect ratio *)
ImagePad[i, i~pad~r, "Fixed"]

Before padding enter image description here

After padding enter image description here

$\endgroup$
1
  • 2
    $\begingroup$ do you mind adding an example input(s) showing the connection to image aspect ratio? $\endgroup$
    – kglr
    Mar 24 at 0:07
2
$\begingroup$
ClearAll[pad2]
pad2[i_Image, ar_] := Module[{d = ImageDimensions @ i},
  Round[{#, #}/2] & /@ Ramp[Max[d] Min[ar, 1/ar] - d]]

ImagePad[i, pad2[i, 4/5], "Fixed"]

enter image description here

ImagePad[#, pad2[#, 4/5], "Fixed"] & @ ImageRotate[i, Pi/2]

enter image description here

$\endgroup$
2
  • 1
    $\begingroup$ This does feel more idiomatic! Sweet use of Ramp and 4/5. Obviously, I was not posting the above receipt to Instagram but (helping post) a watercolor painting by my wife 💖: instagram.com/p/CMxh05OH4Ss $\endgroup$
    – Cetin Sert
    Mar 24 at 11:42
  • 2
    $\begingroup$ @CetinSert, wonderful pictures! $\endgroup$
    – kglr
    Mar 24 at 11:57
0
$\begingroup$

Would the following do?

pad[im_Image, r : {w_, h_}] := 
 Module[{idim = ImageDimensions[im], 
   ratim = Divide @@ ImageDimensions[im], rat = Divide @@ r, pad},
  If[ratim < rat
   , pad = Round[ (idim[[2]] rat - idim[[1]])/2]; 
   ImagePad[im, {{pad, pad}, {0, 0}}, "Fixed"]
   , pad = Round[ (idim[[1]] /rat - idim[[2]])/2]; 
   ImagePad[im, {{0, 0}, {pad, pad}}, "Fixed"]
   ]
  ]

t = pad[i, {4, 5}]
$\endgroup$
2
  • $\begingroup$ I guess so (not a fan of the explicit If though, feels too C-ish) but I also believe something a lot shorter than both of our answers must be possible with a language like Mathematica. $\endgroup$
    – Cetin Sert
    Mar 24 at 11:13
  • 1
    $\begingroup$ It is sort of a game to produce the shortest code. But it is often very cryptical and the ideas behind are well hidden. $\endgroup$ Mar 24 at 16:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.