Page 1 of 2

jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T19:46:29-07:00
by gusteng
Hi
Im trying to convert a jpg file to grey scale 8-bit bmp (another program requires this specification). However, no matter what I do, colorspace remains sRGB. I tried to convert the jpg to grey scale jpg and then to bmp but that didnt help. Separating out one color (e.g. -channel B -separate) did not change the result. I also tried different bmp formats as suggested here. viewtopic.php?f=3&t=21621

# For example

Code: Select all

convert in.jpg -type GrayScale -colorspace GRAY -depth 8 out.bmp
identify -format "%[colorspace] <== %f\n" out.bmp
#gives
sRGB <== out.bmp

Similar issue described here
viewtopic.php?f=1&t=25460

Im I right that the IM coder cannot save bmp with a single grey channel? Hence IM will always save bmp as sRGB. A thread indicates that this is the case:
http://www.wizards-toolkit.org/discours ... =3&t=10524

Ubuntu 14.04
ImageMagick 6.7.7-10

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T20:12:13-07:00
by fmw42
Im I right that the IM coder cannot save bmp with a single grey channel? Hence IM will always save bmp as sRGB
I believe that is correct in older versions of IM. But on current versions (IM 6.8.9.7 Q16 Mac OSX) I can do

Code: Select all

convert rose: -colorspace gray rose.bmp
Image: rose.bmp
Format: BMP (Microsoft Windows bitmap image)
Class: DirectClass
Geometry: 70x46+0+0
Units: PixelsPerCentimeter
Type: Grayscale
Base type: Grayscale
Endianess: Undefined
Colorspace: Gray
Depth: 8-bit

But I cannot say whether other tools will recognize it as gray or not, since it still has 3 equal channels.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T20:45:01-07:00
by snibgo
To see how many channels are saved by IM, always use an external tool, such as exiftool. IM only tells us how many channels an image (not a file) has, after it has been read in.

Using exiftool, it does seem that IM always writes 3 channels to BMP files.

I hope it always writes gray images encoded in sRGB (as opposed to RGB).

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T20:46:04-07:00
by fmw42
IM 7 will write only one grayscale channel when it is release, though you can use the beta now.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T22:21:36-07:00
by gusteng
Thanks for the input. I tested IM7 (beta) but got the same result. Can it be that Im missing some libraries?

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-18T22:31:06-07:00
by fmw42
gusteng wrote:Thanks for the input. I tested IM7 (beta) but got the same result. Can it be that Im missing some libraries?
How are you checking the colorspace? What was your exact command? Can you post your jpg image to some free hosting service such as dropbox.com and put the URL here?

Does BMP support colorspace gray?

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T09:42:00-07:00
by gusteng
How are you checking the colorspace?
identify -format "%[colorspace] <== %f\n" out_gray.bmp
Also, the software that requires grey scale 8-bit bmp gives an error msg saying the format is not correct.
What was your exact command?
I have tried many ways (see my first post) but this is what I originally wanted to do.

Code: Select all

convert in.jpg -channel B -separate out_B.jpg
convert out_B.jpg -type GrayScale -colorspace GRAY out_gray.bmp
Or with IM7 (beta) I use magick instead of convert.
Can you post your jpg image to some free hosting service such as dropbox.com and put the URL here?
I have tried with all sorts of jpg images. I even tested with the IM wizard image!
http://www.imagemagick.org/image/wizard.jpg
Does BMP support colorspace gray?
I think so (but this is more based on what other software can do - e.g. ImageJ/Photoshop) but maybe this is more complicated than I first anticipated.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T10:38:22-07:00
by snibgo
http://en.wikipedia.org/wiki/BMP_file_format says the 8-bit/pixel format is for palette files. It doesn't give a non-palette 8-bit format.

Thus:

Code: Select all

convert rose: -colorspace gray -type palette b.bmp

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T10:46:13-07:00
by fmw42
I do not think BMP specifically supports grayscale, only palette as snibgo said. I tried the following and opened the images in PS and got the following results.

Code: Select all

convert rose: -colorspace gray -type palette b.bmp
indexed color

Code: Select all

im7 magick rose: -colorspace gray -type palette b.bmp
RGB color

I think IM7 has a bug here.

If I do

Code: Select all

convert rose: -colorspace gray -type palette b.jpg
grayscale

Code: Select all

im7 magick rose: -colorspace gray -type palette b.jpg
grayscale

You also have to ensure that you do not add a RGB profile when opening images in PS or other tools, which may automatically do so unless you tell it not to.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T13:51:30-07:00
by gusteng
I was wrong, bmp does not support grey scale (all 3 channels are needed) and -type should be pallette (but IM fixes that it seems). Sorry for stiring things up.
However, the following work to get the greyscale 8-bit (match output from other programs):

Code: Select all

convert in.jpg -colorspace GRAY -colors 256 out.bmp
The key here is the use of -colors.

I noticed that if I use -threshold on the greyscale file, e.g.:

Code: Select all

convert out.bmp -threshold 45% out_bw.bmp
something changes and out_bw.bmp is not recoqnized in the same way as out.bmp. Thats a mystery to me (the only change is that pixels are given a new value, 0 or 255).

Thanks for all the help!

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T14:42:37-07:00
by fmw42
When you do -threshold, the result is not grayscale palette, but b/w binary and binary images are not supported by all image formats. I do not know about bmp.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T14:53:09-07:00
by snibgo

Code: Select all

convert rose: -colorspace gray -threshold 45% out_bw2.bmp
This gives me a BMP with 3 colour channels, 8 bits/channel.

Code: Select all

convert rose: -colorspace gray -threshold 45% -type palette out_bw3.bmp
This gives me a palette file with 2 entries in the palette. Pixel data is stored at 1 bit/pixel.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T17:03:06-07:00
by fmw42
bmp does not seem to support grayscale colorspace, so it will be either 3-channel 24-bit RGB with the same data in each channel or palette with 8-bits total depending upon whether you specify -type palette or not. Apparently when doing palette, bmp recognized that there are only two colors in the palette for the thresholded images.

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T17:30:31-07:00
by fmw42
Did you try

Code: Select all

convert rose: -colorspace gray -threshold 45% -depth 1 -type bilevel out_bw3.bmp

Re: jpg to bmp grayscale - colorspace still sRGB

Posted: 2014-09-19T17:58:14-07:00
by snibgo
That gives 3 channels/pixel, 8 bits/channel.

Adding "-type palette" makes it a 1-bit palette file, as we might expect.

Code: Select all

convert rose: -colorspace gray  -type palette out_bw5.bmp
This makes a palette file with 256 entries, with the same values across the RGB channels of the palette entries.