Converting to Monochrome

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
stevenp
Posts: 5
Joined: 2012-06-21T16:44:58-07:00
Authentication code: 13

Converting to Monochrome

Post by stevenp »

I have a need to use gocr and to improve it's recognition I have to convert an image to Black & White.
the source image:
Image

opened and saved as "Monochrome Bitmap" in Windows 7 x64 Paint
Image
and gocr recognizes all the numbers from the image

then because I need this stuff working in an AutoHotkey script, I have tried ImageMagick
at first it was as simple as

Code: Select all

convert in.jpg -monochrome in.pcx
but the result is:
Image

then i tried another approach

Code: Select all

convert in.jpg -colors 2 -monochrome in.pcx
Image

still not the result I pursued
I tried few hours to obtain the same result as in MS Paint but with no luck.
Neither of trials was recognized by gocr.
The most similar was to use contrast:

Code: Select all

convert in.jpg -contrast-stretch 700x0 -monochrome in.pcx
Image
But the text gocr recognized:

Code: Select all

__o__?q__00__0___0______0__?0_____

Please Help, i have tried various dithering methods too with no luck
Maybe you have a better experience with ImageMagick and could reproduce the monochrome image from mspaint with ImageMagick.
Please
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting to Monochrome

Post by fmw42 »

try this and adjust the threshold percent as desired.

convert sourcem.png -colorspace gray -auto-level -threshold 50% result.pcx
stevenp
Posts: 5
Joined: 2012-06-21T16:44:58-07:00
Authentication code: 13

Re: Converting to Monochrome

Post by stevenp »

thanks it was really easy to deploy resultNN.pcx with a batch file and check results with another one
-colorspace gray -auto-level -threshold 50% result.pcx
the threshold 64% gived the near-identical result, but gocr still didn't recognized nothing, so it's probable a gocr fault or something wrong with the pcx files
Opened in Photoshop and... there's a difference
Image
stevenp
Posts: 5
Joined: 2012-06-21T16:44:58-07:00
Authentication code: 13

Re: Converting to Monochrome

Post by stevenp »

However, there's everything fine when viewing in some viewer, like ACDSee:
Image

it's because the image is still 24bit
I have tried -depth 2, but the pcx still remain at 24 bits per pixel

So now the question is how to save a 1-bit per pixel pcx file?
Thanks
Last edited by stevenp on 2012-06-21T18:53:36-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Converting to Monochrome

Post by anthony »

stevenp wrote:thanks it was really easy to deploy resultNN.pcx with a batch file and check results with another one
-colorspace gray -auto-level -threshold 50% result.pcx
the threshold 64% gived the near-identical result, but gocr still didn't recognized nothing, so it's probable a gocr fault or something wrong with the pcx files
Opened in Photoshop and... there's a difference
Image
That looks like a file file format error of some kind.

For other ways of thresholding see
http://www.imagemagick.org/Usage/quantize/#monochrome
You will not what dithering.
You could also try 'two color quantization' which is a sort of 'auto-level threshold'.
Just turn of dithering!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
stevenp
Posts: 5
Joined: 2012-06-21T16:44:58-07:00
Authentication code: 13

Re: Converting to Monochrome

Post by stevenp »

it's because the image is still 24bit
I have tried -depth 2, but the pcx still remain at 24 bits per pixel

So now the question is how to save a 1-bit per pixel pcx file?
Thanks!
stevenp
Posts: 5
Joined: 2012-06-21T16:44:58-07:00
Authentication code: 13

Re: Converting to Monochrome

Post by stevenp »

Solved by saving as pgm file - it is supported in gocr
Thanks for the Help!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Converting to Monochrome

Post by anthony »

try -type grayscale
or -type bilevel
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Thirumal
Posts: 1
Joined: 2015-05-07T22:50:42-07:00
Authentication code: 6789

Re: Converting from Bitmap to 1Bpp image

Post by Thirumal »

Can any one please share Code to convert Bitmap to 1Bpp image. Looking forward for your reply.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Converting from Bitmap to 1Bpp image

Post by fmw42 »

Thirumal wrote:Can any one please share Code to convert Bitmap to 1Bpp image. Looking forward for your reply.

Please provide an example input image. You can upload to some place such as dropbox.com and post the URL here.

Also always provide your version of IM and platform.

Without seeing your image, we do not know if it is color or grayscale, but try

Code: Select all

convert image -colorspace gray +dither -colors 2 -type bilevel result
or

Code: Select all

convert image -monochrome result
or

Code: Select all

convert image -colorspace gray -threshold XX% -type bilevel result
Post Reply