Page 1 of 2
how -trim images with black border and black background
Posted: 2013-08-13T13:33:55-07:00
by pet
hi,
how i can convert -trim images with black borders , but many images have also a big black background. how i can 'trim' not more then for example 15-20 pixels on the edge ?
pet
Re: how -trim images with black border and black background
Posted: 2013-08-13T13:53:38-07:00
by fmw42
It would be best to provide some examples?
Why not just shave off the sides by 15 to 20 pixels? See -shave or -chop
http://www.imagemagick.org/Usage/crop/#shave
http://www.imagemagick.org/Usage/crop/#chop
Also what version of IM and what platform? please read
viewtopic.php?f=1&t=9620
Re: how -trim images with black border and black background
Posted: 2013-08-13T14:11:40-07:00
by snibgo
If you want to manipulate how much is actually trimed, you can:
Code: Select all
convert in.png -format "%w %h %X %Y" info:
This tells you how much would be trimmed. Capture these in a script, manipulate as desired, then crop.
Re: how -trim images with black border and black background
Posted: 2013-08-13T14:23:35-07:00
by pet
ok,
examples: Image A and B
http://www.webform.ch/ttt/hc_105.jpg
all images have different borders !
with trim i can auto-crop perfectly (example B)
if IM 'trim' not can find out the difference between border and background (example A) IM trim the half image.
i need a solution where i can 'auto-crop' trim limited.
Re: how -trim images with black border and black background
Posted: 2013-08-13T14:46:21-07:00
by fmw42
Best I can suggest is do it twice, once with -trim and once with -shave. Then chose the larger of the two output images.
The careful selection of a -fuzz XX% value before -trim, may allow you to trim the other.
You could also do -trim -background black -extent WxH
where W=original width - 15to20 as desired and H=original height -15to20 as desired.
That way, you trim to minimum bounds and then pad back out with black. You can change the pad color by adding -background somecolor before the -extent so that it matches the color of the boundary of the trimmed image. You would need to extract the boundary color after trimming if you want to be precise.
You can also shave more than you need and then pad back out to some desirable size.
Re: how -trim images with black border and black background
Posted: 2013-08-13T15:15:02-07:00
by pet
if i fill out the image after trimming, the problem is the position of the trimmed part.
here a example with the moon:
example A after
convert A.jpg -bordercolor black -fuzz 20% -trim B.jpg
if i fill out to the original size , the moon is in the middle of the image. (example B)
i have thousands of pictures with black-borders. so i need a efficient (script) solution
can be, that IM is a wrong way.
Re: how -trim images with black border and black background
Posted: 2013-08-13T15:20:53-07:00
by snibgo
You need to define exactly what you think is a "bad trim".
It might be: "A bad trim is where one side is trimmed by more than twice any of the other sides."
Re: how -trim images with black border and black background
Posted: 2013-08-13T15:30:54-07:00
by pet
no, is not a 'bad trim'
i think the IM 'trim' function is a perfectly solution for 'auto-crop' the most images with color-borders .
only i need defined limits of crop.
Re: how -trim images with black border and black background
Posted: 2013-08-13T15:39:50-07:00
by GreenKoopa
Is this what you want?
Code: Select all
: create an example image
convert rose: -background black -gravity center -extent 100x100 in.png
: Calculate trim, but only 10 pixels off max
convert in.png ^
( +clone -shave 10x10 -evaluate Set 100%% ) -gravity center -composite ^
-trim -format "%%wx%%h%%X%%Y" info:
--- Output ---
80x80+10+10
Re: how -trim images with black border and black background
Posted: 2013-08-13T16:06:32-07:00
by fmw42
You missed my first comment:
"Best I can suggest is do it twice, once with -trim and once with -shave. Then chose the larger of the two output images."
But GreenKoopa's solution will produce the same result. You run his script, get the crop coordinates and then use -crop on your original image.
GreenKoopa:
You can actually get the trim coordinates without having to actually trim the image. See the string format at
http://www.imagemagick.org/script/escape.php
%@ CALCULATED: trim bounding box (without actually trimming)
Re: how -trim images with black border and black background
Posted: 2013-08-13T17:51:02-07:00
by GreenKoopa
fmw42 wrote:
"Best I can suggest is do it twice, once with -trim and once with -shave. Then chose the larger of the two output images." But GreenKoopa's solution will produce the same result.
I'm not sure that they are always the same. Each of the four sides may be trimmed more or less than the shave. What exactly
pet wants I'm not sure.
fmw42 wrote:
You can actually get the trim coordinates without having to actually trim the image.
I learn something new every day here.
Re: how -trim images with black border and black background
Posted: 2013-08-13T18:23:38-07:00
by snibgo
See my fist reply, which should have read:
Code: Select all
convert in.png -trim -format "%w %h %X %Y" info:
Re: how -trim images with black border and black background
Posted: 2013-08-13T19:17:06-07:00
by fmw42
GreenKoopa wrote:I'm not sure that they are always the same. Each of the four sides may be trimmed more or less than the shave. What exactly pet wants I'm not sure.
My comment was addressed to the OP, who perhaps did not try my solution. I have not tested it either.
But I suppose one could test both widths and both heights (and offsets) and limit accordingly.
But your solution with the addition of %@ rather than actually trimming, is much better.
Re: how -trim images with black border and black background
Posted: 2013-08-14T13:53:59-07:00
by pet
now i have a shell-script written, that exactly convert what i need
Code: Select all
#!/bin/sh
## settings:
I="Q.jpg" ## image filename
F=20 ## -fuzz %
B="black" ## bordercolor
h=180 ## h = max border trim
######################################################
FULL=`convert $I -format "%w %h" info:`
TRIM=`convert $I -bordercolor $B -fuzz $F% -trim -format "%w %h %X %Y" info:`
## DEBUG echo $TRIM
declare -i a=`echo $FULL | awk '{ print $1 }'` ## fullvormat with
declare -i b=`echo $FULL | awk '{ print $2 }'` ## fullvormat height
declare -i c=`echo $TRIM | awk '{ print $3 }'` ## trim current width in pixels
declare -i d=`echo $TRIM | awk '{ print $4 }'` ## trim current image height in pixels
declare -i e=`echo $TRIM | awk '{ print $1 }'` ## trim page (canvas) x offset
declare -i f=`echo $TRIM | awk '{ print $2 }'` ## trim page (canvas) y offset
## test left limit
if [ $c -gt $h ] ; then LEFT=$h ; else LEFT=$c ; fi
## test right limit
if [ $((c+e)) -lt $((a-h)) ] ; then RIGHT=$h ; else RIGHT=$((a-e-c)) ; fi
## test top limit
if [ $d -gt $h ] ; then TOP=$h ; else TOP=$d ; fi
## test bottom limit
if [ $((d+f)) -lt $((b-h)) ] ; then BOTTOM=$h ; else BOTTOM=$((b-f-d)); fi
## DEBUG echo "crop $LEFT $RIGHT $TOP $BOTTOM"
WIDTH=$((a-LEFT-RIGHT))
HEIGHT=$((b-TOP-BOTTOM))
X=$LEFT
Y=$TOP
## OUTPUT for IM convert
echo " -crop $WIDTH x $HEIGHT + $X + $Y"
convert Q.jpg -crop 3872x2592+180+180 QQ.jpg
exit 0
EXAMPLE:
trim only the black border
thanks for the help
pet
Re: how -trim images with black border and black background
Posted: 2013-08-14T14:10:35-07:00
by fmw42
You do not have to actually trim the image. You can get the same results using %@
convert Q.jpg -fuzz 20% -format "%@" info:
319x121+15+39
or
convert Q.jpg -fuzz 20% -format "%@" info: | tr "x" " " | sed 's/+/ +/g'
319 121 +15 +39
vs
convert Q.jpg -fuzz 20% -trim -format "%w %h %X %Y" info:
319 121 +15 +39
Internally, I suspect, IM is doing the same thing, so I doubt in this case there is any difference. It would probably only help if one were to save the trimmed image and then get the size and offset.
I suspect that the %@ is just a shorthand version for "%w %h %X %Y" without specifying -trim, but with a slightly different output format.