Convert to (nice) indexed

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?".
Post Reply
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Convert to (nice) indexed

Post by tomse »

I have run into an issue that I don't know how to properly handle.

I'm trying to convert images into indexed (3 colour) using a ref image.

Here is the before image:
Image

The output of using my reference image (which is still 24 bit)
Image

And this is what I want (using an image very similar to this as reference image) (ofc I understand it can't be as clean as this)
Image

this is the interesting part of my script - this is taken from an example from I don't remember where - most likely in this forum

Code: Select all

convert input.tif -separate -threshold 75% -combine -remap "..\3_colour_index_ref.tif" indexed\output.tif
1. How do I output to X amount of colours only? (this outputs 24bit)
2. Is there a way to do what I want?
2.1 I expect that the green/purple colours needs to be mapped to black
2.2 I also expect that the blue range should be remapped to my targeted blue colour.

This is a script that I'll be using for quite many scanned pages, so it'll definately save me a lot of days compared to the manual labour for each page.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert to (nice) indexed

Post by fmw42 »

Please always provide your IM version and platform, since syntax may differ. What are the 3 colors you want to map to? Have you seen -depth at http://www.imagemagick.org/script/comma ... .php#depth. -depth 2 will have only 4 colors. No way I know to specify 3 colors only.
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Re: Convert to (nice) indexed

Post by tomse »

Running ImageMagick on Windows platform.
I don't remember which version right now but I'd say it's around 6 months old or so, I'll have to check up on that.

4 or 8 colours can do as well as long as the amount isn't high (to keep file sizes small)
I'll take a look at -depth 2

The colours that I want use are shown in the last pic.
I'm doing this by hand in GIMP today, but I'd really like to automate this.

And I'd like to thank the team for creating this awesome tool, it saves me a lot of manual labour already.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert to (nice) indexed

Post by snibgo »

Your third image contains the colours you want? Then just remap the first to the third, without dithering.

If you are remapping many inputs, it would be quicker to make a simpler version of the map first, like this:

Code: Select all

convert 0iCxtUz.png -unique-colors 3col.png

convert mRAOuXU.png +dither -remap 3col.png out.png
3col.png contains the same colours as your third image. It has only 3 pixels. The second command remaps with just those 3 colours.
snibgo's IM pages: im.snibgo.com
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Re: Convert to (nice) indexed

Post by tomse »

Thank you, I'll try this tonight as well.

I use the reference image "3col.png" as-is, is that bad? or should it just be the 3px with the 3 colour palette?
Which impact does it give other than the file is larger and loading it takes a little longer than with a smaller file?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert to (nice) indexed

Post by snibgo »

Your "reference" image is mostly black and white, with a few pixels of saturated colours. I don't know what it is for, or what you want to do with it. It contains 8 colours, but you want only three.

If you are remapping many images using the same map, then using the smallest map (ie without duplicate colours) will save processing time. If the file is larger than it needs to be, is will take longer to read from disk, and to decompress, and to analyse. The analysis time is proportional to W*H*L, width * height * (number of levels in the analysis tree).

Of course, reducing it to unique colours also takes time, but if that is needed once for 1000 input images, it would be worth doing.
snibgo's IM pages: im.snibgo.com
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Re: Convert to (nice) indexed

Post by tomse »

Perhaps you're looking at the middle image? which is the result I get by using the script mentioned.
the last pic which only contains 3 colours is my ref pic, or a pic similar atleast.
(sorry I'm a little confused why you say my ref image is saturated and 8 colours)


But you're right in that I have a bunch of images in 24bit colours, which I want to use a script file to loop through. Using the same reference image of course.

Time isn't of the essence, the end result is.. and preferably have the computer do it rather than myself doing it manually ;-)
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Re: Convert to (nice) indexed

Post by tomse »

version info:
Version: ImageMagick 6.9.0-3 Q16 x64 2014-12-30 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates (built-in): bzlib cairo freetype jbig jng jp2 jpeg lcms lqr openexr pangocairo png ps rsvg tiff webp xml zlib

And now I just upgraded to latest (6.9.3-2)

snibgo: using
<code>convert mRAOuXU.png +dither -remap 3col.png out.tif</code>
results in a corrupt tif file. using png instead seems to work.

I tried

Code: Select all

convert 0iCxtUz.png -unique-colors 3col.png
convert mRAOuXU.png +dither -remap 3col.png out.png
Which gave this result (ouch)
Image


using the 3 colour ref image (1x3 px)

Code: Select all

convert mRAOuXU.png +dither -remap 3col.png out.png
Image

much better thanks.. but notice the blue outline of the black letters.. can anything be done automagically here?
otherwise.. your parameters has really improved my output images, and you've just saved me hours of manual labour.
Thank you so much!!!


(as a side note the source images are tif, but that shouldn't affect the outcome)
Last edited by tomse on 2016-02-03T14:30:43-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert to (nice) indexed

Post by fmw42 »

One problem is that your input image is antialiased around the edges. When you convert that to 3 colors, you get jagged edges, since some parts of the edges get one color and other parts get another color. It is hard to get smooth results when you reduce colors. You would have to add a little blur to the result to antialias again, but then you would get more than 3 colors in the result. You could try a little morhology open or close or smooth and see if that helps. See http://www.imagemagick.org/Usage/morphology/#basic
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert to (nice) indexed

Post by snibgo »

As Fred says. The black letters had gray outlines. The closest colour to gray, given a choice of (white, blue, black) is blue.

If you don't want IM to choose the closest colour, you need to do some pre-processing. What do you want instead of blue? Perhaps black, or white. Applying a gamma in one direction or the other will influence the choice.
snibgo's IM pages: im.snibgo.com
tomse
Posts: 6
Joined: 2016-01-25T04:34:43-07:00
Authentication code: 1151

Re: Convert to (nice) indexed

Post by tomse »

@snibgo I didn't see these 2 responses before but edited my prior post..
But you saved me hours of work (just on this set of images I'm working on right now) so thank you very very much!.

@fmv42 The antialiasing green/purple are sideeffects (misalignment?) of the optics of an older document scanner, of course these would have been antialiased gray tones if it was proper. and eventually end up in either black or white, but as they're green/purple I think imagemagick would choose these colours to be blue using the 3 colour index only.

the jagged edges won't be seen on the output due to high resolution and the size the output sould be viewed in.
the scans are pages from manuals, so the images you see above are zoomed in way more than it should be seen, but is so you could see my problem(s).

Limiting the amount of colours also limit the size of the digital manual, also it makes it look much better.
Adding more colours to 200-400 pages


I could of course go with black and white only and stll get a good result.. but I'd like to keep things as close to the original as possible, but keeping a small filesize as well.

I'm very satisfied with the endresult of the manual (which I just finished now that the result of my tests were much better than expected).

Just for your fun:
The manual has 172 pages scanned in 24bit 600dpi
the raw pages in TIF LZH compressed : 5.9GB - frontpage: 39meg - avg size pr page: ~34megs
After doing some saturation/white balancing: 2.5GB - frontpage: 35meg - avg size pr page: ~14megs
doing the last step, 3 colours (except frontpage): 32megs - frontpage (256colour indexed): 5.5megs - avg size pr page: 190kb

Finished PDF 39Megs.

Calculated avg time spent pr page if this was done by hand: 30-40 sec pr page (maybe 15-20 for some pages, but loading times and crashes etc are calculated into the 30-40 sec pr page)
approx 1½-2 hours would it have taken me to do this script in gimp.
Actual run time of script: 5 min
Post Reply