Trimming 3 margins

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?".
Post Reply
wile
Posts: 5
Joined: 2012-03-05T20:18:54-07:00
Authentication code: 8675308

Trimming 3 margins

Post by wile »

Hi,

I have been successfully using trim command along with fuzz to remove white margins from my images. This time I want to remove only left, bottom and right margins, the top one must remain intact. I'm not sure how to do this, can anyone help please. I do batch processing so hopefully it can be done in a single line.

Many thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming 3 margins

Post by fmw42 »

I think you will have to do 3 successive one-sided trims as depicted in http://www.imagemagick.org/Usage/crop/#trim_oneside. They can all be chained in one command line, I believe.

You can also use my bash unix script, trimmer, if on Mac/Linux or Windows with Cygwin. See the link below. Or you can duplicate my code as you need and fold into one command line.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trimming 3 margins

Post by anthony »

Or add a single 'changed' pixel in the center of the top row of the image, the trim.
After the trim fix that pixel!

Making -trim, trim only selected sides as been something I wanted for a long time.
trouble is getting someone to do the work. The trim code itself is VERY straight forward.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming 3 margins

Post by fmw42 »

anthony wrote:Or add a single 'changed' pixel in the center of the top row of the image, the trim.
After the trim fix that pixel!

Making -trim, trim only selected sides as been something I wanted for a long time.
trouble is getting someone to do the work. The trim code itself is VERY straight forward.

Yes, that is basically what I did in my trimmer script. Added one black pixel to the center of the added border(s).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trimming 3 margins

Post by anthony »

As long as the rest of those pixels weren't black :-)

Actually negating that one pixel should work well, unless the pixel is a perfect (or near perfect) gray. negateing it back after trimming will restore it.

Of course this assumes the 'center' is not actually part of the trim region :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
wile
Posts: 5
Joined: 2012-03-05T20:18:54-07:00
Authentication code: 8675308

Re: Trimming 3 margins

Post by wile »

Thanks for your suggestions. Background is always white or near white and margins are more or less even so changing colour of a single pixel in the middle of the top row of pixels and changing it back would work. Could somebody please help me with the actual command line for it. Much appreciated.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trimming 3 margins

Post by anthony »

To draw a single pixel see the 'pixel' primative for -draw
http://www.imagemagick.org/Usage/draw/#primitives
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
wile
Posts: 5
Joined: 2012-03-05T20:18:54-07:00
Authentication code: 8675308

Re: Trimming 3 margins

Post by wile »

I don't know what I'm doing here. I tried the first basic examples from the link you've given but all I'm getting is this:

Code: Select all

D:\Images>convert -size 10x6 xc:skyblue  -fill black -draw 'point 3,2' -scale 10
0x60   draw_point.gif
convert.exe: Non-conforming drawing primitive definition `point' @ error/draw.c/
DrawImage/3142.
convert.exe: unable to open image `3,2'': No such file or directory @ error/blob
.c/OpenBlob/2589.
convert.exe: no decode delegate for this image format `3,2'' @ error/constitute.
c/ReadImage/532.
convert.exe: Non-conforming drawing primitive definition `point' @ error/draw.c/
DrawImage/3142.

D:\Images>convert -size 10x6 xc:skyblue  -fill black -draw 'color 6,3 point' -sc
ale 100x60 draw_color_point.gif
convert.exe: Non-conforming drawing primitive definition `color' @ error/draw.c/
DrawImage/3142.
convert.exe: unable to open image `6,3': No such file or directory @ error/blob.
c/OpenBlob/2589.
convert.exe: no decode delegate for this image format `6,3' @ error/constitute.c
/ReadImage/532.
convert.exe: unable to open image `point'': No such file or directory @ error/bl
ob.c/OpenBlob/2589.
convert.exe: no decode delegate for this image format `point'' @ error/constitut
e.c/ReadImage/532.
convert.exe: Non-conforming drawing primitive definition `color' @ error/draw.c/
DrawImage/3142.
The result is 2 blue rectangles but no dots on them.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Trimming 3 margins

Post by anthony »

As you are on windows you need to use DOS script syntax. Replace the single quotes with double.

See IM Examples, Windows Usage, DOS Scripts
http://www.imagemagick.org/Usage/windows/#dos
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
wile
Posts: 5
Joined: 2012-03-05T20:18:54-07:00
Authentication code: 8675308

Re: Trimming 3 margins

Post by wile »

OK that worked fine. So I should be able to paint 1 pixel black but this command seems to be using absolute coordinates while my images have very different dimensions. How can I paint it exactly in the middle of the top row of pixels regardless of image width?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Trimming 3 margins

Post by fmw42 »

Use -gravity north with your composition

convert image \( -size 1x1 xc:black \) -gravity north -compose over -composite result

In windows leave off the \ and just use (...)
wile
Posts: 5
Joined: 2012-03-05T20:18:54-07:00
Authentication code: 8675308

Re: Trimming 3 margins

Post by wile »

That worked beautifully, thank you all for help
Post Reply