Page 1 of 1
How to find defined colours?
Posted: Tue Mar 18, 2025 4:38 pm
by Peter2
Hi
I'm working on CAD drawings, using coloured areas. And sometimes there is the condition like this:
"when everything is OK there should be no red area in the output"
I can not check it on CAD site, because the "output" is a printed result of overlaying different information. So my idea is to print to file (PDF, bitmap, HPGL) and start an analysis of the image:
"Search for red pixels AND show me where the are (zoom to it or something else ...)"
Any ideas "if" and "how" this could be done?
Re: How to find defined colours?
Posted: Tue Mar 18, 2025 5:36 pm
by cday
Interesting problem, you presumably can't post a saved image, or possibly even a small cropped area that could be used to test ideas?
If you open a saved image in XnView MP and look at '
Image > Count colors used' is the result a small number indicating a small number of pure colours, or a large number?
XnTriq likes a challenge too...

Re: How to find defined colours?
Posted: Tue Mar 18, 2025 6:38 pm
by Peter2
Hi cday
here is a rather simple example - 5 colours and a red area big enough to see. But in reality it could be a little bit more difficult, but similar to this.
So - where is the red colour?
Re: How to find defined colours?
Posted: Tue Mar 18, 2025 7:06 pm
by user0
use
python
AI can improve it, eg print coordinates of shapes of red color from png files in d:\test folder
Code: Select all
from pathlib import Path
from PIL import Image
import numpy as np
from scipy.spatial import ConvexHull
from scipy.ndimage import label
# Define the folder containing PNG images
folder_path = Path(r"D:\test")
# Define the target color (R, G, B) - Strict match only
target_color = (255, 0, 0) # Example: Pure red
def get_color_mask(image, target_color):
"""Returns a binary mask where pixels match the exact target color."""
return (
(image[:, :, 0] == target_color[0]) &
(image[:, :, 1] == target_color[1]) &
(image[:, :, 2] == target_color[2])
)
def get_corner_coordinates(coords):
"""Returns the corner coordinates using ConvexHull if possible."""
if len(coords) >= 3:
hull = ConvexHull(coords)
return coords[hull.vertices].tolist()
return coords.tolist()
def process_image(image_path, target_color):
"""Processes a single image and returns formatted shape details."""
image = np.array(Image.open(image_path).convert("RGB"))
color_mask = get_color_mask(image, target_color)
labeled_array, num_shapes = label(color_mask)
shape_details = []
for shape_num in range(1, num_shapes + 1):
shape_coords = np.column_stack(np.where(labeled_array == shape_num))
corner_points = get_corner_coordinates(shape_coords)
shape_details.append(f"Shape {shape_num}: {corner_points}")
return num_shapes, shape_details
# Process all PNG images in the folder
for image_path in folder_path.glob("*.png"):
num_shapes, shape_details = process_image(image_path, target_color)
if shape_details:
print(f"File: {image_path.name} | Shapes: {num_shapes} | {' | '.join(shape_details)}")
File: where_is_red_area.png | Shapes: 1 | Shape 1: [[2576, 5095], [2570, 5095], [2568, 5094], [2563, 5089], [2560, 5083], [2549, 5060], [2555, 5057], [2586, 5043], [2587, 5043], [2588, 5044], [2590, 5047], [2605, 5071], [2607, 5075], [2607, 5076], [2606, 5077], [2603, 5079], [2593, 5085]]
Re: How to find defined colours?
Posted: Tue Mar 18, 2025 7:15 pm
by XnTriq
cday wrote: Tue Mar 18, 2025 5:36 pmXnTriq likes a challenge too...

