# (Batch) resize images to fit within circle

How can I automatically resize hundreds of images so that each fits within the same-sized circle?

I have images (icons, in PNG format) with various shapes. I will be compositing these each with an identical circle background. So, I need to resize the images such that all the axes, in every degree, of the shape fits within a circle of specified size.

Most image resizing tools will resize assuming a rectangular shape. With differing shapes, this can result in an image that has been made too small with too much padding to the circle's edge; or that is too large, and overlaps the circle's edge.

What tool (I might use Gimp or Inkscape, but general discussion is also fine) can I use to automatically resize these images to each entirely fit within a circle, with their largest axis in any direction fitting to a specified circle?

• You can't if the images are all different sizes. Neither GIMP nor Inkscape know anything about where a subject in an image is located or what size it is. Photoshop CC has an auto select subject feature, but it doesn't always work and very much depends on the image. You can record Actions in Photoshop and apply the action on a batch of images. However the time taken to experiment and set this up, and test it, will probably take longer than just doing it manually. – Billy Kerr Sep 18 '20 at 10:08

In general the process is this:

1. Loop over your set of PNG images, and for each one:
2. Get the current dimensions (width & height)
3. Calculate the scaling factor
4. Apply the scale transformation
5. Write the new image out to disk

For step 3, the formula involves the Pythagorean theorem.

Given W = width, H = height, and R = radius of the circle, the scaling factor would be: `2*R/sqrt(W^2+H^2)`

To actually implement this, I would recommend trying ImageMagick or writing some Python code.

Using ImageMagick 7, processing a single image to fit a circle of diameter 123 can be done with:

``````magick input.png -precision 15 -resize "%[fx:100*123/hypot(w,h)]\%" output.png
``````

To batch process an entire folder on Windows, use a BAT file like:

``````set DIAMETER=123
for %%I in ( *.png ) do (
magick "%%I" -precision 15 -resize "%%[fx:100*%DIAMETER%/hypot(w,h)]\%%" "output\%%I"
)
``````
• Thanks Byron. “I would recommend trying ImageMagick”; can you edit the answer to say how ImageMagick can be used to automate this? – bignose Dec 22 '20 at 1:17