Aliased/jagged edges when creating animated gif with transparent background

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
starjet
Posts: 4
Joined: 2017-06-08T10:36:34-07:00
Authentication code: 1151

Aliased/jagged edges when creating animated gif with transparent background

Post by starjet »

(Using version ImageMagick 7.0.5-10 Q16 x64 on Windows 10, convert.exe via command line)

I have a small batch of PNG files with transparent background that I want made into an animated GIF file. I'll provide them here for reference: https://files.catbox.moe/r1qu5c.rar

Now after using

Code: Select all

convert -dispose previous *.png test.gif
I get a GIF file that works as expected, but on some backgrounds appears highly aliased. On googling for solutions I have read things that implies that it's natural for GIF. However, I can do this in Photoshop without the result appearing aliased. Comparison here: http://imgur.com/a/FrIdk

Unfortunately, Photoshop is not an option for me as I need this to be doable via command line. If somebody could help me out, I'd be really grateful.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

Gif does not support 8-bit transparency. It only allows binary transparency. So that is why you get the noticeable aliasing in the gif output. There is nothing that can be done about as far as I know other than to create your animations as video (mpeg, etc) and not use gif animation. ImageMagick supports MPNG, but not APNG for animation, I believe. But MPNG is not very portable.
starjet
Posts: 4
Joined: 2017-06-08T10:36:34-07:00
Authentication code: 1151

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by starjet »

fmw42 wrote: 2017-06-08T13:30:31-07:00 Gif does not support 8-bit transparency. It only allows binary transparency. So that is why you get the noticeable aliasing in the gif output. There is nothing that can be done about as far as I know other than to create your animations as video (mpeg, etc) and not use gif animation. ImageMagick supports MPNG, but not APNG for animation, I believe. But MPNG is not very portable.
As I mentioned, I read explanations along these lines before coming here. However, both files in my comparsion are gif. The one made with Photoshop doesn't have this issue.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

Post both files to some free hosting service and put the URLs here so we can examine them. Also post your exact IM code or better the equivalent as a command line.

EDIT: sorry, I did not see you had posted that above.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

Sorry, I tested your data and get the expected aliasing due to gif binary transparency. If you are willing to replace the transparency with some color, then each file can be flattened against the background color and the gif animation created from them, which should not have aliased edges, since there is no transparency.

Unfortunately, I do not know how you created your animation in Photoshop, so cannot comment on how they are able to avoid that issue. Nor do I know enough about GIF transparency. Perhaps some other tools can created 8-bit gif transparency, but IM will not, to my understanding.

glennrp is the GIF expert here. Perhaps he can comment further or correct my understanding.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

Photoshop has a matte mode that perhaps mitigates the issue. See http://photoshopcafe.com/tutorials/tran ... parent.htm and viewtopic.php?f=1&t=32121
starjet
Posts: 4
Joined: 2017-06-08T10:36:34-07:00
Authentication code: 1151

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by starjet »

I simply imported all the frames in as layers and then used the "make frames from layers" feature, on Photoshop CS3.
Thank you for the information on how Photoshop does it. I'll try looking into other options.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

If I flatten each PNG against a black background, then make the animation, it shows no aliasing as I mentioned above. I chose black since the color under your transparency in the PNG was black, so any antialiasing was against a black background. However, I tried a white background and that worked fine also. So the color probably does not matter.

Here I use subshell processing in Unix (Mac OS X) to avoid having to save each new PNG image before making the animation. I called your folder of images, data. Curious why there is no imagename001.png?

Code: Select all

cd
cd desktop/data
list=`ls *.png`
echo "$list"
( for img in $list; do
convert $img -background black -flatten miff:-
done ) | convert -dispose previous -delay 10 - -layers optimize animation.gif
Image
starjet
Posts: 4
Joined: 2017-06-08T10:36:34-07:00
Authentication code: 1151

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by starjet »

Thanks again. This definitely works. However, my idea is to retain transparency, so this is a little less than ideal. I'm looking into some other ways to go about this now, so I'll report back if there's any progress, just in case someone else might also want to do something like this.
Also, the files were originally 001 to 017, and 001 was actually the last frame, so I just renamed it to 018 for convenience.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by snibgo »

As Fred says, gif alpha is binary (either 0 or 100%), but the input PNG files have values across the full range. Some experimentation on just the first frame shows that PS thresholds alpha at 50%, and IM doesn't, so:

Code: Select all

-channel A -threshold 50% +channel
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Aliased/jagged edges when creating animated gif with transparent background

Post by fmw42 »

snibgo wrote: 2017-06-09T00:10:17-07:00 As Fred says, gif alpha is binary (either 0 or 100%), but the input PNG files have values across the full range. Some experimentation on just the first frame shows that PS thresholds alpha at 50%, and IM doesn't, so:

Code: Select all

-channel A -threshold 50% +channel
That is a good point. But I tried it in my scripting and it does not seem to make much difference. I still cannot figure out exactly how PS matte works to mitigate aliasing for transparent images
Post Reply