Help with dithering

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?".
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Help with dithering

Post by SharkD »

I describe a method of approximating the alpha channel using dithering techniques in this article. Could someone please help me by telling me how this can be automated using ImageMagick?

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

Re: Help with dithering

Post by glennrp »

SharkD wrote: I describe a method of approximating the alpha channel using dithering techniques in this article. Could someone please help me by telling me how this can be automated using ImageMagick?
Thanks!

With ImageMagick you would not need to dither. Simply composite the transparent image over the blue image. The result will not have a matte (alpha) channel. I suppose you could dither the alpha channel if you wanted, but why? ../glennrp
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

I'm not sure what you mean. Could you explain that again?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Post by glennrp »

SharkD wrote: I'm not sure what you mean. Could you explain that again?

If your objective is to end up with a simpler image, with no alpha channel, you do not need to dither. Just do a full-alpha composition of your image over the blue background. The resulting image will not have an alpha channel, and it will look better than the ragged-edge appearance that your example has.

See http://www.simplesystems.org/users/glennrp/composite/
Notice that the "composite" version has smooth edges compared to your "modified" version. Even if we reduce the composited version to 256 colors, it still looks better.

I used
  • convert -size 64x128 xc:blue blue64x128.png
    composite over Exp_image_original.png blue64x128.png Exp_image_full_composite.png
    convert Exp_image_full_composite.png -colors 256 Exp_image_full_composite_256colors.png
Glenn
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

I can't have the image blend in with the background--that's why I'm using dithering. I need to follow the steps outlined in the article I linked to.

[EDIT] nevermind, i figured it out. Here's what I did:

Code: Select all

set EXTN=.png
set FILE=Walls_Arena01
convert -channel matte -negate -separate -colors 2 -dither %FILE%%EXTN% %FILE%_alpha%EXTN%
composite -compose CopyOpacity %FILE%_alpha%EXTN% %FILE%%EXTN% %FILE%_new%EXTN%
convert -size 64x128 xc:blue blue64x128.png 
composite over %FILE%_new%EXTN% blue64x128.png E%FILE%_output%EXTN%
del %FILE%_alpha%EXTN%
del %FILE%_new%EXTN%
del blue64x128.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

As part of IM Example work, I am currently reviewing all the dithering aspects available in ImageMagick.

First of all if the resulting image does not require any transparency, you can just flatten the image to a fixed color the same as the background page on which you plan to display the image.

However if that page contains some background pattern than that will not work, One solution is to do the flattening, but then re-add a shaped transparency around the image.

This is already detailed in IM Examples for GIF image handling
http://www.cit.gu.edu.au/~anthony/graph ... bg_pattern

As for your example and artical. you do not need ths -dither argument at all as that is the default. als a -colors 2 is equivelent to a -monochorme operator. Also as -composite is no an operator for "convert" it is now posible to cobine all the operations given into a single command, removing the need for intermediate temporary files, and the clean up required afterward.

That is not the only dither technique however and there are other methods which is also shown in the above IM examples reference. These examples may however move to the current IM Example 'Color Reduction' area currently under development.
http://www.cit.gu.edu.au/~anthony/graph ... #reduction

Any further input or examples that you have found would also be appreciated, by you or anyone else.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

anthony wrote: First of all if the resulting image does not require any transparency, you can just flatten the image to a fixed color the same as the background page on which you plan to display the image.

How do you mean?
anthony wrote: As for your example and artical. you do not need ths -dither argument at all as that is the default. als a -colors 2 is equivelent to a -monochorme operator. Also as -composite is no an operator for "convert" it is now posible to cobine all the operations given into a single command, removing the need for intermediate temporary files, and the clean up required afterward.

Any help in this regard would be appreciated, as I'm new to ImageMagick.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

I suggest you go look at and try to follow the pointer I made to IM Examples
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

The resulting image does require transparency; so I can't just flatten it.

How would I truncate the above batch file into a single command?
Also, how would I change the dithering method to ordered instead of Floyd-Steinberg?
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Post by glennrp »

SharkD wrote: The resulting image does require transparency; so I can't just flatten it.

How would I truncate the above batch file into a single command?
Also, how would I change the dithering method to ordered instead of Floyd-Steinberg?

Code: Select all

convert -channel A -ordered-dither 4x4 %FILE%%EXTN% %FILE%_alpha%EXTN% 
Edit: removed "-dither" option.
Last edited by glennrp on 2006-08-11T07:54:35-07:00, edited 1 time in total.
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

I'm having trouble getting the -random-threshold command to apply only to the alpha channel, as described in this tutorial. It always causes the other channels to become reduced to two colors, as well.

Likewise for the example where the alpha channel is seperated before using the -monochrome command.

Ordered-dither works OK.

Here's are the command-lines I'm using:

Code: Select all

convert "%1%2" ( -channel A -random-threshold 5x95% ) ( -compose CopyOpacity -composite ) "%1_new%2"
and:

Code: Select all

convert "%1%2" ( +clone -fx a -monochrome +matte ) ( -compose CopyOpacity -composite )  "%1_new%2"
[EDIT] I just noticed that the percent sign can't be used in MS-DOS batch files. Is there an alternative way to set the argument?

[EDIT] I figured it out. In batch files, two percent signs are needed to return a literal percent sign; otherwise it's interpreted as one of the batch file's arguments or an environmental variable. Still can't get the command with the -monochrome option to work (the second example).
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

There appears to be a change in the way -monochrome is handled, resulting in a change in that way you need to apply it to dither just the alpha channel of an image.

Stay Turned to the IM examples section you refered to.

You may also like to watch this area, whcih is where I am currently adding things for color quantization, monochroming, and dithering.
http://www.cit.gu.edu.au/~anthony/graph ... ck_n_white
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

If you would PM me when you get done rewriting that section, I would greatly appreciate it.
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Post by SharkD »

I managed to truncate the commands down to this much:

Code: Select all

convert "%1%2" ( -channel A -ordered-dither 2x1 ) ( -compose CopyOpacity -composite ) "%1_new%2"
convert -size 64x128 xc:blue "%1_blue%2"
convert ( "%1_blue%2" "%1_new%2" -compose SrcOver -composite ) -transparent rgb(0,0,255) "%1%2"
del "%1_new%2"
del "%1_blue%2"
Can anyone tell me how to get this all into a single command that is on one line?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

You mentioned having problems with -monochrome, not -ordered dither.
That should not be having any problems.

What version of IM are you using?

Try this

Code: Select all

convert a.png -channel A -ordered-dither    3x3   a_ordered_3x3.gif
PS: why are you overlaying you image onto blue? Why not just do

Code: Select all

convert  image_new   -background blue  -flatten   image_flattened
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply