How does one batch substitute colors based on criteria?

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
PandoraBox
Posts: 23
Joined: 2011-04-10T14:08:11-07:00
Authentication code: 8675308

How does one batch substitute colors based on criteria?

Post by PandoraBox »

I'm stumped on this one as I really hate doing a for loop in dos to check all 16 million colors on a image.

What I am trying to do is for example if I have the color 237,23,23 I want to have 255,0,0.

The common rule is that if the color is 127 or more set to 255 if not set to 0.

To end up with only the possible colors 0,0,0 0,0,255 0,255,0 0,255,255 255,0,0 255,0,255 255,255,0 255,255,255.

currently I could split the channels and re-merge them once I've done my color loop which would reduce this to 768 steps however as far as I know or I just missed the information in regarding saving under the same file name.

Any help would be greatly appreciated.

This is what I'm currently using but it's a pain

convert image.png -fill Rgb(0,0,0) -opaque rgb(15,16,12) imageout.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How does one batch substitute colors based on criteria?

Post by snibgo »

"-posterize 2", although I think the cut-off will be that more than 50% (128 or greater) will become maximim (eg 255).
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: How does one batch substitute colors based on criteria?

Post by fmw42 »

"-separate -threshold 49.803% -combine" should work to get you your threshold at 127, but it still needs to be done in a loop. mogrify does not work by my testing.

Code: Select all

convert xc:"rgb(127,23,23)" -separate -threshold 49.803% -combine txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (100%,0%,0%)  #FFFF00000000  red

Code: Select all

convert xc:"rgb(126,23,23)" -separate -threshold 49.803% -combine txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (0%,0%,0%)  #000000000000  black
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How does one batch substitute colors based on criteria?

Post by fmw42 »

This also work with convert but not mogrify.

Code: Select all

convert xc:"rgb(127,23,23)" -channel rgb -threshold 49.803% +channel txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (100%,0%,0%)  #FFFF00000000  red

Code: Select all

convert xc:"rgb(126,23,23)" -channel rgb -threshold 49.803% +channel txt:
# ImageMagick pixel enumeration: 1,1,65535,srgb
0,0: (0%,0%,0%)  #000000000000  black
PandoraBox
Posts: 23
Joined: 2011-04-10T14:08:11-07:00
Authentication code: 8675308

Re: How does one batch substitute colors based on criteria?

Post by PandoraBox »

The posterize command worked great that was exactly what I was looking for.

The reason why I needed those primary colors is that I am using a tool called PaperBa(c)k http://www.ollydbg.de/Paperbak/index.html and I was wishing to modify this code to work with colored bar codes the issue was no scanner is perfect in giving accurate colors to be able to re-split the channels to process the data.

And you both we're right it was 128 the cutting point I needed.

Well thanks you both for the quick reply once I get this source code to work with the colors properly I'll gladly donate it to who ever wants it.
Post Reply