Page 1 of 1

Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-12T15:11:59-07:00
by Cruor
I need to chunk the image up in 2x3s and then limited these chunks to max two colors from a specified palette.

Here is the (some) of the code im running with the commandline.
This ends up with too many colors in some of these chunks, which is not desired.

Code: Select all

convert test.png -quantize YIQ -dither FloydSteinberg -remap palette.png test_tmp.png
convert test_tmp.png +dither `
    -region 2x3+0+0 -colors 2 +dither -remap palette.png
    -region 2x3+0.0+3.0 -colors 2 +dither -remap palette.png `
    -region 2x3+0.0+6.0 -colors 2 +dither -remap palette.png `
    -region 2x3+0.0+9.0 -colors 2 +dither -remap palette.png `
    *and so on*
    test_tmp.png

Edit, added expected output:

Using code in my second post in this thread.

Example image with valid output
Image
Image

With palette Image

And a image with 20x30 chunks for it to be easier to see how it's supposed to look.
Image

The only problem now is that the command is taking to long to run.

Edit, working and accepted solution

Code: Select all

convert `
  test.png `
  -quantize YIQ -dither FloydSteinberg `
  -remap palette.png +dither `
  -crop 2x3 `
  -colors 2 `
  -remap palette.png `
  -layers merge `
  t5.png
Version: ImageMagick 6.8.7-6 Q16 x64 2013-11-07

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-12T15:36:22-07:00
by snibgo
Cruor wrote:I need to chunk the image up in 2x3s ...
I don't know what that means. Perhaps "-crop 2x3@", do some processing on each, then "-layers merge".
Cruor wrote:... and then limited these chunks to max two colors from a specified palette.
If you know which two colours you want, then make a palette of just those colors.

But perhaps you want to remap to the entire palette, then find the two most commonly used colours, then build a palette with just those two colours, and remap to that. If that's what you want, I don't know a shortcut.

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-12T15:37:43-07:00
by fmw42
Note you have not specified an output image in your command.

try

Code: Select all

convert test_tmp.png -region 2x3+0+0 +dither -colors 2 -remap palette.png +region result.png

But -region only does the top left 2x3 corner of your image. It does not do every 2x3 region.

So like user snibgo, I am puzzled what you are really trying to do.

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-12T15:58:54-07:00
by Cruor
I am aware that region only takes the top left corner, thats why there is duplicates of that line with offsets for the rest of the image (added to code section).
The command i am running has 2500 or so lines, generated by another script.

I am attempting at converting the image to teletext, for printing in a terminal. That means each char is 2x3 pixels, and can only have two colors totall.
The terminal also only supports 16 diffrent colors, which are in the palette.png

Testing at the moment, is using waytoo long to complete, please tell me if this can be optimized, I am not too familiar with this program.

Code: Select all

convert test.png -quantize YIQ -dither FloydSteinberg -remap palette.png -crop 2x3 -colors 2 -dither none -remap palette.png +crop -layers merge test_tmp.png
Edit:
Code is working as intended, but is just taking too long. Taking minutes with 2x3 and a few seconds with 20x30. Any obvious reasons?

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T03:51:31-07:00
by snibgo
Arguments to "-region" should be integer, not floating point.

I assume your palette.png has only 16 pixels? But you are telling IM to re-read this image for each of your 2x3 regions, so IM has to tell the operating system to open a file, read the data, etc.

Instead, you can read it just once, and write that to an "mpr:" memory structure, and read that for each region. Windows BAT syntax, so adjust for your shell.

Code: Select all

convert ^
  palette.png +write mpr:PAL +delete ^
  test.png ^
    -region 2x3+0+0 -colors 2 +dither -remap mpr:PAL ^
    -region 2x3+0+3 -colors 2 +dither -remap mpr:PAL ^
    -region 2x3+0+6 -colors 2 +dither -remap mpr:PAL ^
    -region 2x3+0+9 -colors 2 +dither -remap mpr:PAL ^
  t.png
Instead of using regions in a long script, you could "-crop 2x3" which will create a long list of small images. Then process all the images, then merge them back together. I don't know if that would be faster.

Code: Select all

convert ^
  test.png ^
  -crop 2x3 ^
  -colors 2 +dither -remap palette.png ^
  -layers merge ^
  t.png
The quickest method would involve writing a standalone program, or an IM "-process" module.

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T04:09:50-07:00
by Cruor
Regions can take floats, atleast in the specified version.

The thing is, when i use the crop code, it's extremely slow taking several minutes.
While the region code is near instant, taking at most a few seconds.

Code: Select all

convert test.png -quantize YIQ -dither Riemersma -remap palette.png test_tmp.png
convert test_tmp.png -dither None `
    -region 2x3+0+0 -colors 2 -dither none -remap cc_colors.png +region `
    -region 2x3+0+3 -colors 2 -dither none -remap cc_colors.png +region `
    -region 2x3+0+6 -colors 2 -dither none -remap cc_colors.png +region `
    -region 2x3+0+9 -colors 2 -dither none -remap cc_colors.png +region `
    test_tmp.png
    (shortened for sanity reasons)
But does not give valid output.
Image

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T04:15:02-07:00
by snibgo
Please upload your palette.png

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T04:24:00-07:00
by Cruor
Image
Just a stripe of 16 colors.

Also tried the memory structure for palette, takes about the same time to run (region code).

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T05:19:38-07:00
by snibgo
Here's another way of doing it. Remap the entire image, so every pixel is one of the 16 colours. Then crop it, then reduce each 2x3 image to just two colours, then merge them. It takes about 5 seconds.

Code: Select all

%IM%convert ^
  test.png ^
  +dither -remap palette.png ^
  -crop 2x3 ^
  -colors 2 ^
  -layers merge ^
  t5.png

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T05:34:07-07:00
by Cruor
Modified your code a little bit, and it seems to be working (with similar output as in main post).
(Added dithering and made the image have only palette colors)

Code: Select all

convert `
  test.png `
  -quantize YIQ -dither FloydSteinberg `
  -remap palette.png +dither `
  -crop 2x3 `
  -colors 2 `
  -remap palette.png `
  -layers merge `
  t5.png
Thanks for your help, this is a lot faster than the other crop code i was using.
If there is anyway to make it even faster, please tell me.

Else than that, I think this is the solution for my question.

Re: Dithering a 2x3 region with two best colors from a palette

Posted: 2016-03-13T05:49:28-07:00
by snibgo
Yes, I forgot the second "-remap" is needed. Thanks.