Page 1 of 1

Png8 to Png4, problem with alpha channel

Posted: 2015-04-21T23:07:45-07:00
by Vurtba2
Hello

I have a large number of small png images that I want to optimize in regards of file-size.
Originally the images are 8-bit png and the optimization I do is to convert them to 4-bit paletted png's. The results are quite good, the images contains rather few colors so reducing them to a 4-bit palette gives ok results.

However I have run into a problem with some images. Images that are just "empty" (all-white) beckome all-black after I've applied the optimization.

I am using ImageMagick 6.7.7-10 2014-03-06 Q16
The command I run is:

Code: Select all

mogrify -dither FloydSteinberg -colors 16 -define png:color-type='3' -define png:bit-depth='4' image.png
As you might tell, I specify that the image shall be 16 colors, bit depth 4, type 3=palette and a dithering should be applied to counter the color reduction.

Example original image:
http://i.imgur.com/rbGHcS7.png

After conversion:
http://i.imgur.com/6XwHEpS.png

How can this be solved?

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-22T08:44:04-07:00
by fmw42
IM 6.7.7.10 is rather old (over 230 versions old). Perhaps you should try upgrading and try again. Both your images download and display as white for me using Mac PREVIEW

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-22T23:18:28-07:00
by Vurtba2
Hi fmw42

Regarding the version of ImageMagick I am simply using the version available from the repository.
I am using Ubuntu 14.04 which packages IM 6.7.7 http://packages.ubuntu.com/search?keywo ... ection=all

It's quite suprising that the second image displays as white for you. I have looked at it on both Windows and Linux and with various software (IE, Firefox, image editors) and it always shows as black.

Ok, but to return to issue at hand I think that the root of the problem is that the converted image has 'Background color' set to black, whereas in the original image it is set to white.

identify on original image: http://pastebin.com/dD1n6GEg
identify on converted: http://pastebin.com/N9jYHEYF

I have tried to solve the problem by tweaking the command I run in various ways, but the results are always the same.

For example I've tried:

Code: Select all

convert file.png -dither FloydSteinberg -colors 16 -define png:color-type='3' -define png:bit-depth='4' -background white -alpha remove -flatten convert.png
Maybe I'm missing something in the invocation of IM?

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-23T07:27:02-07:00
by fmw42
The file you have for download http://i.imgur.com/rbGHcS7.png is bilevel (and in fact perfectly white - one color only) and setting the depth to 4 is not going to change that. Also adding -background white is a setting and not an operator, so it will have no effect unless there is an operator to use the setting. Try -set background white. Not sure if that will work.

Code: Select all

Image: file.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: DirectClass
  Geometry: 256x256+0+0
  Units: Undefined
  Type: Bilevel
  Base type: Bilevel
  Endianess: Undefined
  Colorspace: Gray
  Depth: 8/1-bit
  Channel depth:
    gray: 1-bit
    alpha: 1-bit
  Channel statistics:
    Pixels: 65536
    Gray:
      min: 255 (1)
      max: 255 (1)
      mean: 255 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
    Alpha:
      min: 255 (1)
      max: 255 (1)
      mean: 255 (1)
      standard deviation: 0 (0)
      kurtosis: 0
      skewness: 0
      entropy: nan
  Colors: 1
  Histogram:
     65536: (255,255,255,255) #FFFFFF graya(255,1)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Background color: graya(255,1)
  Border color: graya(223,1)
  Matte color: graya(189,1)
  Transparent color: graya(0,0)
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 256x256+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2015-04-23T07:24:30-07:00
    date:modify: 2015-04-23T07:22:48-07:00
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 6
    png:IHDR.color_type: 6 (RGBA)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 256, 256
    png:sRGB: intent=0 (Perceptual Intent)
    signature: 3b874d3ba46c638fc3094f8e92fb744ca974893873f8885f54e23760f9b6311b
  Artifacts:
    filename: file.png
    verbose: true
  Tainted: False
  Filesize: 854B
  Number pixels: 65.5K
  Pixels per second: 65.537GB
  User time: 0.000u
  Elapsed time: 0:01.000
  Version: ImageMagick 6.9.1-2 Q16 x86_64 2015-04-20 http://www.imagemagick.org
Perhaps you need to upload your exact file that has problems.

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-23T16:02:05-07:00
by glennrp
If your specified background color is one that is not in the image itself, you need to use -colors 15 instead of -colors 16, to leave a palette entry open to hold it.

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-27T23:09:23-07:00
by Vurtba2
The file you have for download http://i.imgur.com/rbGHcS7.png is bilevel (and in fact perfectly white - one color only)
I have linked to two images.
The image you mention is the original image and yes, the original image displays as it should for me (all white).
The issue that I'm facing is that after running ImageMagick and performing some operations on the image it displays as all black which is entirely not what I want, it should still be all-white.

Original image (all white):
http://i.imgur.com/rbGHcS7.png

Command:

Code: Select all

mogrify -dither FloydSteinberg -colors 16 -define png:color-type='3' -define png:bit-depth='4' image.png
Image after conversion (all black):
http://i.imgur.com/6XwHEpS.png
Perhaps you need to upload your exact file that has problems.
Please see the first post in this topic (posted by me), or above in this post.
Try -set background white. Not sure if that will work.
Ok, I corrected the command and the background color is now set to white in the converted image, but still the converted image shows as all-black.
Hm, this was harder than I tought it was.

Command:

Code: Select all

mogrify image.png -dither FloydSteinberg -set background white -flatten -colors 16 -define png:color-type='3' -define png:bit-depth='4' test.png
Identify on converted image:
http://pastebin.com/UwL2pBjf

Resulting converted image from above command:
http://i.imgur.com/UqOVdcx.png

I don't really know what's causing the image to be displayed as all black. My best guess is the "Transparent color: black" value that turns the entirely white image into black.

Again, I have tried tweaking the command and specifying transparent color to something else, but I've always ended up with "Transparent color: black" in the resulting images.
Any pointers on how to specify transparent color?

Re: Png8 to Png4, problem with alpha channel

Posted: 2015-04-28T09:12:26-07:00
by glennrp
There is a bug with converting grayscale+alpha images to indexed (png color-type=3). A workaround is to add the "-colorspace sRGB" option:

Code: Select all

mogrify -colorspace sRGB image.png -dither FloydSteinberg -set background white -flatten -colors 16
                     -define png:color-type='3' -define png:bit-depth='4' test.png