Announcement

Collapse
No announcement yet.

convert and clean color images of documents to bw using imagemagick

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • convert and clean color images of documents to bw using imagemagick

    I am not going to write a lot.
    In working on image files that are mostly documents that are color and might have to be cleaned up and converted to black and white documents for whatever reason.
    I had worked with imagemagick some today and come up with these command line routines.
    I have not had a lot of time to test but put in a lot of hours and i have tried different colors on the colored images and my preference is png files.
    I have created color image files from documents using scanners and photo like devices like mobile devices/
    You might want to give this a try if you are having issues converting color image files to black and white, and it would be easy to do gray scale too.
    Here are a few lines of command line code to test using imagemagick.
    You will need a bw image file and i created a gif file using microsoft paint.
    The gif file only has 2 pixels in it, one black and the other white.
    I have include one in the zip file here.
    Code:
    rem batch routine to convert document in color being a color png file to a black and white png file
    rem using imagemagick software for windows
    rem
    copy colornew.png color.png
    rem  convert color png file to black and white
    c:\imagemagick\imconvert color.png -unsharp 35x35 -colorspace gray -lat 20x20-10%% +dither -remap bw.gif -normalize -level 55%% -negate -threshold 99.9%% bw.png
    
    rem  despeckle the bw.png file using morphology feature of imagemagick
    c:\imagemagick\imconvert bw.png -write MPR:source -morphology close rectangle:2x2 -clip-mask MPR:source -morphology erode:6 square  +clip-mask bwdespeckle.png
    
    rem  one pass to convert the color.png file to black and white png file with despeckle in one pass
    c:\imagemagick\imconvert color.png -unsharp 35x35 -colorspace gray -lat 20x20-10%% +dither -remap bw.gif -normalize -level 55%% -negate -threshold 99.9%% -write MPR:source -morphology close rectangle:2x2 -clip-mask MPR:source -morphology erode:6 square  +clip-mask bwdespeckle-onepass.png
    The last line is long the code you want usually, that creates bwspeckle-onepass.png.
    The only problem with that line is that it does remove some pixel on characters that are usually bad in print quality from the source.
    Compare both results of bw.png and the despeckled png images. The way this routine runs does remove a few pixels at are really wanted to be keep from the bw.png or original color.
    I just could not get imagemagick to remove all the black pixels that i wanted to remove that usually are noise or speckles really.
    That will have to come later but you can try and see the results you get.
    I am very happy about the end results of the bw png file sizes created.
    You just have to test the code for yourself to see if any of this helps or gets you started in some color to bw process.

    You can play with the -level and -threshold values.
    I believe you can use a low of 51 for the -level.
    I started the threshold percent to be somewhere between 65 and 75 but found i could 99.9 and get some better results on faded or light printed characters in the color image.



    Attached Files
    p purvis
Working...
X