Code: Select all
convert in.JPG -fuzz 50% -trim out.JPG
Example:
Input file:
Output file:
Expected output file (+5% of padding):
Code: Select all
convert in.JPG -fuzz 50% -trim out.JPG
Linux shell.Bonzo wrote:How you are going to use the code eg. batch, shell, php etc.
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.).Bonzo wrote:You do not want to add padding but stop the trim removing so much? Reducing the fuzz value should help.
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%
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
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