Cannot make '-replace_ex' option work

Discussions on NConvert - the command line tool for image conversion and manipulation

Moderators: XnTriq, helmut, xnview

Post Reply
HeiWo
Posts: 4
Joined: Wed Nov 11, 2020 2:18 pm

Cannot make '-replace_ex' option work

Post by HeiWo »

Hi,
I try to replace pixel several with '-replace_ex'.
I tried this:

Code: Select all

nconvert.exe -ctype rgb -replace_ex 192 192 192 192 192 192 5 -overwrite "C:\Temp\Example.bmp
But no pixel are replaced.
Is 'replace_ex' not working or what is wrong with my commandline?
Regards,
-HeiWo-
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Can not make '-replace_ex' working

Post by cday »

HeiWo wrote: Wed Nov 11, 2020 2:23 pm Hi,
I try to replace pixel several with '-replace_ex'.
I tried this:

Code: Select all

nconvert.exe -ctype rgb -replace_ex 192 192 192 192 192 192 5 -overwrite "C:\Temp\Example.bmp
But no pixel are replaced.
Is 'replace_ex' not working or what is wrong with my commandline?

This is a recently added option which I haven't used myself:

Code: Select all

-replace r g b r g b : Replace color
-replace_ex r g b r g b tolerance: Replace color

I would expect the usage to be in the following form:

Code: Select all

-replace r g b r g b -replace_ex r g b r g b
Where the -replace term specifies the colour to be replaced, followed by the new colour, and the following -replace_ex term sets a tolerance on the colour to be replaced to allow for the fact that in practice the colour to be replaced may not be a pure colour.

Note: I'm not sure what the '5' in your code after those terms represents, possibly intended as a colour tolerance value, which wouldn't seem to reflect the stated option format?

Edit:

I'm not clear in the absence of further documentation what the tolerance r g b terms are intended to represent, whether they specify two colour values, or whether they are a small numeric +/- value for each colour component.

If necessary, perhaps Pierre could clarify and possibly post example code :?:
HeiWo
Posts: 4
Joined: Wed Nov 11, 2020 2:18 pm

Re: Cannot make '-replace_ex' option work

Post by HeiWo »

Hi,
I understood it the way that the tolerance gives a range in which the RGB is replaced.
I set the 5 to have a tolerance of RGB 187 187 187 up to RGB 197 197 197 to be converted to RGB 192 192 192.
That is what I need. But I'm unsure if my commandline is correct.
Regards,
-HeiWo-
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Cannot make '-replace_ex' option work

Post by cday »

HeiWo wrote: Wed Nov 11, 2020 4:56 pm I understood it the way that the tolerance gives a range in which the RGB is replaced.
I set the 5 to have a tolerance of RGB 187 187 187 up to RGB 197 197 197 to be converted to RGB 192 192 192.
That is what I need. But I'm unsure if my commandline is correct.
I am assuming that both the above terms are necessary, but in the absence of further documentation or tested example code, it isn't entirely clear how the available options are intended to be used... :?:

Perhaps you could test using both terms, in various forms if necessary: if you obtain the result you need please post your code!
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Cannot make '-replace_ex' option work

Post by cday »

A small step forward:

The following code converts the attached pure red 255 0 0 PNG image into a pure green 0 255 0 PNG file :

Code: Select all

NConvert.exe -replace 255 0 0 0 255 0 Red_image.png
I haven't so far managed to obtain a result with a tolerance term added (or alone)... :wink:

XnConvert MP can be used to easily create test images of a specified colour using File > New image... in the viewer, which if needed can be joined to create more complex test images using the browser tool Create > Join images... .

Maybe you can deduce how to use the tolerance term to replace a colour in a real life image?

Note: Testing using a pure colour may not be optimum, in case it exposes a limitation in the new code when a tolerance is added.

Could Pierre please provide a code example...


Red_image.png
Red_image.png (448 Bytes) Viewed 1219 times
User avatar
xnview
Author of XnView
Posts: 43326
Joined: Mon Oct 13, 2003 7:31 am
Location: France
Contact:

Re: Cannot make '-replace_ex' option work

Post by xnview »

cday wrote: Wed Nov 11, 2020 6:52 pm Could Pierre please provide a code example...

Code: Select all

nconvert -replace_ex 255 0 0 0 0 255 5 -o test.png -out png Red_image.png
Pierre.
HeiWo
Posts: 4
Joined: Wed Nov 11, 2020 2:18 pm

Re: Cannot make '-replace_ex' option work

Post by HeiWo »

Hi,
I tried with the attached file and get this:
hw_202011_24.png
C:\Temp\NConvert>nconvert -replace_ex 255 0 0 0 0 255 5 -o Test.png -out png Red_image.png
** NCONVERT v7.60 (c) 1991-2019 Pierre-E Gougelet (Oct 15 2020/10:54:16) **
Version for Windows Xp/Vista/7 x64 (All rights reserved)
** This is freeware software (for non-commercial use)

Error: Can't open file (Red_image.png)

Regards,
-HeiWo-
Attachments
Test.png
Test.png (174 Bytes) Viewed 1176 times
HeiWo
Posts: 4
Joined: Wed Nov 11, 2020 2:18 pm

Re: Cannot make '-replace_ex' option work

Post by HeiWo »

Hi,
Now I get it...
The 'Red_image.png' must exist. After renaming from 'test.png' to 'Red_image.png' it works. The output is a 'Test.png.

Anyhow using:

Code: Select all

nconvert -replace 255 0 0 0 0 255 -o test.png -out png Red_image.png
or

Code: Select all

nconvert -replace_ex 255 0 0 0 0 255 5 -o test.png -out png Red_image.png
gives the exact same result.

In my attached PNG I had used:
RGB 254 0 0 in the upper left corner
RGB 252 0 0 in the upper right corner
RGB 254 0 0 in the lower right corner

For what I want to do and what I understand how it should work:
A tolerance means for me that all RGB 250 0 0 upto RGB 255 0 0 should be changed to RGB 0 0 255.
That would mean for my Test.png that the RGB 245 0 0 (lower right corner) is not changed but all other's are changed.
Or how should it work? Why is there a tolerance what does the tolerance do?

Regards,
-HeiWo-
cday
XnThusiast
Posts: 3973
Joined: Sun Apr 29, 2012 9:45 am
Location: Cheltenham, U.K.

Re: Cannot make '-replace_ex' option work

Post by cday »

My understanding is that the above options examine the colour value (r g b) of every pixel in the source image(s), and change the colour value if the specified colour condition is satisfied.

The first option only tests against the exact colour value specified, the second option tests and if necessary changes the colour of all pixels within the colour range specified when the tolerance is applied...

Note:

If I now understand the above options correctly, there no way to specify individual pixels in an image, all pixels are examined and their colour changed if the specified colour condition is satisfied.
Post Reply