Problem converting PNG with transparent background to GIF

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
masterofnone
Posts: 3
Joined: 2012-08-03T09:04:11-07:00
Authentication code: 15

Problem converting PNG with transparent background to GIF

Post by masterofnone »

Using ImageMagick-6.7.8-7-Q16-windows-dll

I have been trying to convert a small PNG image with a transparent background to a GIF image with a transparent background. I used this command structure:

convert -background none img.png img.gif

The result is a GIF image with a black background. However, if I reduce the color palette using:

convert -colors 4 -background none img.png img2.gif

I get the correct result, a GIF image with a transparent background.

Am I doing something wrong? Why would ImageMagick fill the background? And also, are there automatic settings to save the GIF with the indexed color palette? The quantization settings documentation specifies many options. Is there a shortcut or predefined group of settings?

Original PNG
Image

GIF with black background
Image

Correct result
Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem converting PNG with transparent background to GI

Post by fmw42 »

This indeed fails even with proper syntax:

convert 1emnwi.png 1emnwi.gif
convert 1emnwi.png -type palettealpha 1emnwi.gif

But this works fine for me on IM 6.7.8.7 Mac OSX Snow Leopard:

convert 1emnwi.png PNG8:1emnwi.gif

EDIT: the above command is nonsense and makes a png though labelled as gif suffix as pointed out by Glennrp below. So ignore this. I am not sure if any method works. I seem to recall a problem reported earlier about GIF smaller than 16x16, but cannot be sure.
Last edited by fmw42 on 2012-08-03T11:16:55-07:00, edited 1 time in total.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Problem converting PNG with transparent background to GI

Post by glennrp »

But this works fine for me on IM 6.7.8.7 Mac OSX Snow Leopard:

convert 1emnwi.png PNG8:1emnwi.gif
But doesn't that end up with a PNG not a GIF? (use "identify -verbose 1emnwi.gif" to find out).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem converting PNG with transparent background to GI

Post by fmw42 »

glennrp wrote:
But this works fine for me on IM 6.7.8.7 Mac OSX Snow Leopard:

convert 1emnwi.png PNG8:1emnwi.gif
But doesn't that end up with a PNG not a GIF? (use "identify -verbose 1emnwi.gif" to find out).
Glenn,

DOH! :oops: Sorry I must not have been fully awake. That was nonsense and I don't know what I was thinking.

Is the original issue related to a problem I seem to recall about GIFs smaller than 16x16?

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

Re: Problem converting PNG with transparent background to GI

Post by glennrp »

fmw42 wrote: Is this related to a problem I seem to recall about GIFs smaller than 16x16?
Fred
Yes, it's very likely the same bug. If I do -sample 200% then the transparency is preserved in the GIF. ../glennrp
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Problem converting PNG with transparent background to GI

Post by fmw42 »

glennrp wrote:
fmw42 wrote: Is this related to a problem I seem to recall about GIFs smaller than 16x16?
Fred
Yes, it's very likely the same bug. If I do -sample 200% then the transparency is preserved in the GIF. ../glennrp

It is likely in the GIF writing. I tried converting to miff and piping to gif with the same bad results.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Problem converting PNG with transparent background to GI

Post by glennrp »

Interestingly, the GIF actually has one transprent pixel, at position 0,3. That doesn't help the original poster much,
but might give us something to think about while debugging this.
masterofnone
Posts: 3
Joined: 2012-08-03T09:04:11-07:00
Authentication code: 15

Re: Problem converting PNG with transparent background to GI

Post by masterofnone »

glennrp wrote:
fmw42 wrote: Is this related to a problem I seem to recall about GIFs smaller than 16x16?
Fred
Yes, it's very likely the same bug. If I do -sample 200% then the transparency is preserved in the GIF. ../glennrp
So there is a pre-existing issue with images less than 16x16? That is good to know.

I suppose in the short term I could use GIMP to batch convert my files as it saves the ouput correctly and reduces the color pallette. It's just that in the long term I'd like to build a process that will do this and ImageMagick is defintely the right tool for the job.

Thanks for the help guys!
Last edited by masterofnone on 2012-08-06T01:53:19-07:00, edited 1 time in total.
masterofnone
Posts: 3
Joined: 2012-08-03T09:04:11-07:00
Authentication code: 15

Re: Problem converting PNG with transparent background to GI

Post by masterofnone »

I've found other, older, threads with the same issue.


Possible bug writing small .gif images
viewtopic.php?f=3&t=18463

Losing transparency while resizing GIF image
viewtopic.php?f=3&t=19787
Post Reply