1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
Quantize
========
..
This document covers methods defined in MagickCore's quantize.c file.
https://imagemagick.org/api/MagickCore/quantize_8c.html
.. _kmeans:
Kmeans
------
.. versionadded:: 0.6.4
Reduces the number of colors to a target number. Processing stops if
max number of iterations, or tolerance are reached.
.. code-block:: python
from wand.image import Image
with Image(filename='hummingbird.jpg') as img:
img.kmeans(number_colors=32, max_iterations=100, tolerance=0.01)
img.save(filename='quantize_kmeans.jpg')
+---------------------------------------+-------------------------------------------+
| Original | Quantize Kmeans |
+---------------------------------------+-------------------------------------------+
| .. image:: ../_images/hummingbird.jpg | .. image:: ../_images/quantize_kmeans.jpg |
| :alt: Original | :alt: Quantize Kmeans |
+---------------------------------------+-------------------------------------------+
We can also seed the initial colors used on the first iteration by
defining a semicolon delimited list of colors.
.. code-block:: python
from wand.image import Image
with Image(filename='hummingbird.jpg') as img:
img.artifacts['kmeans:seed-colors'] = 'teal;#586f5f;#4d545c;#617284'
img.kmeans(number_colors=32, max_iterations=100, tolerance=0.01)
img.save(filename='quantize_kmeans_seed.jpg')
+---------------------------------------+------------------------------------------------+
| Original | Quantize Kmeans (seed) |
+---------------------------------------+------------------------------------------------+
| .. image:: ../_images/hummingbird.jpg | .. image:: ../_images/quantize_kmeans_seed.jpg |
| :alt: Original | :alt: Quantize Kmeans (seed) |
+---------------------------------------+------------------------------------------------+
.. note:: Requires ImageMagick-7.0.10-37
.. _posterize:
Posterize
---------
.. versionadded:: 0.5.0
Reduces number of colors per channel. Dither can be defined by passing
``'no'``, ``'riemersma'``, or ``'floyd_steinberg'`` arguments.
.. code-block:: python
from wand.image import Image
with Image(filename='hummingbird.jpg') as img:
img.posterize(levels=16, dither='floyd_steinberg')
img.save(filename='quantize_posterize.jpg')
+---------------------------------------+----------------------------------------------+
| Original | Quantize Posterize |
+---------------------------------------+----------------------------------------------+
| .. image:: ../_images/hummingbird.jpg | .. image:: ../_images/quantize_posterize.jpg |
| :alt: Original | :alt: Quantize Posterize |
+---------------------------------------+----------------------------------------------+
.. _quantize:
Quantize
--------
.. versionadded:: 0.4.2
Analyzes the colors in an image, and replace pixel values from a fixed number
of color.
.. code-block:: python
from wand.image import Image
with Image(filename='hummingbird.jpg') as img:
img.quantize(number_colors=8, colorspace_type='srgb',
treedepth=1, dither=True, measure_error=False)
img.save(filename='quantize_quantize.jpg')
+---------------------------------------+---------------------------------------------+
| Original | Quantize |
+---------------------------------------+---------------------------------------------+
| .. image:: ../_images/hummingbird.jpg | .. image:: ../_images/quantize_quantize.jpg |
| :alt: Original | :alt: Quantize |
+---------------------------------------+---------------------------------------------+
.. _remap:
Remap
-----
.. versionadded:: 0.5.3
Remap replaces all pixels with the closest matching pixel found in the
*affinity* reference image.
.. code-block:: python
from wand.image import Image
with Image(filename='hummingbird.jpg') as img:
with Image(width=256, height=1,
pseudo='gradient:SaddleBrown-LavenderBlush') as amap:
img.remap(affinity=amap, method='riemersma')
img.save(filename='quantize_remap.jpg')
+---------------------------------------+------------------------------------------+
| Original | Quantize Remap |
+---------------------------------------+------------------------------------------+
| .. image:: ../_images/hummingbird.jpg | .. image:: ../_images/quantize_remap.jpg |
| :alt: Original | :alt: Quantize Remap |
+---------------------------------------+------------------------------------------+
|