convert to PNG8 with index transparency but without alpha

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
axxo1
Posts: 6
Joined: 2013-08-23T07:13:06-07:00
Authentication code: 6789

convert to PNG8 with index transparency but without alpha

Post by axxo1 »

Hello everyone!

I try to explain my problem as best I can.

I have many bmp (about a game) such as this one http://imgur.com/sFGCDET

As you can see these pictures have the most unlikely possible colour as background colour (#FC00FC) because my objective is to make #FC00FC transparent.

My second objective is to make these pictures as light as possible.

According to my Fireworks installation, that would be in the order from lightest to heavier
PNG8 no transparency 5.54K
PNG8 index transparency 5.60K
PNG8 alpha transparency 5.61K
GIF no transparency 7.49K
GIF index transparency 7.49K

As far as I know the difference between index transparency and alpha transparency is that the first is like a switch, you pick one colour to be transparent, whereas alpha transparency is the "fading" effect - different levels of transparency - which is not at all required in my case.

So I was trying to find a "convert" command that would translate the bmps in PNG8s with index transparency.

I try

Code: Select all

for file in *.bmp
do
convert $file ${file%????}.png
done
that gives me a
100b.png 6160 B
which I am afraid is not PNG8 because if I try

Code: Select all

for file in *.bmp
do
convert $file PNG8:${file%????}.png
done
I get a
100b.png 6134 B
so I learn that I have to use the "PNG8:" part

Now I have the #FC00FC-to-transparent problem to solve.
I try

Code: Select all

for file in *.bmp
do
convert $file -transparent '#FC00FC' PNG8:${file%????}.png
done
I get a
convert: Invalid palette length `100b.png' @ png.c/PNGErrorHandler/1416.

What "convert" command should I use to get a PNG8 index transparency conversion?

Thanks in advance.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert to PNG8 with index transparency but without alph

Post by glennrp »

I'm not seeing the problem with IM-6.8.8-9 Q16

Code: Select all

convert sFGCDET.png -transparent '#FC00FC' sfg.png
and

Code: Select all

convert sFGCDET.png -transparent '#FC00FC' png8:sfg8.png
both give me a PNG8. The only difference is that the first contains a bKGD chunk
while the other does not.
axxo1
Posts: 6
Joined: 2013-08-23T07:13:06-07:00
Authentication code: 6789

Re: convert to PNG8 with index transparency but without alph

Post by axxo1 »

glennrp wrote:I'm not seeing the problem with IM-6.8.8-9 Q16

Code: Select all

convert sFGCDET.png -transparent '#FC00FC' sfg.png
and

Code: Select all

convert sFGCDET.png -transparent '#FC00FC' png8:sfg8.png
both give me a PNG8. The only difference is that the first contains a bKGD chunk
while the other does not.
Hello Glen,

Thanks for your input.

I am on CentOS and I have done a quick yum install and because of this I have an old version (ImageMagick-6.5.4.7-7.el6_5.x86_64).

I try to install a new version now and will report back how it goes.
axxo1
Posts: 6
Joined: 2013-08-23T07:13:06-07:00
Authentication code: 6789

Re: convert to PNG8 with index transparency but without alph

Post by axxo1 »

This is not my area of expertise so I hope you will excuse my asking the following "stupid" questions:

First of all I failed to install ImageMagick-6.8.8-9.x86_64.rpm

I did as instructed on imagemagick.org
rpm -Uvh http://www.imagemagick.org/download/lin ... x86_64.rpm
and got many missing dependencies

Code: Select all

-bash-4.1# rpm -Uvh ImageMagick-6.8.8-9.x86_64.rpm
error: Failed dependencies:
        libHalf.so.4()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
        libIex.so.4()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
        libIlmImf.so.4()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
        libImath.so.4()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
        libfpx.so.1()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
        libltdl.so.3()(64bit) is needed by ImageMagick-6.8.8-9.x86_64
As I am very bad at finding dependencies/problems I installed Remi's RPM instead with

Code: Select all

rpm -i http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
yum --enablerepo=remi install ImageMagick-last
and got ImageMagick-last-6.8.7.4-1.el6.remi.x86_64

and
convert 100b.bmp -transparent '#FC00FC' PNG8:100b.png
is finally working and giving me a 5395 bytes file.
Thanks for pointing me in the right direction Glen!

I wonder if I can do anything else to make it smaller without losing its quality?
For example how do I make sure alpha transparency is not being used?

Wait a second :shock:

I notice now that
convert 100b.bmp PNG8:100b.png
gives me a 5382 bytes file with IM 6.8.7.4-1 as opposed to the 6134 bytes file I got by the same command with 6.5.4.7-7.
This means there is an improvement with each new IM version?

With the -transparent flag it becomes bigger (from 5382 to 5395 bytes) which makes sense although I don't know if this difference is because of index transparency or because of alpha transparency - I don't need alpha transparency.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert to PNG8 with index transparency but without alph

Post by snibgo »

If you want the very smallest png possible, you might use pngcrush after IM: http://pmt.sourceforge.net/pngcrush/
snibgo's IM pages: im.snibgo.com
axxo1
Posts: 6
Joined: 2013-08-23T07:13:06-07:00
Authentication code: 6789

Re: convert to PNG8 with index transparency but without alph

Post by axxo1 »

snibgo wrote:If you want the very smallest png possible, you might use pngcrush after IM: http://pmt.sourceforge.net/pngcrush/
Thanks snibgo.

After your post I read around that it is very popular and probably the first of its kind.

I compiled the source version with "make" without flags, put pngcrush inside /usr/bin and did
pngcrush 100b.png 100b1.png
pngcrush -brute 100b.png 100b2.png
but I only get it down to 8657 bytes which seems strange - I have no idea why.

On the other hand I have tried a website called tinypng.com and got 100b.png down to 4707 bytes!
The website states it's a lossy compression system but my naked eye can't see the difference and that's all that matters to me.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: convert to PNG8 with index transparency but without alph

Post by snibgo »

As far as I know, pngcrush is lossless. Glenrp, who responded upthread, would know for sure as he wrote it.

Another tip: png files often contain metadata we don't need, and "-strip" will remove this (but pngcrush won't, AFAIK).
snibgo's IM pages: im.snibgo.com
axxo1
Posts: 6
Joined: 2013-08-23T07:13:06-07:00
Authentication code: 6789

Re: convert to PNG8 with index transparency but without alph

Post by axxo1 »

Thanks for the tip!

As the author himself cared enough to answer my thread I will give it another go.

I must have done something wrong with the ImageMagick bmp to png conversion.

This time I will use the best version like this

wget http://www.imagemagick.org/download/ImageMagick.tar.gz
tar xzf ImageMagick.tar.gz
cd ImageMagick-x.x.x-x
yum install gcc
./configure
make
make install
ldconfig /usr/local/lib
/usr/local/bin/convert logo: logo.gif

OK now I am getting
convert: no encode delegate for this image format `PNG8:100b.png' @ warning/constitute.c/WriteImage/1168
Better stop for today, I have had enough for today.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert to PNG8 with index transparency but without alph

Post by fmw42 »

what do you get from

convert -list configure

for the line starting with Delegates. Just checking to be sure that libpng is installed and IM can find it.

What was your exact command?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert to PNG8 with index transparency but without alph

Post by glennrp »

snibgo wrote:As far as I know, pngcrush is lossless. Glenrp, who responded upthread, would know for sure as he wrote it.
Pngcrush is by default lossless. You can make it lose info by various options such as "-rem text". Try the "-reduce" option which
is still lossless but will remove an all-opaque alpha channel, recognize all-gray RGB images and convert them to grayscale, and
reduce 16-bit to 8-bit when that can be done losslessly (only use "-reduce" with pngcrush-1.7.73 or later).
Post Reply