There are various times I've needed to change a picture to a new size with a different aspect ratio. I keep reimplementing the algorithm, so why not use this subsidized hosting to avoid that piece ?
The idea is to pick an appropriate side, increase the size of the other side until it is the right aspect for the target size, and resize down to the target dimensions. I prefer these in the center, so increasing the size is two step.
resize.py is the script I used to create kindle screensavers. I assume the final dimensions are 800 x 600 px. It starts a repl expecting a character code {r, c, h, exit}, the int of a single side, and y/n of whether the target of that side is 800. It looks like I last modified it 24 July '17. But I uploaded the last screensaver five years ago, '13. At that time I'm practicing python intentionally, hence the ternary on line 17.
np 181012 resize_image.py is the script I reimplemented today because I didn't have access to another copy. This is in service of work, where, once in a blue moon, I'll need to change a client's web logo to match the aspect ratio I prefer for report headers. I hadn't really needed it because, while the math is a little tricky, it's not many steps and had been for just one image. Today I needed three, and foresaw that maybe I'll end up doing this again. At this point I haven't used python in a year and one half. It assumes four integer sizes, corresponding to source height,width and target height,width. I assume that the width is the larger ratio side.
It's funny to look at these side by side. The elder is polished (though I mixed variable styles). The younger is a bit brute force. To be fair, I used the elder quite a bit, so I'd have many occasions to refactor it. On the other hand, I have vaguely noticed that I've not been so quick to decompose my functions as much as I could.
Did I mention I miss writing for my website ?
© Nicholas Prado. Released under terms of wtfp license, as described at http://www.wtfpl.net/txt/copying/.