Use of '-levels' option

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
peterhull90
Posts: 5
Joined: 2012-01-13T08:50:08-07:00
Authentication code: 8675308

Use of '-levels' option

Post by peterhull90 »

Hi,
I've got an gray scale raw image where every pixel is one byte in the range 0=black, 15=white. I wanted to convert this, and thought the -level option was the right one.

Code: Select all

convert -size 104x160 -depth 8 gray:input -level 0,15 output.png
but this gives me an image mostly white with some black pixels.

To test this, create a grayscale image

Code: Select all

convert -size 16x16 -depth 8 "gradient:black-gray(15)" 1.png
And

Code: Select all

convert 1.png -level 0,15 2.png
I would expect this to be something similar to what '-normalize' would do, but I get a row of black pixels at the top the rest is white.

Is this a bug or am I using '-level' wrong?

Using Version: ImageMagick 6.9.9-11 Q16 x86_64 2017-09-30
(for Cygwin)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Use of '-levels' option

Post by snibgo »

You create 1.png with 8 bits per channel, just one channel. But you are using IM Q16, so when 1.png is read, pixels are expanded to 16 bits/channel. This expansion is done by multiplying by 257. So the instead of 15 in the level, use 15*257 which is 3855.
snibgo's IM pages: im.snibgo.com
peterhull90
Posts: 5
Joined: 2012-01-13T08:50:08-07:00
Authentication code: 8675308

Re: Use of '-levels' option

Post by peterhull90 »

Got it, thanks.
Post Reply