Imagemagick automatically lowering depth too far.

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?".
Post Reply
Aroymart
Posts: 4
Joined: 2016-05-11T21:47:04-07:00
Authentication code: 1151
Contact:

Imagemagick automatically lowering depth too far.

Post by Aroymart »

Hello! I've just started using ImageMagick so I appologize if I'm missing something simple.

I'm working on a project that requires indexed pngs with a Bit Depth of 2. I'm creating the images and converting them using

Code: Select all

convert [image location 1] -depth 2 [new image location]
This works perfectly for images with at least 3 colors, however with an image that uses only black and white, it gives it a bit depth of 1. I've confirmed this by running it on several different images and going to http://regex.info/exif.cgi to check the BitDepth.

My question is, is there any way to force a bit depth of 2 regardless of whether or not there are only 2 colors?

I'm using

Code: Select all

 ImageMagick 6.9.3-10 Q16 x86_64 2016-05-04
on the command line.

Thank you in advance
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by fmw42 »

What platform? What output image format? Did you use PNG8:output.png?

IM typically tries to output the smallest depth that fits the data?

Did you try using

-define png:bit-depth {depth}

See http://www.imagemagick.org/Usage/formats/#png_write
Aroymart
Posts: 4
Joined: 2016-05-11T21:47:04-07:00
Authentication code: 1151
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by Aroymart »

I'm using Windows 7 but through Cygwin. I'm outputting as png. I just tried it and that sets the depth to 8. There doesn't seem to be a PNG2: option though.

yea, I'm very limited by the other software.

I just tried

Code: Select all

 convert src/gfx/test.png -define png:color-depth=2 -depth 2 src/gfx/test4.png

and it's still bit depth of 1
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by fmw42 »

try

Code: Select all

convert src/gfx/test.png -define png:color-depth=2 PNG8:src/gfx/test4.png
But does your input image have more colors that 2? If so, you need to reduce the colors to black and white for example by

Code: Select all

convert src/gfx/test.png -colorspace gray +dither -colors 2 -threshold 50% -define png:color-depth=2 PNG8:src/gfx/test4.png
Aroymart
Posts: 4
Joined: 2016-05-11T21:47:04-07:00
Authentication code: 1151
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by Aroymart »

Thank you for the suggestions

The line

Code: Select all

convert src/gfx/test.png -define png:color-depth=2 PNG8:src/gfx/test4.png
results in a png of bit-depth 8

for images that start with 3-4 colors, converting the image to a bit depth of 2 works. Sometimes it groups greys that are too close together as the same color though so I'll experiment with those options to see if I can make it more strict, Thank you!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by fmw42 »

The PNG developer may have other ideas to suggest. You can also try -monochrome to convert to binary if you have other colors than black and white.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by glennrp »

Don't use "PNG8:" because that forces a bit-depth of 8. Try this:

Code: Select all

convert test.png -colors 4 \
              -define png:color-type=3 \
              -define png:bit-depth=2 \
              -define png:exclude-chunk=bKGD \
              test5.png
(excluding the bKGD to prevent your image from having 5 colors, that won't fit in a 2-bit palette)
Aroymart
Posts: 4
Joined: 2016-05-11T21:47:04-07:00
Authentication code: 1151
Contact:

Re: Imagemagick automatically lowering depth too far.

Post by Aroymart »

fmw42 wrote:The PNG developer may have other ideas to suggest.
Oh sorry, I know there are multiple ways to define/talk about bit depth. I and the website are talking about 2 bits per pixel, so up to 4 different colors at once. I'd like to have my images indexed with a bit depth of 2, which works when I feed it 3 or 4 colors.
glennrp wrote:Don't use "PNG8:" because that forces a bit-depth of 8. Try this:

Code: Select all

convert test.png -colors 4 \
              -define png:color-type=3 \
              -define png:bit-depth=2 \
              -define png:exclude-chunk=bKGD \
              test5.png
(excluding the bKGD to prevent your image from having 5 colors, that won't fit in a 2-bit palette)
That worked! Thank you so much! I did change one thing though (for future reference if anyone needs anything as specific as this :P ). With

Code: Select all

-define png:color-type=3
the resulting image came out negated and gave me the warning

Code: Select all

convert: Wrote palette index exceeding num_palette `src/gfx/music2.png' @ warning/png.c/MagickPNGWarningHandler/1656.
It's probably just me misunderstanding how the input file should have been formatting, but removing it worked so I'm content.


Thank you all very much!
Post Reply

Who is online

Users browsing this forum: Google [Bot], Semrush [Bot] and 15 guests