Ordered dithering with a set amount of colors?

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?".
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Ordered dithering with a set amount of colors?

Post by plshelpx_x »

I'm aware that I can use "+dither -colors x" and get an image with x colors that is as close to the original image's colors as possible. But I can't, for example, use "-ordered-dither o8x8 -colors x" and achieve the same effects. It appears to me that "-ordered-dither" use 'posterized' color maps, and does not take account in the original image's palette, and while I can change the posterize level, I can't create an ordered dither that picks colors based on the current image contents.

If there's any way to achieve my goal please tell me, that would be extremely helpful.

*Edit: On that note, can I also ask why "-quantize transparent" doesn't work anymore? And how can I replicate its effect using a different set of commands?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ordered dithering with a set amount of colors?

Post by fmw42 »

plshelpx_x wrote: 2017-02-25T04:30:15-07:00 *Edit: On that note, can I also ask why "-quantize transparent" doesn't work anymore? And how can I replicate its effect using a different set of commands?
I posted a bug report on this.
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Re: Ordered dithering with a set amount of colors?

Post by plshelpx_x »

That's great to hear! I was hoping for a work around for the time being, however.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ordered dithering with a set amount of colors?

Post by fmw42 »

Does this do what you want? Example from Anthony's pages on Usage.

Code: Select all

convert alpha_gradient.png -alpha off +dither -colors 15 -alpha on alpha_colors_15qt.tif
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Re: Ordered dithering with a set amount of colors?

Post by plshelpx_x »

No unfortunately. The "-alpha on" at the end doesn't seem to do anything, but thanks for trying!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ordered dithering with a set amount of colors?

Post by fmw42 »

try this

Unix syntax

Code: Select all

convert alpha_gradient.png \
\( -clone 0 -alpha extract \) \
\( -clone 0 -alpha off +dither -colors 15 \) \
-delete 0 +swap -alpha off -compose copy_opacity -composite \
alpha_colors_15qt.png
Windows syntax

Code: Select all

convert alpha_gradient.png ^
( -clone 0 -alpha extract ) ^
( -clone 0 -alpha off +dither -colors 15 ) ^
-delete 0 +swap -alpha off -compose copy_opacity -composite ^
alpha_colors_15qt.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ordered dithering with a set amount of colors?

Post by fmw42 »

Or this (Unix syntax)

Code: Select all

convert alpha_gradient.png -write mpr:img +delete \
\( mpr:img -alpha off +dither -colors 15 \) \
\( mpr:img -alpha extract \) \
-alpha off -compose copy_opacity -composite \
alpha_colors_15qt.png
For Windows change the line ending \ to ^ and replace \( ... \) with ( ... )
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Re: Ordered dithering with a set amount of colors?

Post by plshelpx_x »

That does the trick! Thank you!
Please tell me dithering is still something that's being worked on, though. I understand it's very niche but it was what brought me to IM and I've since found IM to be extremely useful!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Ordered dithering with a set amount of colors?

Post by snibgo »

plshelpx_x wrote:It appears to me that "-ordered-dither" use 'posterized' color maps, ...
That is correct. Each channel is treated separately. The output channel value is one of a number of levels, according to the threshold map and the (x,y) coordinate. The code in OrderedPosterizeImageChannel() is fairly simple, merely calculating the output value.

I can't see how an ordered dither would work with a pre-built palette of colours, such as the N "best" colours.
snibgo's IM pages: im.snibgo.com
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Re: Ordered dithering with a set amount of colors?

Post by plshelpx_x »

That is a huge bummer. But I guess it's just me being ignorant of how it works internally.

If there is even a slightest chance of "an ordered dither with a pre-built palette of colours" being realized, do you have any suggestions of how it might be done?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Ordered dithering with a set amount of colors?

Post by snibgo »

Well, I don't know what the output would be. I don't even know how the ordered dither map might be specified.

Dither maps are currently specified (in thresholds.xml) as levels, ie each pixel in the map has a one-dimensional threshold, a single number. This is applied to each channel in turn.

I can't see how those one-dimensional levels might apply to palette of colours that are each three-dimensional.

Perhaps you can describe exactly what you want, or something.
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: Ordered dithering with a set amount of colors?

Post by fmw42 »

I do not know if this is meaningful, but you could do the ordered dither, then -remap with fixed set of colors. Or the other way around?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Ordered dithering with a set amount of colors?

Post by snibgo »

There are many possibilities. For example, ordered dither, and remap either to a small a "-colors N" selection or the unique colours from the input image. Windows BAT syntax:

Input: toes.jpg
Image

Code: Select all

%IM%convert ^
  toes.jpg ^
  ( +clone +dither -colors 5 -unique-colors ^
    +write mpr:MAP +delete ^
  ) ^
  -ordered-dither o8x8,3 -remap mpr:MAP ^
  orddith1.jpg
orddith1.jpg:
Image

Code: Select all

%IM%convert ^
  toes.jpg ^
  ( +clone +dither -unique-colors ^
    +write mpr:MAP +delete ^
  ) ^
  -ordered-dither o8x8,3 -remap mpr:MAP ^
  orddith2.jpg
orddith2.jpg:
Image
snibgo's IM pages: im.snibgo.com
plshelpx_x
Posts: 7
Joined: 2017-02-12T08:57:40-07:00
Authentication code: 1151

Re: Ordered dithering with a set amount of colors?

Post by plshelpx_x »

snibgo wrote: 2017-02-25T15:05:19-07:00 Perhaps you can describe exactly what you want, or something.
I do not know what I want now, after realizing it's been math all along and not some black magic that turns colors into a fun looking dots pattern. I just thought -dither and -ordered-dither are similar at the core and are only different in executions, like -dither none and -dither Riemersma, but turns out they are different beasts altogether.
fmw42 wrote: 2017-02-25T15:18:41-07:00 I do not know if this is meaningful, but you could do the ordered dither, then -remap with fixed set of colors. Or the other way around?
But -remap basically means -dither Riemersma -remap, right? And -ordered-dither means -ordered-dither -posterize 2 (unless the posterize level is specified otherwise). So by using them in conjunction with one another I'm dithering twice, or changing the existing colors with a different set, which shouldn't give me a satisfactory result.

I guess what I want is the math behind -dither, but executed like an -ordered-dither? But I can't imagine it'd be able to work now that I've been explained by snibgo that it just does not roll that way.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Ordered dithering with a set amount of colors?

Post by fmw42 »

-remap can use -dither none, so that it adds no new dithering. Thus do something like snibgo suggested but use remap to limit the number of colors to those in the remap colortable image

see http://www.imagemagick.org/Usage/quantize/#remap
Post Reply