how -trim images with black border and black background

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

how -trim images with black border and black background

Post 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
User avatar
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

Post 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
snibgo
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

Post 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.
snibgo's IM pages: im.snibgo.com
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: how -trim images with black border and black background

Post by pet »

ok,
examples: Image A and B http://www.webform.ch/ttt/hc_105.jpg
Image

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.
User avatar
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

Post 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.
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: how -trim images with black border and black background

Post 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:
Image

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

Post 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."
snibgo's IM pages: im.snibgo.com
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: how -trim images with black border and black background

Post 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.
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: how -trim images with black border and black background

Post 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
User avatar
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

Post 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)
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: how -trim images with black border and black background

Post 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.
snibgo
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

Post by snibgo »

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
User avatar
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

Post 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.
pet
Posts: 13
Joined: 2013-08-13T10:11:30-07:00
Authentication code: 6789
Location: Switzerland

Re: how -trim images with black border and black background

Post 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:

Image

trim only the black border

Image



thanks for the help

pet
User avatar
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

Post 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.
Post Reply