How to control output bit depth in graphicsmagick (for Node)?

1.4k Views Asked by At

So I have two PNG images, both non-transparent 24bpp. One image contains a rainbow, other one contains a single line of text: enter image description here

I do the same thing with both of them:

var gm = require('gm').subClass({imageMagick: true})
gm("./sources/source.png").bitdepth(24).write("test.png", function(){
    console.log("test.png")
});
gm("./sources/source2.png").bitdepth(24).write("test2.png", function(){
    console.log("test2.png")
});

where gm is this

And I set both to 24bpp explicitly

In result I have two images with different bit depth:

enter image description here

In some cases I also had 32bpp image.

How can I make it create only 24bpp image (discard alpha channel if needed). Also, I don't want to create jpgs.


Thanks to @mark-setchell, I could force bit depth. I did it this way in Node:

gm("./sources/source.png")
    .out("-define")
    .out("png:color-type=2")
    .write("test.png", function(){
    console.log("test.png")
});

out() is an undocumented method but it basically helps you add custom parameters to commandline. Notice that

    .out("-define png:color-type=2")

won't work, it only works if you pass each parameter in individual .out() call

.bitdepth(24) doesn't seem to affect output at all, probably because I did .subClass({imageMagick: true}) above.

1

There are 1 best solutions below

0
On

My suggestion is to try using -define to set the variable png:color-type=2. As you worked out, and kindly shared with the community, it is done as follows:

gm("./sources/source.png")
    .out("-define")
    .out("png:color-type=2")
    .write("test.png", function(){
    console.log("test.png")
});