Code: Select all
magick $filename -fuzz 10% -trim +repage -bordercolor white -border 12%% "$filename"


To complicate things, there are images that lie somewhere 'between' these...the first one has some shadows, whilst the second one uses a predominantly light grey, rather than white, background. If possible, I'd also like to apply the trim and re-border operation to images such as these:


Given I have several thousand images to go through, I need some way to process images that will come out well and ignore those that can't be operated on in this way. I thought of doing this by detecting the proportion of white (or if need be the colours/shades close to white) in each image and setting a threshold, above which I will trim and re-border those images and below which I will have to leave untouched. To find the right threshold and avoid mistakenly edited images, I thought it would make sense to have ImageMagick first categorise the images into two different directories based on the percentage of white in each image - so those above x% would go into the 'whiteBackground' folder and those below x% go into the 'otherBackground' folder. Is this possible, or for that matter is there a more efficient way to achieve what I am trying to do?
I am using using ImageMagick 7.0.8-14 on a Windows PC, with BASH syntax.