Trimming a photo that remains a few percent of padding

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
rafal_rr
Posts: 4
Joined: 2015-08-22T06:20:31-07:00
Authentication code: 1151

Trimming a photo that remains a few percent of padding

Post by rafal_rr »

Hello. I want to create a batch file to automatically trim my photos of some items. I used:

Code: Select all

convert in.JPG -fuzz 50% -trim out.JPG
But I want to remain some padding around trimmed imaged. How can I do it?

Example:

Input file:
Image

Output file:
Image

Expected output file (+5% of padding):
Image
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Trimming a photo that remains a few percent of padding

Post by Bonzo »

How you are going to use the code eg. batch, shell, php etc.

You do not want to add padding but stop the trim removing so much? Reducing the fuzz value should help.
rafal_rr
Posts: 4
Joined: 2015-08-22T06:20:31-07:00
Authentication code: 1151

Re: Trimming a photo that remains a few percent of padding

Post by rafal_rr »

Bonzo wrote:How you are going to use the code eg. batch, shell, php etc.
Linux shell.
Bonzo wrote:You do not want to add padding but stop the trim removing so much? Reducing the fuzz value should help.
No. Decreaing the fuzz value causes that some elements like shadows will not be trimmed as I expect. I want to define when the main element of photo is placed and cut off the rest without some margin (5% e.g.).
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Trimming a photo that remains a few percent of padding

Post by snibgo »

I think this has to be done in a script. "-format %@" gives the crop parameters that a trim would give. The script reads these values, modifies them so the crop isn't so severe, and does the crop. Windows BAT syntax:

Code: Select all

set INFILE=IXqVbz2.jpg
set OUTFILE=out.png

for /F "usebackq tokens=1-4 delims=x+" %%A in (`convert ^
  %INFILE% -fuzz 50%% -format %%@ info:`) do (
  set WW=%%A
  set HH=%%B
  set XX=%%C
  set YY=%%D
)

echo %WW% %HH% %XX% %YY%

set /A dx=%WW%/20
set /A dy=%HH%/20

set /A WW=%WW%+%dx%+%dx%
set /A HH=%HH%+%dy%+%dy%
set /A XX=%XX%-%dx%
set /A YY=%YY%-%dy%

if %XX% LSS 0 set XX=0
if %YY% LSS 0 set YY=0

echo %WW% %HH% %XX% %YY%

convert %INFILE% -crop %WW%x%HH%+%XX%+%YY% +repage %OUTFILE%
The "echo" commands are not needed. They are just so you can see what is happening.
snibgo's IM pages: im.snibgo.com
rafal_rr
Posts: 4
Joined: 2015-08-22T06:20:31-07:00
Authentication code: 1151

Re: Trimming a photo that remains a few percent of padding

Post by rafal_rr »

@snibgo: I think it is the thing I'm looking for! Thanks!
I will rewrite it into a bash script and paste here my result.
rafal_rr
Posts: 4
Joined: 2015-08-22T06:20:31-07:00
Authentication code: 1151

Re: Trimming a photo that remains a few percent of padding

Post by rafal_rr »

Bash version:

Code: Select all


#!/bin/bash

INPUT_FN=$1
PERCENT=$2

echo "Processing $INPUT_FN"

FN=${INPUT_FN%.*}
EXT=${INPUT_FN#*.}

# trim with padding:
TRIM_PARAMS=$(convert $INPUT_FN -fuzz ${PERCENT}% -format %@ info:)

#echo "Params: $TRIM_PARAMS"
set -- $(echo $TRIM_PARAMS | sed -e 's/[x+]/ /g')
w=$1
h=$2
x=$3
y=$4

dx=$((w /20))
dy=$((h /20))
if [ $((dx)) -gt $((dy)) ]; then
    d=$dx
else
    d=$dy
fi

#echo "w h x y d: $w $h $x $y $d"
if [ $((x)) -lt $((d)) ]; then
    dleft=$x
else
    dleft=$d
fi
if [ $((y)) -lt $((d)) ]; then
    dtop=$y
else
    dtop=$d
fi

w=$((w + dleft + d))
h=$((h + dtop + d))
x=$((x - dleft))
y=$((y - dtop))

#echo "w h x y: $w $h $x $y"

OUTPUT_FN=${FN}_fuzz${PERCENT}_pad.${EXT}
COMMAND="convert $INPUT_FN -crop ${w}x${h}+${x}+${y} +repage $OUTPUT_FN"
echo $COMMAND
$COMMAND


Usage:

Code: Select all

rr@rr-debian:~/Obrazy/automatyczne_wycinanie/work$ autotrim_with_padding.sh 1.JPG 50
Processing 1.JPG
convert 1.JPG -crop 2499x2442+942+0 +repage 1_fuzz50_pad.JPG
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Trimming a photo that remains a few percent of padding

Post by Bonzo »

Thanks for posting your code as it should help others, a lot of users do not :(
Post Reply