# Image Quantisation

**URL:** <https://forums.solar2d.com/t/image-quantisation/356083>\
**Category:** Advanced Graphics\
**Created:** [October 24, 2023, 11:13am UTC](https://forums.solar2d.com/t/image-quantisation/356083 "2023-10-24T11:13:07Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 24, 2023, 11:13am UTC](https://forums.solar2d.com/t/image-quantisation/356083/1 "2023-10-24T11:13:07Z")

</div>

Hi,

I am looking for a compatible image quantiser for solar2d. An algorithm that reduces the number of colours in an image from say 5000 colours to 255 colours. I had a look at impack and I don’t think it has anything implemented. Before I start the process of coding one up just wanted to check there isn’t a plugin with one already done.

Thanks  
David

---

<div class="post-metadata">

**Author:** ![kakula](https://forums.solar2d.com/user_avatar/forums.solar2d.com/kakula/32/418_2.png) [@kakula](https://forums.solar2d.com/u/kakula)\
**Post date:** [October 24, 2023, 1:01pm UTC](https://forums.solar2d.com/t/image-quantisation/356083/2 "2023-10-24T13:01:04Z")

</div>

Just a question … what is the main purpose of that? is it just to reduce image size, or something else

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 24, 2023, 1:50pm UTC](https://forums.solar2d.com/t/image-quantisation/356083/3 "2023-10-24T13:50:30Z")

</div>

Hi kakula… Image Quantisation reduces the number of colours in a image with the minimum perceptual. difference to the viewer. So for instance if an image has 5000 colours after quantisation say to 255 colours the image is close to the original. Used in most image processing packages GIMP, Paint shop pro etc its purpose is in this instance is to reduce the vectorisation overhead in my app PAINTIFY

see this in wikipedia for more details

> **[Color quantization](https://en.wikipedia.org/wiki/Color_quantization)**
>
> In computer graphics, color quantization or color image quantization is quantization applied to color spaces; it is a process that reduces the number of distinct colors used in an image, usually with the intention that the new image should be as visually similar as possible to the original image. Computer algorithms to perform color quantization on bitmaps have been studied since the 1970s. Color quantization is critical for displaying images with many colors on devices that can only display a li...

Hopefully there is a plugin that can do it although it will be strange too write something just for this image processing function … hopefully someone will know

Thanks

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 24, 2023, 2:52pm UTC](https://forums.solar2d.com/t/image-quantisation/356083/4 "2023-10-24T14:52:36Z")

</div>

Okay looks like I have solved it …

I missed the posterise filter in filter and effects… basically a image quantiser using the GPU, I think… certainly good enough for what I need… super fast… just choose the bits per channel you want and it quantises an image in a heart beat to the colour level … Well Solar2d knew you would be able to do it … onwards …

Topic closed

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 24, 2023, 3:58pm UTC](https://forums.solar2d.com/t/image-quantisation/356083/5 "2023-10-24T15:58:38Z")

</div>

Actually not totally solved … does anybody know what colorsperchannel actually does in the posterise filter as it gives different results based on the input image ?

Thanks  
David

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 25, 2023, 6:42am UTC](https://forums.solar2d.com/t/image-quantisation/356083/6 "2023-10-25T06:42:53Z")

</div>

That will divvy up each channel into regions, although with 256 you wouldn’t notice any differents. So say with 8, values like from 0-.124999… would snap to 0, from .125 to .24999… to .125, etc.

An effect wouldn’t really be able to do this since it can only write one output per pixel, and it would be expensive to search many neighbors.

Didn’t have anything specifically dedicated to this in Impack, no, since I was largely just exposing other APIs. I believe GIFs do in fact do this, though, so it might be possible to load an image, save it out to a GIF (in memory), then reload it.

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 25, 2023, 7:31am UTC](https://forums.solar2d.com/t/image-quantisation/356083/7 "2023-10-25T07:31:12Z")

</div>

Thanks StarCrunch … Thats a great idea about gifs I hadn’t thought of that … very innovative. The operation is a one off this at the start of processing so that will work great , I will give it a go and check it works okay.

On the posterise function, I am still a little unclear about coloursperchannel… are the channels r,g,b,a or r,g,b… what are the values 12499 and 24999 in your description ?

Many thanks for the GIF idea  
David

---

<div class="post-metadata">

**Author:** ![StarCrunch](https://forums.solar2d.com/user_avatar/forums.solar2d.com/starcrunch/32/67_2.png) [@StarCrunch](https://forums.solar2d.com/u/StarCrunch)\
**Post date:** [October 25, 2023, 7:59am UTC](https://forums.solar2d.com/t/image-quantisation/356083/8 "2023-10-25T07:59:59Z")

</div>

I mentioned 8 as the choice, so in this case I was just listing off the decimal values of 1/8 (or almost, in the …999 cases), 2/8, and so on.

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 25, 2023, 8:50am UTC](https://forums.solar2d.com/t/image-quantisation/356083/9 "2023-10-25T08:50:15Z")

</div>

Sorry being thick, didn’t see the tiny decimal point , need glasses, so the posterise just quantises each r,g,b,a channel in decimal form (0-1) by what ever value of colorsperchannel you choose

So if I want an image with 255 unique colours , what value of colorsperchannel do I use ? is that dependent on the image being processed.

Many Thanks  
Just trying GIF option

---

<div class="post-metadata">

**Author:** ![DRF.MGBV8](https://forums.solar2d.com/user_avatar/forums.solar2d.com/drf.mgbv8/32/1859_2.png) [@DRF.MGBV8](https://forums.solar2d.com/u/DRF.MGBV8)\
**Post date:** [October 25, 2023, 2:54pm UTC](https://forums.solar2d.com/t/image-quantisation/356083/10 "2023-10-25T14:54:45Z")

</div>

Just an update , finished coding the GIF idea all works well and is quite fast for 255 image quantisation. Couldn’t get memory GIF swap working so used a file instead. Typical example attached

![Q2](https://forums.solar2d.com/uploads/default/original/2X/b/b64325b12ec612ebac4a7570c4db3fc3121a9685.gif)

255 Unique Colour Image Quantised Result shown above

![rose](https://forums.solar2d.com/uploads/default/original/2X/e/ee825d338d221522dbac6f6e3a1df734b144733c.png)

Original image 3400 Unique Colours shown above

Happy to share code if anyone else needs it in the future, will bung it in a lua library. Thanks to impack …a great plugin !!!  
Onwards