Inkscape:
File →
Document Resources →
Colors
This is just a first idea.
I'm still looking for better solutions.
PS: I missed user0's post!
Re: How to find defined colours?
Posted: Tue Mar 18, 2025 7:30 pm
by cday
Peter2 wrote: Tue Mar 18, 2025 4:38 pm
I'm working on CAD drawings, using coloured areas. And sometimes there is the condition like this:
"when everything is OK there should be no red area in the output"/home/chris/Downloads/where_is_red_area.png
Your posted image has five colours, which seems a good start although the small number of colours may or may not actually be useful.
Trying a few ideas quickly, if you convert the test image to binary using
Image > Change color depth... the red area becomes black, which as it is the only black area should allow any remaining red areas to be identified easily: when there are no remaining red areas your mission is complete. That does assume, though, that other images do not have additional colours that might affect that simple result.
I cannot check it on CAD site, because the "output" is a printed result of overlaying different information. So my idea is to print to file (PDF, bitmap, HPGL) and start an analysis of the image:
"Search for red pixels AND show me where the are (zoom to it or something else ..."
If you envisage automatically identifying remaining red areas and providing, for example, coordinates for them, that would be a significantly more complex aspiration well beyond the capabilities of XnView MP alone! However, a limited 'workaround' if needed might be to crop out areas of the image (maybe automating that using the browser option
Create > Split image(s)...) and then convert those to binary to make any red areas easy to identify.
Edit: Yes, look at user0's more advanced suggestion above...

Re: How to find defined colours?
Posted: Tue Mar 18, 2025 8:56 pm
by Peter2
Yes, I think too that this is beyond the features of XnViewMP, so I posted it under "Miscellaneous".
The code of user0 looks fine; maybe he want's to make an exe or a plugin from it

? Or Pierre integrates it?
Re: How to find defined colours?
Posted: Tue Mar 18, 2025 9:27 pm
by cday
For completeness, here is your image posted above converted to binary, the single small red area clearly visible as the only 'area' rather than line:

- where_is_red_area_binary.png (112.67 KiB) Viewed 602 times
Whether that could be a simple way of identifying red areas in the general case, and particularly later that no red areas remain, is for you to judge.
Re: How to find defined colours?
Posted: Wed Mar 19, 2025 1:00 am
by XnTriq
If you're working with raster (instead of vector) graphics:
- Optimize where_is_red_area.png with PngOptimizer.
- Open the result in XnView MP.
- Go to Image → Edit colormap… and change all colors except red to white (#ffffff).
- Optimize the result with PngOptimizer.
Re: How to find defined colours?
Posted: Wed Mar 19, 2025 3:00 am
by XnTriq
Re: How to find defined colours?
Posted: Wed Mar 19, 2025 7:54 am
by Peter2
Good morning, everybody
@cday and "binary":
I could reproduce the result, but I don't understand the behaviour in detail. I think in another, more complex drawing with other dark colours this would fail or gives unprecise results - correct?
@XnTriq and color palette:
Fine, precise results. A way that I will use the next time - until the EXE, made by some White Knight

- based on user0's code is ready to use.
Let's see if there are still other ideas...
Re: How to find defined colours?
Posted: Wed Mar 19, 2025 9:28 am
by cday
Good morning Peter!
Peter2 wrote: Wed Mar 19, 2025 7:54 am
@cday and "binary":
I could reproduce the result, but I don't understand the behaviour in detail. I think in another, more complex drawing with other dark colours this would fail or gives imprecise results - correct?
A basic problem remains that the overall problem isn't defined fully yet: in this aspect in terms of the possible colours present in the images to be processed. Given the stated need to easily identify remaining red areas, I experimented briefly with dropping out selected colours, then noticed that simply converting the posted image to grayscale achieved the desired result, in so far as all colour 'areas' in the posted image other than red were deleted leaving only empty rectangles. It is true that the original red areas were easy to identify but that might not always be the case.
It seems very likely that the original drawings are created and edited using vector graphic software, but ultimately that doesn't necessarily preclude using a bitmap export of the current state of the drawing to be used for the identification of any remaining red areas, if it has an advantage. I was only responding to the original stated need as I interpreted it.
Re: How to find defined colours?
Posted: Wed Mar 19, 2025 6:45 pm
by XnTriq
I use CorelDraw X6 (2013) for editing vector graphics.
It has an
object manager docker I can't do without.
This seems to be missing in Inkscape

Re: How to find defined colours?
Posted: Wed Mar 19, 2025 10:00 pm
by XnTriq
As it turns out, Inkscape does have a Layers and Objects docker, but it's less detailed than CorelDraw's Objects Manager.