Guide for analyzing chess positions from images and determining optimal moves. This skill should be used when asked to find the best move, checkmate, or tactical solution from a chess board image. It provides structured approaches for image-based chess analysis, piece detection calibration, position validation, and move verification.
This skill provides guidance for analyzing chess positions from images and determining the best move. It covers image processing techniques for detecting pieces, validating detected positions, and verifying candidate moves using chess engines.
Before attempting any chess image analysis:
Before attempting piece detection, calibrate against the actual image:
Sample board square colors
Identify piece color signatures
Check for metadata
Apply structured detection with validation at each step:
Grid detection
Square-by-square analysis
Immediate validation checks
After detection, validate the position is legal:
def validate_position(board):
"""
Validate a detected chess position.
Returns (is_valid, errors) tuple.
"""
errors = []
# Count pieces
white_kings = count_pieces(board, 'K')
black_kings = count_pieces(board, 'k')
white_pieces = count_all_white(board)
black_pieces = count_all_black(board)
# Validation rules
if white_kings != 1:
errors.append(f"Invalid: {white_kings} white kings (must be 1)")
if black_kings != 1:
errors.append(f"Invalid: {black_kings} black kings (must be 1)")
if white_pieces > 16:
errors.append(f"Invalid: {white_pieces} white pieces (max 16)")
if black_pieces > 16:
errors.append(f"Invalid: {black_pieces} black pieces (max 16)")
if white_pieces + black_pieces > 32:
errors.append(f"Invalid: {white_pieces + black_pieces} total pieces (max 32)")
# Check pawn counts
white_pawns = count_pieces(board, 'P')
black_pawns = count_pieces(board, 'p')
if white_pawns > 8:
errors.append(f"Invalid: {white_pawns} white pawns (max 8)")
if black_pawns > 8:
errors.append(f"Invalid: {black_pawns} black pawns (max 8)")
return len(errors) == 0, errors
Once position is validated, analyze for best move:
Use a chess engine
Consider the task requirements
Verify the move
| Symptom | Likely Cause | Solution |
|---|---|---|
| Too many pieces detected (>32) | Detecting board squares as pieces | Recalibrate piece color thresholds |
| All light squares detected as white pieces | Threshold too low | Sample actual piece colors, not square colors |
| Inconsistent results across runs | Arbitrary threshold values | Use calibrated values from actual image |
| Wrong piece types | Color confusion | Separate detection of color vs piece type |
Before submitting a solution, verify:
If position cannot be reliably detected:
references/chess_image_analysis.md - Detailed guide for chess image processing techniquesscripts/validate_position.py - Position validation utility