Page 1 of 2

Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T13:43:24-07:00
by Konrad B
Hi,

to convert a color bitmap to 1-bit (black&white) bmp is simple,
per example in php environment:
@system('convert -colorspace gray -monochrome -depth 1 ' . $sourceColorBmp . ' ' . $oneBitBmp);

I need some help to convert diverse color bitmaps (with dark or light background) to "1-bit" - but always WHITE & black bitmap,
viz. the background of converted "1-bit" image must be always white, the foreground must be always black

Here are two examples for color bitmaps (source) i need to convert in 1-bit images with WHITE background & black foreground:
1. Bitmap with light background: http://bogx.de/media/20701-2756.bmp
2. Bitmap with dark background: http://bogx.de/media/20701-2757.bmp

Best regards,
Konrad

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T13:48:54-07:00
by snibgo
Your images have more then two colour (they are aliased). So what do you really want to happen?

What defines the background colour?

What version of IM do you use? On what platform?

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T14:41:35-07:00
by Konrad B
Hi Snibgo,

yes, the source (input) bitmap files have 24 BitsPerPixel with aliasing.

but desired quality of output files is low, very low, per example:
http://bogx.de/media/20670-2615_1bit.bmp
http://bogx.de/media/20672-2627_1bit.bmp

All input multicolor files (whatever their background color is - dark or light) should convert to white background and black foreground (text, icon, frame).

-----------------------
platform = Linux
API = php 5.6.3
libmagic = 517
-----------------------

Best regards,
Konrad

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T15:12:49-07:00
by snibgo
The basic problem is trivial, eg:

Code: Select all

convert 20701-2756.bmp -colorspace Gray -threshold 50% out.png
But the result might need to be negated. How do you define "background"?

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T23:25:36-07:00
by Konrad B
background is always homogeneous color (no gradient or color mix),
foreground is text, or text and icon or text, icon and frame - all of those always the same color.

per example:
Input files
1. input1.bmp: background = color1 (dark), foreground = color2 (light)
2. input2.bmp: backgrond = color3 (light), foreground = color4 (dark)

output files
ad 1. output1.bmp should be: background = white, foreground = black
ad 2. output2.bmp should be: background = white, foreground = black

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-25T23:55:54-07:00
by fmw42
What you want to do is make a binary image as snibgo suggested. Then blur it slightly to anti-alias the edges.

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% result.png

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T01:17:37-07:00
by Konrad B
Hi fmw42,

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% result.png
the result in your code must be "png" file?

I have changed "result.png" in "result.bmp":

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% result.bmp
but the result was completely black bitmap.

Is it easier to get input file as "png" also? For input I have both: png & bmp.
My result must be 1-bit "bmp" file (not a "png" image file) ...

Best, Konrad

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T01:54:52-07:00
by fmw42
Looks fine to me using

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% result.bmp
and then (unix)

display result.bmp

What is your IM version? What viewer are you using? Perhaps the viewer needs a BMP3: or BMP2: image format

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% BMP3:result.bmp

Code: Select all

convert 20701-2757.bmp -colorspace Gray -auto-level -threshold 50% -blur 0x1 -level 0x50% BMP2:result.bmp

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T02:12:33-07:00
by fmw42
If in a .bat file, then % needs to be doubled to %%.

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T02:24:17-07:00
by Konrad B
My IM Version:
Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-09-23 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

Viewer ist "IrfanView 64"

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T02:31:51-07:00
by fmw42
Did you try my BMP3: or BMP2: commands? What viewer are you using to look at your results. My command works fine for me on IM 6.9.7.9 Q16 Mac OSX.

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T02:54:10-07:00
by Konrad B
is my IM 6.8.9-9 out-dated?
My viewer is "IrfanView"

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T03:13:36-07:00
by fmw42
IM 6.8.9.9 should be OK. But check the changelog for changes to BMP. I suspect that your Irfanviewer is not liking BMP 4 with 1-bit.

See http://legacy.imagemagick.org/script/changelog.php

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T03:20:01-07:00
by fmw42
Perhaps another IM Windows user can double check my command and view the results. I am not a Windows user.

Re: Convert color bmp always to White & Black not to B&W

Posted: 2017-02-26T03:35:11-07:00
by Konrad B
Sorry, but this make me confused:
I made on my linux-server now a simple php file with 2 lines for test:
exec('convert 20701-2756.bmp convert -colorspace gray -monochrome -depth 1 result_1.bmp');
exec('convert 20701-2756.bmp -colorspace gray -auto-level -threshold 50% -blur 0x1 -level 0x50% result_2.bmp');

Result:
line 1 works
line 2 dont works - no file (result_2.bmp) is created :-(

My IM is maybe not up to date?