Page 1 of 1
Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T06:26:09-07:00
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:
Output file:
Expected output file (+5% of padding):
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T06:41:03-07:00
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.
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T06:50:21-07:00
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.).
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T06:55:58-07:00
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.
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T07:51:10-07:00
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.
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T09:21:13-07:00
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
Re: Trimming a photo that remains a few percent of padding
Posted: 2015-08-22T09:35:19-07:00
by Bonzo
Thanks for posting your code as it should help others, a lot of users do not