png dither colors, odd artifacts?

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
terry
Posts: 4
Joined: 2012-11-11T12:24:43-07:00
Authentication code: 67789

png dither colors, odd artifacts?

Post by terry »

First off, ImageMagick is awesome. Thanks to the people out there that put it together. Great work!

I'm trying to convert a png32 file down to 256 colours/png8, preserve transparency/alpha, and dither BUT I keep on getting some weird artifacts OR a black background.
The closest I could get to having 256 colours and dithering is example #7 - by separating the alpha channel and by using -ordered-dither on it beforehand. It looks kinda wonky @_@.
I'm relatively new to ImageMagick. So, I apologize if I'm doing something obviously stupid or if what I'm trying to do is just not possible...

I'm running...
WAMP 2.2
PHP 5.3
Win 7x64
ImageMagick 6.8.0 Q16

Here's some various sample code with img output results...

1.Original File
Image

2.

Code: Select all

exec('convert old.png -alpha off -dither FloydSteinberg -alpha on -colors 255 new.png');
Image

3.

Code: Select all

exec('convert old.png -alpha off -dither FloydSteinberg -colors 255 -alpha on new.png');
Image

4.

Code: Select all

exec('convert old.png -colors 255 -dither FloydSteinberg new.png');
Image

5.

Code: Select all

exec('convert old.png -dither FloydSteinberg -colors 255 new.png');
Image

6.

Code: Select all

exec('convert old.png -quantize transparent -dither floydSteinberg -colors 255 new.png');
Image

7.

Code: Select all

exec('convert old.png ( -channel A -ordered-dither 8x8  ) -dither FloydSteinberg -colors 255 new.png');
Image

8.hi-res - similiar to #2, #5
Image
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: png dither colors, odd artifacts?

Post by glennrp »

I verified the behavior you observed with IM-6.8.0-5, and also saw the same when I used txt as the output format, so this has nothing to do with PNG.
This doesn't look too bad to me:

Code: Select all

exec('convert old.png ( -channel A -threshold 60%  ) -dither FloydSteinberg -colors 255 new.png');
but that doesn't answer your question about the artifacts in the transparent area.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: png dither colors, odd artifacts?

Post by fmw42 »

Looks to me like alpha value is getting included in the dither to one of your selected number of colors unless you use -alpha off .... -alpha on
terry
Posts: 4
Joined: 2012-11-11T12:24:43-07:00
Authentication code: 67789

Re: png dither colors, odd artifacts?

Post by terry »

Thanks Glen!
I appreciate the recommendation. It sucks that you could confirm the artifacts too, i was hoping that it was just me... Just in case it was just me I though I would post on the forum and perhaps resolve the issue but otherwise I'm not too worried. Would you know why this is happening?
terry
Posts: 4
Joined: 2012-11-11T12:24:43-07:00
Authentication code: 67789

Re: png dither colors, odd artifacts?

Post by terry »

fmw42 i did tinker with -alpha off, -alpha on

#2

Code: Select all

exec('convert old.png -alpha off -dither FloydSteinberg -alpha on -colors 255 new.png');
#3

Code: Select all

exec('convert old.png -alpha off -dither FloydSteinberg -colors 255 -alpha on new.png');
Perhaps I implemented it in a incorrect way? Could you please elaborate, maybe with an example?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: png dither colors, odd artifacts?

Post by fmw42 »

This seems to work (better?).

imb convert original.png -alpha off -dither FloydSteinberg -colors 255 -alpha on PNG32:- | convert - PNG8:original_c255.png
terry
Posts: 4
Joined: 2012-11-11T12:24:43-07:00
Authentication code: 67789

Re: png dither colors, odd artifacts?

Post by terry »

Cool, thanks fmw42. Your code gives similar results to Glen's recommendation.

Code: Select all

exec('convert old.png ( -channel A -threshold 60%  ) -dither FloydSteinberg -colors 255 new.png');
Both will suffice.

Also, just wanted to say that the ImageMagick team has done some really great work. Really appreciate what you guys have done!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: png dither colors, odd artifacts?

Post by fmw42 »

Glenn's code would appear to be more flexible as you have control over the threshold.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: png dither colors, odd artifacts?

Post by anthony »

But are you actually getting png8 format output?

Note last I heard the quantization code generates 255 colors, but the dither code seems to ignore many of those colors and generate images with only about 64 colors. A color tree search problem.

As such some verification on exactly how many colors the final image is using should be looked at.

Dither in IM really needs a overhaul, especially to have it work better for <256 colors, using some new dithering techniques that is now available.

Actually can anyone see a point for dither with more than 256 colors. This was what the IM dither was designed to actually allow, and I think the reason why it is having problems.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply