Page 1 of 1

trim alpha but maintain center

Posted: 2012-05-23T10:47:49-07:00
by svenr
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!

Re: trim alpha but maintain center

Posted: 2012-05-23T12:14:46-07:00
by fmw42
post a link to an example image

Re: trim alpha but maintain center

Posted: 2012-05-23T23:29:01-07:00
by anthony
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

Re: trim alpha but maintain center

Posted: 2012-05-24T16:07:19-07:00
by svenr
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).

Image

Re: trim alpha but maintain center

Posted: 2012-05-24T17:16:39-07:00
by anthony
Let us know how you go. No matter what happens!

Re: trim alpha but maintain center

Posted: 2012-05-24T22:04:46-07:00
by fmw42
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).

Image
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.

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.

Re: trim alpha but maintain center

Posted: 2012-05-24T22:28:01-07:00
by anthony
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.

Re: trim alpha but maintain center

Posted: 2012-05-27T10:58:39-07:00
by svenr
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.

Re: trim alpha but maintain center

Posted: 2012-05-27T14:27:32-07:00
by fmw42
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

Posted: 2012-05-28T09:46:05-07:00
by svenr
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!!!

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