How can I convert a png to 2 bits per pixel?

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
Lassar
Posts: 2
Joined: 2014-10-31T15:15:24-07:00
Authentication code: 6789

How can I convert a png to 2 bits per pixel?

Post by Lassar »

I have been trying to optimize a png for the web.

Had some luck with this.

"convert xxx.png depth 1 colors 1 yyy.png"

It actually converted it to a 1 bit per pixel png.

I tried "convert xxx.png depth 2 colors 2 yyy.png" and did not have any luck with it.

Is this a limitation of the ImageMagik?

I know png's can have a bit depth of 2 bits per pixel.

I tired irfanview, and did not have any luck.

Are there other tools on the net, that can convert png's to 2 bits per pixel?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I convert a png to 2 bits per pixel?

Post by snibgo »

"-colors 4" will reduce the image to 4 colours, and make a palette image with 4 entries, so each pixel needs just 2 pixels.
snibgo's IM pages: im.snibgo.com
barbos
Posts: 1
Joined: 2019-02-11T02:51:56-07:00
Authentication code: 1152

Re: How can I convert a png to 2 bits per pixel?

Post by barbos »

unfortunately it didn't works in some cases.

For example, here is png I want to make 2 bit indexed color:
https://i.imgur.com/AmjOlkT.png

Here is original PNG content:

Code: Select all

IHDR = 0x49484452
    width       = 512
    height      = 512
    bitDepth    = 8
    colorType   = 3
    compression = 0
    filter      = 0
    interlace   = 0
PLTE = 0x504c5445: 768 bytes
IDAT = 0x49444154: 30641 bytes
IEND = 0x49454e44
I executed the following command:

Code: Select all

convert 555.png -colors 2 555_2.png
And got this result:
https://i.imgur.com/DYfJw80.png

At a glance looks good, but let's look under the hood:

Code: Select all

IHDR = 0x49484452
    width       = 512
    height      = 512
    bitDepth    = 8
    colorType   = 0
    compression = 0
    filter      = 0
    interlace   = 0
gAMA = 0x67414d41: gamma = 45455 [0.45455]
cHRM = 0x6348524d: 32 bytes
bKGD = 0x624b4744: 2 bytes
tIME = 0x74494d45: 7 bytes
IDAT = 0x49444154: 6824 bytes
tEXt = 0x74455874: keyword="date:create", text="2019-02-11T11:59:11+02:00"
tEXt = 0x74455874: keyword="date:modify", text="2019-02-11T11:39:37+02:00"
IEND = 0x49454e44
as you can see, the new image is still 8 bit color! And now it even with no palette!
I tried to modify original picture, reduce colors to 16 before processing but imagemagick convert anyway removes palette and make it 8 bit color!

Why it happens? Is this a bug?

Is there any other way to make 2 bit indexed color output?

PS: I used the latest version from this site: https://imagemagick.org/download/binari ... 16-x86.zip
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How can I convert a png to 2 bits per pixel?

Post by snibgo »

"-type palette" will tell IM to make a paletted file. There is no method to tell IM to use a certain number of entries in the palette.
snibgo's IM pages: im.snibgo.com
Post Reply