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

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?".
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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?
snibgo's IM pages: im.snibgo.com
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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"?
snibgo's IM pages: im.snibgo.com
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

If in a .bat file, then % needs to be doubled to %%.
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post by Konrad B »

is my IM 6.8.9-9 out-dated?
My viewer is "IrfanView"
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Perhaps another IM Windows user can double check my command and view the results. I am not a Windows user.
Konrad B
Posts: 10
Joined: 2017-02-25T12:47:24-07:00
Authentication code: 1151

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

Post 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?
Post Reply