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

Grayscale image isn't saved correctly for BMP format #218

Open
ehgus opened this issue Feb 13, 2023 · 1 comment
Open

Grayscale image isn't saved correctly for BMP format #218

ehgus opened this issue Feb 13, 2023 · 1 comment

Comments

@ehgus
Copy link

ehgus commented Feb 13, 2023

I discovered that a grayscale image is saved like a color image. I have compared the saved images from Julia and Matlab:

using Images
using FileIO

img = Gray.(rand(512,512));
save("image_julia.bmp",img);
img = rand(512,512);
imwrite(img,"image_matlab.bmp");

The result shows that Matlab represents an image as true grayscale, whereas Julia changes img to a colored view before saving. The image from Julia is obviously three times larger. Such unwanted behavior is not discovered in PNG format.

@timholy
Copy link
Member

timholy commented Feb 13, 2023

Are you sure Matlab is using ImageMagick? Julia is not "forcing" the conversion: what gets passed to ccall((:MagickWriteImages, libwand), ... is a wand created with grayscale UInt8 values: with

tim@diva:~/.julia/dev/ImageMagick/src$ git diff libmagickwand.jl
diff --git a/src/libmagickwand.jl b/src/libmagickwand.jl
index 51766d7..7e1d971 100644
--- a/src/libmagickwand.jl
+++ b/src/libmagickwand.jl
@@ -242,6 +242,7 @@ function constituteimage(buffer::AbstractArray{T}, wand::MagickWand, colorspace:
     ncolors = colorsize(buffer, channelorder)
     p = pointer(buffer)
     depth = T == Bool ? 1 : bitdepth(buffer)
+    @show cols rows nimages ncolors depth colorspace channelorder
     for i = 1:nimages
         status = ccall((:MagickConstituteImage, libwand), Cint, (Ptr{Cvoid}, Cssize_t, Cssize_t, Ptr{UInt8}, Cint, Ptr{Cvoid}), wand, cols, rows, channelorder, storagetype(T), p)
         status == 0 && error(wand)

I get

julia> save("image_julia.bmp",img)
cols = 512
rows = 512
nimages = 1
ncolors = 1
depth = 8
colorspace = "Gray"
channelorder = "I"

Seems like we're doing everything we can to convince it to save as a grayscale image; the fault might lie with the ImageMagick C library itself. If that's the case, this is a bug that would have to be reported to their developers.

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