how -trim images with black border and black background
how -trim images with black border and black background
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
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: how -trim images with black border and black background
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
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
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: how -trim images with black border and black background
If you want to manipulate how much is actually trimed, you can:
This tells you how much would be trimmed. Capture these in a script, manipulate as desired, then crop.
Code: Select all
convert in.png -format "%w %h %X %Y" info:
snibgo's IM pages: im.snibgo.com
Re: how -trim images with black border and black background
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: how -trim images with black border and black background
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.
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
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.
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: how -trim images with black border and black background
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."
It might be: "A bad trim is where one side is trimmed by more than twice any of the other sides."
snibgo's IM pages: im.snibgo.com
Re: how -trim images with black border and black background
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.
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.
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: how -trim images with black border and black background
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
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: how -trim images with black border and black background
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)
"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)
- GreenKoopa
- Posts: 457
- Joined: 2010-11-04T17:24:08-07:00
- Authentication code: 8675308
Re: how -trim images with black border and black background
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: "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 learn something new every day here.fmw42 wrote: You can actually get the trim coordinates without having to actually trim the image.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: how -trim images with black border and black background
See my fist reply, which should have read:
Code: Select all
convert in.png -trim -format "%w %h %X %Y" info:
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: how -trim images with black border and black background
My comment was addressed to the OP, who perhaps did not try my solution. I have not tested it either.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.
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
now i have a shell-script written, that exactly convert what i need
EXAMPLE:
trim only the black border
thanks for the help
pet
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
trim only the black border
thanks for the help
pet
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: how -trim images with black border and black background
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.
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.