Page 1 of 1

Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-01T12:18:53-07:00
by FUrn
I have a large number of images with differing levels/amounts of white background/border, meaning the contents of each image takes up different amounts of the canvas. See for example this image:

Image

Which fills a lot less of the canvas than this image:

Image

What I'd like to do (and I hope it's possible) is to process each image in the batch, and for each one identify the side of the image with the least amount of white space between the canvas edge and the outermost non-white element of the photo (e.g. the bottom of the chair legs in the above images), and then apply this white space 'distance' to the other 3 sides. I'm perhaps not super clear in my description, or at the very least not familiar with the technical terms relevant to what I'm looking to do. But in essence I'm looking to automatically transform the first image above into something like this, i.e. adjust the canvas size so that the subject of the image takes up roughly the same proportions of the canvas across all images in my batch.

Image

Thanks in advance!

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-01T15:09:38-07:00
by fmw42
One simple way is to trim the image to its bounding box of the object, then pad as much as you want.

But please always provide your IM version and platform since syntax may differ.

In Unix syntax for all images in a folder using IM 6

Code: Select all

mogrify -path path_to_existing_output_folder -fuzz XX% -trim +repage -background white -border X *
or for one image at a time

Code: Select all

convert image.suffix -fuzz XX% -trim +repage -background white -border X path_to_existing_output_folder/output.suffix

________________________

If using Imagemagick 7, then see http://imagemagick.org/script/porting.php#cli


For novices, see

http://www.imagemagick.org/discourse-se ... f=1&t=9620
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/
https://github.com/ImageMagick/usage-markdown
https://imagemagick.org/script/porting.php#cli

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-01T15:45:50-07:00
by FUrn
Thanks fmw42, and thanks for pointing out the need to always mention my IM version! I'm using IM 7 on a Windows PC. Would the IM 6 Unix syntax you wrote earlier be the same or similar for my setup?

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-01T17:38:45-07:00
by fmw42
For IM 7, change convert to magick and change mogrify to magick mogrify. If in a .bat file then double the % to %%

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-02T13:38:53-07:00
by FUrn
I've amended your code to this:

Code: Select all

magick Original.jpg -fuzz 90% -trim +repage -background white -border 10 -bordercolor white Output/modified.jpg
A couple of things though:

The border colour is grey, despite my use of '-bordercolor white'. Am I doing something wrong here?

More than that, the border command seems to add a border on top of the image, rather than around it. How do I add a fixed amount of white space around the trimmed image? Could I simply enlarge the canvas by a fixed number of pixels on each end, and then centre the image? How would I do that? As far as I'm aware, -extent would set an absolute canvas size; however all my images are different sizes (which I want to maintain) and I would just be looking to add, say, 100 pixels to each edge.

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-02T14:39:15-07:00
by snibgo
Fred made a slight typo. To set the color for borders, use "-bordercolor", not "-background". See http://www.imagemagick.org/script/comma ... php#border

As your input is fully opaque (because it is from a JPG), this should add a border around the image.

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-02T14:43:53-07:00
by fmw42
Thanks for the correction, snibgo. It should indeed be

Code: Select all

magick Original.jpg -fuzz 90% -trim +repage -bordercolor white -border 10 -bordercolor white Output/modified.jpg

Re: Adjust Canvas Size To Align Canvas Fill Proportions Across Images

Posted: 2019-04-03T01:46:01-07:00
by FUrn
Thanks both! Focusing on doing this for image batches, how would I amend it to only operate on images called 'Main.jpg'? I have lots of images in a number of sub-folders, but only want to trim and border those fitting a certain naming convention. I thought of putting it into a loop such as this, but it doesn't seem to work!

Code: Select all

for filename in *Main.jpg; do
    mogrify $filename -fuzz 90% -trim +repage -bordercolor white -border 200
done
I don't get any errors per se, but no images have changed - neither the ones not called Main or the ones called Main.jpg

UPDATE: I got it to work using magick rather than mogrify:

Code: Select all

for filename in *Main.jpg; do
    magick $filename -fuzz 10% -trim +repage -bordercolor white -border 60 "$filename"
done