Dithered Edges when converting from PNG to GIF - RESOLVED

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?".
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Dithered Edges when converting from PNG to GIF - RESOLVED

Post by mrshake »

I'm converting a PNG to GIF, and the dithering between the colored and transparent edges in the image are acting weird. It looks like the dither is choosing black as the trasition color... this is sticking out badly... can I control the color?
Last edited by mrshake on 2009-01-15T13:26:58-07:00, edited 1 time in total.
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

Post by anthony »

Can you give a link to an example image of the problem (before and after).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
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

Post by fmw42 »

mrshake wrote:I'm converting a PNG to GIF, and the dithering between the colored and transparent edges in the image are acting weird. It looks like the dither is choosing black as the trasition color... this is sticking out badly... can I control the color?
Note that GIF does not allow full 8-bit transparency. If your PNG has 8-bit transparency, then at least half will be lost to probably black. See

http://www.imagemagick.org/Usage/formats/#boolean_trans
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

Post by anthony »

GIF saving should not dither transparency by default in any case. It should be equivelent to a threshold of 50% of the alpha channel.
In any case 'black' should not be a result!

An example of the problem (images and command) will let us confirm just what is going on.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Re: Dithered Edges when converting from PNG to GIF

Post by mrshake »

Here is the image I'm starting with:

Image

and the final gif with the bad edges

Image
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

Post by anthony »

That is not dithering.

What it looks like is that you rotated the image on a black background then made anything perfectly black transparent. Naturally the edges will be blended with the black, resulting in what you see.

NOTE this is preferable if the GIF is to be displayed on a very dark background (such as on a web page). It will make the edge look smoother, and less jaged.

Suggestion. add -matte -background none before the rotate then when the image gets saved to GIF IM should do a automatic 50% transparency threshold (for boolean transparency).

That should get ride of the black.

For more help I need to see the command you are trying to use.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Re: Dithered Edges when converting from PNG to GIF

Post by mrshake »

Changing the matte doesn't seem to help... there is never a black background, nor one set... thats the tough part... this is all in PerlMagick... but here is the code:

Code: Select all

print "Set Density\n";
my $x15 = $agent4->Resample(density=>'300x300');
warn "$x4" if "$x4";
print "Set Units\n";
my $x16 = $agent4->Set(units=>'pixelsperinch', matte=>'True', mattecolor=>'none');
warn "$x4" if "$x4";
print "Set Rotation\n";
my $x17 = $agent4->Rotate(degrees=>'-5', background=>'none');
warn "$x4" if "$x4";
print "Set Scale\n";
my $x18 = $agent4->Scale(geometry=>'194x233');
warn "$x4" if "$x4";

print "Writing rotated temp file\n";
my $x19=$agent4->WriteImage($wftmp);
warn "$x19" if "$x19";

print "Composite rotated png onto transparent background\n";

my $im = Image::Magick->new();
my $bg = Image::Magick->new(size => "225x256");

my $rc = $bg->Read("xc:none");

$rc = $im->Read($wftmp);
die $rc if $rc;

#$rc = $bg->Set(density=>'300x300', matte=>'True', mattecolor=>'white');
$rc = $bg->Set(density=>'300x300');

$rc = $bg->Composite(gravity => "Center", image => $im);
warn $rc if $rc;

print "Write Temporary PNG file\n";

$rc = $bg->Write($wf2);
die $rc if $rc; 

print "Clearing Variables\n";

undef $agent1;
undef $agent2;
undef $agent3;
undef $agent4;
undef $agent5;
undef $im;
undef $bg;
undef $rc;


print "Convert PNG to GIF\n";

$im = Image::Magick->new();
$rc = $im->Read($wf2);

$rc = $im->Set(dither=>'False');

$rc = $im->Write($wf3);
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

Post by anthony »

ImageMagick version?

The default installation from some repositories are so old it pre-dates some very fundamental bug fixes.

Also the PNG image you supplied is the actiual source of the black color. The image contains semi-transparent pixels which has black mixed in with it. As it is semi-transparent, the color is then diluted with the background so you don't see the black.

That black color should NOT be present, and is probably a very VERY old 'black halo' type bug. I'll need the original unrotated image for testing.

You can see the black in the semi-transparent pixels by using...

Code: Select all

