Dithered Edges when converting from PNG to GIF, same problem

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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Dithered Edges when converting from PNG to GIF, same problem

Post by fmw42 »

User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Dithered Edges when converting from PNG to GIF, same problem

Post by anthony »

Without either color replacement or flood fill is tricky.

Basically you will need to determine what pixels are NOT involved in the anti-aliasing of the edges. Usually that is by floodfill or coor replacement.

If those are NOT posible, especially when involving background colors other than white or black, then you will need a masking method.

For that separate out a mask of the original image, ( -alpha extract ) and resize that
a -threshold 0 of that will then give you the transparency mask of the pixel not involved in anti-aliasing.

Now take the original image flatten it on white (or what ever background color), then resize it in the same way. Now if you re-add the thresholded mask, (using -compose CopyOpacity ) you make the pixels not involved in the anti-alising, transparent.

Here is the command (for a skyblue background)

Code: Select all

convert orig2.png \
  \( -clone 0 -background skyblue -flatten -resize 100x100 \) \
  \( -clone 0 -alpha extract -resize 100x100 -threshold 5% \) \
  -delete 0 -compose CopyOpacity -alpha off -composite \
  result.gif
This works, and should produce an image with a anti-aliased edge blending to a skyblue background. You can use whatever color you like.

NOTE: the threshold is 5% due to the way resize works. A smaller
value than this (unless a "Gaussian" or better still a "Mitchell " filter is enforced, can produce 'ringing effects'. This does not happen if transparency is present as IM would automatically switch to a "Mitchell" filter. For more infor see 'Ringing' Artifacts, in Im examples, Resize page.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply