sikuli-driver team mailing list archive
-
sikuli-driver team
-
Mailing list archive
-
Message #59727
Re: [Question #707717]: how to check if image was actually present on disk before using find
Question #707717 on SikuliX changed:
https://answers.launchpad.net/sikuli/+question/707717
castlleapk posted a new comment:
Great question — this is a common issue when using image-based
automation tools (like SikuliX or OpenCV-based scripts). You want to
make sure the image file actually exists on disk before attempting to
run a search like findBest().
Let’s go step-by-step 👇
✅ Problem
When calling:
match = SearchRegion.findBest(name)
If the image (name) isn’t actually present in the filesystem (e.g., missing, wrong path, not checked in to repo), the code might throw confusing errors or silently return None, making it hard to distinguish between:
Image not found on disk, and
Image not matched on screen.
✅ Solution: Check if image exists before searching
You can simply check for the existence of the image file using Python’s
os.path.exists() or Pathlib before calling findBest().
Here’s how you can do it:
import os
def find_image(region, image_path):
if not os.path.exists(image_path):
raise FileNotFoundError(f"Image not found on disk: {image_path}")
match = region.findBest(image_path)
if match is None:
print(f"Image found on disk but not matched on screen: {image_path}")
else:
print(f"Image matched successfully: {image_path}")
return match
Usage:
match = find_image(SearchRegion, "images/button.png")
✅ Alternative (using pathlib):
from pathlib import Path
def find_image(region, image_path):
img = Path(image_path)
if not img.is_file():
raise FileNotFoundError(f"Image file not found: {img.resolve()}")
match = region.findBest(str(img))
return match
✅ Explanation
os.path.exists() or Path.is_file() ensures the image physically exists
on disk.
If not, it raises a clear and early error, like:
FileNotFoundError: Image not found on disk: images/button.png
This prevents confusion between "missing file" vs "no match found."
✅ (Optional) Check multiple possible paths
If your images may live in several directories (like images/,
resources/, or test_images/), you can check all before failing:
def resolve_image_path(image_name, search_paths):
for path in search_paths:
full_path = os.path.join(path, image_name)
if os.path.exists(full_path):
return full_path
raise FileNotFoundError(f"Image '{image_name}' not found in any of: {search_paths}")
# Example usage:
image_path = resolve_image_path("button.png", ["images", "resources", "test_images"])
match = SearchRegion.findBest(image_path)
--
You received this question notification because your team Sikuli Drivers
is an answer contact for SikuliX.