convert testagentws.png -channel A -threshold 99%  
testagentws_check.png

display -texture xc:white testagentws_check.png
No part of the above involves GIF, dithering, color quantization, or any of the things you though was the cause.

The black is in the semi-transparency of the source image. And semi-transparency should never contain any extraneous color such as black.

The more typical test for this is to rotate a pure white image, then when finished flatten it on white. If you see anything that is not white, we have a 'transparency halo' bug.


The current releast of IM does not have this bug for resize (see
Resize Halo Bug Fixed IM v6.2.4. Or one in rotate. here is the test for both.

Code: Select all

convert -size 100x100 xc:white -background none -rotate 5 -resize 50% -background white -flatten x:
result should be pure white.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Re: Dithered Edges when converting from PNG to GIF

Post by mrshake »

I'm currently running 6.4.6Q16 for IM

Here is the original image:

http://www.adamandnicole.com/gallery2/g ... f/test.tif
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

Post by anthony »

Okay I can validate that some operation in the latest IM still contains the 'halo' bug.

Your initial part of the perl script is equivelent to...

Code: Select all

convert test.tif -resample 300x300 -units pixelsperinch -matte -mattecolor none -background none -rotate -5 -scale 194x233 t.miff
Cutting this down and checking for semi-transparent black blending (the halo bug)...

Code: Select all

convert test.tif -resample 300x300 -matte  -background none -rotate -5 -scale 194x233 -channel A -threshold 99% -background white -flatten x:
Now -resample is done before the rotation so no transparency is involved. -rotate I know does not have a halo problem, so that leaves -scale.

Yeap -scale is at fault.

Code: Select all

convert -size 50x50 xc:white -matte -bordercolor none -border 20x20 -scale 5x5 -background white -flatten -unique-colors txt:-
# ImageMagick pixel enumeration: 3,1,65535,rgba
0,0: (54668,54668,54668,65535) #D58CD58CD58C rgba(83.418%,83.418%,83.418%,1)
1,0: (59062,59062,59062,65535) #E6B6E6B6E6B6 rgba(90.1228%,90.1228%,90.1228%,1)
2,0: (65535,65535,65535,65535) #FFFFFFFFFFFF white
This should produce a pure white image, but instead you get three colors (center, edge, corner) caused by the 'black halo' bug

As -resize halo effect was fixed, if you replace -scale with -resize, you get a pure white image and no black halo bug.

In Summery: -scale has the old 'halo bug' present.


As for you problem, replace image scale with proper image resize, something you should have been doing anyway!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
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

Post by anthony »

Cristy just reported that he has now fixed the bug.

I have now also confirmed -scale, has been fixed with regard to the black halo bug.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Re: Dithered Edges when converting from PNG to GIF

Post by mrshake »

Brilliant!! The halo is gone!!

However, I'm still left with a problem... and its still the edges... here is what I'm ending up with, and then an example of what it needs to be:

Image

Image


The 2nd image is what my edges need to look like. I think the biggest issue here is that the original I'm trying to replicate was made in Photoshop with options that don't seem to translate well to IM. If I could convince them to move away from gif, it would make the whole thing easier.. but it needs transparency and is a web graphic... grrr
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

Post by anthony »

GIF does not allow semi-transparent edges. You either get jagged edges as you first image. OR you blend the semi-transparency with some color (like white or black) which was what you were getting before. Your original problem was with black your new example of what you 'want' is with white.

Solution. either rotate the image with a white background, or flatten the PNG image with a white background. After that you can then flood fill (either with -draw or the -floodfill operator) perfectly pure white to transparency. See IM examples, Color Modifications, Floodfill

For more examples for GIF handling see
GIFs on a solid color background
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
mrshake
Posts: 35
Joined: 2008-11-18T08:18:51-07:00

Re: Dithered Edges when converting from PNG to GIF

Post by mrshake »

it seems so simple.. yet it escapes me!!

here is what I added to the script just before the gif write as well as changing all the transparent backgrounds to white in the rest of the script.

$rc = $im->FloodfillPaint(geometry=>'0x0', fill=>'none');

the image when complete now appears to have a black background instead of transparent...
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

Post by anthony »

You must re-enable the transparent channel.. the color "none" is actually fully-transparent black, so if you don't have transparency, you just get black.

However DID you get the white blend in the edge? That is the question.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply