Remove an uneven, monochrome border

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
kaja

Remove an uneven, monochrome border

Post by kaja »

http://omploader.org/vNHZuaA is my image. I need to remove the big white part on one side.

I've been googling around and surprisingly this is more difficult to do than I'd have expected. I don't want to use any bash scripts to make this work (although I will if I /have/ to).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove an uneven, monochrome border

Post by fmw42 »

Why not just trim (-fuzz XX% -trim +repage) the white all around and then pad it back out with white to how ever you want using -border
kaja

Re: Remove an uneven, monochrome border

Post by kaja »

fmw42 wrote:Why not just trim (-fuzz XX% -trim +repage) the white all around and then pad it back out with white to how ever you want using -border
Try it. It does nothing to the image.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Remove an uneven, monochrome border

Post by el_supremo »

With ImageMagick, the basic operator to remove the edges is -trim and this would remove the white margins on your image if they were pure white or near-white.
BUT the margins have single black pixels which prevent the -trim operator from working. For example, if you look at your image you will see that there are two black pixels along the top edge at coordinates (865,0) and (866,0) and two black pixels on the bottom edge at (2088,3337) and (2088,3338). Those and other similar spots in the white area can be removed with a median filter although it will also affect the text in the image as well. You'll have to try it and see if it produces an acceptable result.

Code: Select all

convert scan-017.pnm -median 0.1 -trim +repage scan-017-trim.png
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove an uneven, monochrome border

Post by fmw42 »

That is very strange. I have not been able to get it to trim either even with -fuzz 90%.

But I can do this:

convert -size 100x100 xc:black -bordercolor white -border 50 -monochrome temp.pnm
convert temp.pnm -trim +repage temp.png

and it works just fine. Both images are bi-level.


There must be something odd about your image that I don't see (apart from its large size).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove an uneven, monochrome border

Post by fmw42 »

el_supremo has found the issue.

If the odd pixels are near the border, then just crop or shave the border and then trim

So this works:

convert 017.pnm -shave 5x5 -trim +repage -bordercolor white -border 25 017_proc.pnm

and you don't need to worry about the median changing the looks of your text.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Remove an uneven, monochrome border

Post by el_supremo »

The shave doesn't really work either because there's a black spot at (171,2565) in the original which prevents the remainder of the white margin being trimmed.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Remove an uneven, monochrome border

Post by fmw42 »

el_supremo wrote:The shave doesn't really work either because there's a black spot at (171,2565) in the original which prevents the remainder of the white margin being trimmed.

Pete

Right. Too bad. Then how about:

convert 017.pnm -shave 5x5 -trim -gravity south -chop 0x1 -trim +repage 017_proc.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Remove an uneven, monochrome border

Post by snibgo »

The source image is a scan that has been bilevelled to black and white. You might get better results by working from the original scan.

We can make a mask that is entirely white for the almost-white areas of the source and transparent elsewhere, compose that over the source, and trim the result. This single command does two trims, so an optimisation may be possible.

A Windows command, so for Unix etc change "%%" to "%" and "^" to "\":

Code: Select all

convert ^
  scan-017.pnm ^
  ( ^
    +clone ^
    -median 0.1 -trim ^
    -background rgba(100%%,100%%,100%%,0) -flatten ^
    -channel Alpha -negate ^
  ) ^
  -composite ^
  -trim +repage ^
  sn.png
EDIT: changed the second trim from "+trim" to "-trim".
snibgo's IM pages: im.snibgo.com
Post Reply