Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set bit depth when saving files? #260

Open
orswan opened this issue Jul 25, 2023 · 2 comments
Open

How to set bit depth when saving files? #260

orswan opened this issue Jul 25, 2023 · 2 comments

Comments

@orswan
Copy link

orswan commented Jul 25, 2023

I am trying to save a bitmap with a specific bit depth. However, regardless of the type of an image, when I save it as a bitmap, it ends up with bit depth 24. I cannot find anything in the documentation when I search for "bit depth". How can I accomplish this?

A minimal working example is as follows:

using Images, FileIO
#dir = "/path/to/dir"
im_large = n0f16.(Gray.(rand(100,100)))
save(dir * "large.bmp",im_large)
im_small = n0f8.(Gray.(rand(100,100)))
save(dir * "small.bmp",im_small)

Both saved bmp files end up having bit depth 24 on my system (Windows 10).

Edit: It seems that this issue has to do with the number of channels. Even though my images above are supposedly greyscale, the images are saved with three color channels (all having equal values). An 8-bit bmp should have just a single channel.

@orswan orswan changed the title How to set bit depth? How to set bit depth when saving files? Jul 25, 2023
@timholy
Copy link
Member

timholy commented Jul 26, 2023

This is a limitation of the ImageMagick C library. To prove this, I added

    @show T ncolors colorspace depth

right before this line in constituteimage and got the following output:

julia> save(dir * "large.bmp",im_large)
T = UInt16
ncolors = 1
colorspace = "Gray"
depth = 16

So we're passing in the full grayscale 16-bit image to that ccall; what they do with it is up to them.

We've had a lot of trouble with ImageMagick for many different formats, and the ImageIO package is gradually accumulating Julia-native readers of different file formats. Care to write one for BMP? The file format seems well-documented: https://en.wikipedia.org/wiki/BMP_file_format.

Alternatively, use a format with support for 16-bit grayscale. PNG (as long as you're using ImageIO.jl rather than ImageMagick) and TIFF (via Tiffmages.jl) should work fine.

julia> save(dir * "large.png",im_large)

julia> imgl = load(dir * "large.png");

julia> eltype(imgl)
Gray{N0f16}

@orswan
Copy link
Author

orswan commented Jul 26, 2023

Alternatively, use a format with support for 16-bit grayscale. PNG (as long as you're using ImageIO.jl rather than ImageMagick) and TIFF (via Tiffmages.jl) should work fine.

The images I generate are to be uploaded to a hardware device (a spatial light modulator) which only accepts .bmp. So unless you know of an easy way to batch convert one of those other file types to 8-bit .bmp, this won't work.

We've had a lot of trouble with ImageMagick for many different formats, and the ImageIO package is gradually accumulating Julia-native readers of different file formats. Care to write one for BMP? The file format seems well-documented: https://en.wikipedia.org/wiki/BMP_file_format.

Maybe someday. For now I just need to get something working, so I'm heading to Mathematica land for now (at least for this problem).

At any rate, thanks for the clarifying reply.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants