trim alpha but maintain center
trim alpha but maintain center
I would like to trim as much surrounding alpha/transparency from an image but maintain the images center point/position. The following does the trim but it's over zealous as the center position is not maintained.
convert test1.png -gravity Center -trim test1-out.png
Anyone have any tips or know if this is even possible?
Thx!
convert test1.png -gravity Center -trim test1-out.png
Anyone have any tips or know if this is even possible?
Thx!
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: trim alpha but maintain center
I think you will need to trim (or use the %@ escape) and from that result calculat how much you want to 'shave' of the edges of the image so you trim it equally on each side.
See Shave
http://www.imagemagick.org/Usage/crop/#shave
and Trim (with noisy images) for and example of getting trim details
http://www.imagemagick.org/Usage/crop/#trim_noisy
See Shave
http://www.imagemagick.org/Usage/crop/#shave
and Trim (with noisy images) for and example of getting trim details
http://www.imagemagick.org/Usage/crop/#trim_noisy
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: trim alpha but maintain center
The following shows an example pic. It's a 400x600 sized PNG (1px purple rectangle only there to show outer border) with a black dot in the center and some smiley faces in the mid-lower left side. The red 1px rectangle is there to just show the approximate image size I'd like the result to end up as.
I'll have to look into that 'info" option as if I understand things correctly I should be able to script this puppy!
Thx for the info (pun intended).
I'll have to look into that 'info" option as if I understand things correctly I should be able to script this puppy!
Thx for the info (pun intended).
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: trim alpha but maintain center
Let us know how you go. No matter what happens!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: trim alpha but maintain center
Is this image appropriate with the borders for your example. I did not think you had borders. So I am not sure how you want to trim this and where the problem lies. I thought the issue was that you were not getting a trim that maintained the center position.svenr wrote:The following shows an example pic. It's a 400x600 sized PNG (1px purple rectangle only there to show outer border) with a black dot in the center and some smiley faces in the mid-lower left side. The red 1px rectangle is there to just show the approximate image size I'd like the result to end up as.
I'll have to look into that 'info" option as if I understand things correctly I should be able to script this puppy!
Thx for the info (pun intended).
If your image has just the smileys and the dot, then the -trim (without +repage) will let you know the trim bounds in relation to the original image. That info is in the verbose info or can be extracted using string formats %O and %P (http://www.imagemagick.org/script/escape.php). Then you can compute how you want to crop on the basis of those and the center of your original image.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: trim alpha but maintain center
You can also get that info, without actually 'cropping' the image using %@ in the info: format.
It is something I found out while recently looking at code for IMv7 Shell API changes.
It is something I found out while recently looking at code for IMv7 Shell API changes.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: trim alpha but maintain center
As the image is translucent I added the borders as an example to just show the original image size (purple) compared to the desired image size (red). Those border lines would not be there in the actual input and output images.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: trim alpha but maintain center
Here is a set of command lines that seems to work on your two examples. I have put it into a simple bash unix script and mailed it to you separately.
Code: Select all
infile="sample1_in.png"
inname=`convert "$infile" -format "%t" info:`
ww=`convert -ping "$infile" -format "%w" info:`
hh=`convert -ping "$infile" -format "%h" info:`
cx=`convert xc: -format "%[fx:($ww/2)]" info:`
cy=`convert xc: -format "%[fx:($hh/2)]" info:`
triminfo=`convert "$infile" -format "%@" info:`
#echo "triminfo=$triminfo"
wt=`echo "$triminfo" | cut -d+ -f1 | cut -dx -f1`
ht=`echo "$triminfo" | cut -d+ -f1 | cut -dx -f2`
xoff=`echo "$triminfo" | cut -d+ -f2`
yoff=`echo "$triminfo" | cut -d+ -f3`
#echo "ww=$ww; hh=$hh; cx=$cx; cy=$cy; wt=$wt; ht=$ht; xoff=$xoff; yoff=$yoff"
d1x=`convert xc: -format "%[fx:(abs($xoff-$cx))]" info:`
d1y=`convert xc: -format "%[fx:(abs($yoff-$cy))]" info:`
d2x=`convert xc: -format "%[fx:(abs($xoff+$wt-$cx))]" info:`
d2y=`convert xc: -format "%[fx:(abs($yoff-$cy))]" info:`
d3x=`convert xc: -format "%[fx:(abs($xoff+$wt-$cx))]" info:`
d3y=`convert xc: -format "%[fx:(abs($yoff+$ht-$cy))]" info:`
d4x=`convert xc: -format "%[fx:(abs($xoff-$cx))]" info:`
d4y=`convert xc: -format "%[fx:(abs($yoff+$ht-$cy))]" info:`
d1=`convert xc: -format "%[fx:(hypot($d1x,$d1y))]" info:`
d2=`convert xc: -format "%[fx:(hypot($d2x,$d2y))]" info:`
d3=`convert xc: -format "%[fx:(hypot($d3x,$d3y))]" info:`
d4=`convert xc: -format "%[fx:(hypot($d4x,$d4y))]" info:`
dmax=`convert xc: -format "%[fx:max(max(max($d1,$d2),$d3),$d4)]" info:`
#echo "d1=$d1; d2=$d2; d3=$d3; d4=$d4; dmax=$dmax"
if [ "$d1" = "$dmax" ]; then
wd=`convert xc: -format "%[fx:2*$d1x]" info:`
ht=`convert xc: -format "%[fx:2*$d1y]" info:`
xo=`convert xc: -format "%[fx:($xoff)]" info:`
yo=`convert xc: -format "%[fx:($yoff)]" info:`
elif [ "$d2" = "$dmax" ]; then
wd=`convert xc: -format "%[fx:2*$d2x]" info:`
ht=`convert xc: -format "%[fx:2*$d2y]" info:`
xo=`convert xc: -format "%[fx:($cx-$d2x)]" info:`
yo=`convert xc: -format "%[fx:($yoff)]" info:`
elif [ "$d3" = "$dmax" ]; then
wd=`convert xc: -format "%[fx:2*$d3x]" info:`
ht=`convert xc: -format "%[fx:2*$d3y]" info:`
xo=`convert xc: -format "%[fx:($cx-$d3x)]" info:`
yo=`convert xc: -format "%[fx:($cy-$d3y)]" info:`
elif [ "$d4" = "$dmax" ]; then
wd=`convert xc: -format "%[fx:2*$d4x]" info:`
ht=`convert xc: -format "%[fx:2*$d4y]" info:`
xo=`convert xc: -format "%[fx:($xoff)]" info:`
yo=`convert xc: -format "%[fx:($cy-$d4y)]" info:`
fi
#echo "wd=$wd; ht=$ht; xo=$xo; yo=$yo;"
convert $infile -crop ${wd}x${ht}+${xo}+${yo} +repage ${inname}_crop.png
Re: trim alpha but maintain center
Works awesome Fred... Thanks so much!
I called the code in your script from a script to process all the files in a given directory as follows.
Cheers!!!
I called the code in your script from a script to process all the files in a given directory as follows.
Cheers!!!
Code: Select all
#!/bin/bash
DIR=$1
FILES=$DIR/*.png
shopt -s nullglob
for f in $FILES
do
echo "Processing $f file..."
# take action on each file. $f has current file name
centertrim $f $f.out
rm $f
mv $f.out $f
done