Skip to content

Instantly share code, notes, and snippets.

@pardeike
Created December 21, 2024 18:32
Show Gist options
  • Save pardeike/8588df248ce3d8afb12b342bf70601bf to your computer and use it in GitHub Desktop.
Save pardeike/8588df248ce3d8afb12b342bf70601bf to your computer and use it in GitHub Desktop.
# Calculate the dimensions of each square
image_width, image_height = image.size
num_squares = 6 # 6x6 grid
square_width = image_width // num_squares
square_height = image_height // num_squares
# Function to calculate average color of a region
def average_color(region):
return np.mean(region.reshape(-1, 3), axis=0)
# Initialize list to store average colors of all squares
average_colors = []
# Loop through the 6x6 grid of squares
for row in range(num_squares):
for col in range(num_squares):
# Calculate the bounding box for each square
x_start = col * square_width
y_start = row * square_height
x_end = x_start + square_width
y_end = y_start + square_height
# Extract the square region
square_region = image_array[y_start:y_end, x_start:x_end]
# Calculate the average color of the square
avg_color = average_color(square_region)
average_colors.append(avg_color)
# Convert the list of average colors to a numpy array
average_colors = np.array(average_colors)
# Calculate the Euclidean distance of each square's color to the overall mean color
mean_color = np.mean(average_colors, axis=0)
distances = np.linalg.norm(average_colors - mean_color, axis=1)
# Find the index of the square with the maximum distance
different_square_index = np.argmax(distances)
different_square_index
The square that differs the most in color is at index 5 (in reading order, starting from 0). 
I pressed it and it wasn’t it.
The refined analysis still identifies the square at index 5 as the most different in color. If this result is still incorrect, it might require a visual representation of the distances or further verification of the grid alignment. Would you like me to visualize the differences for clarity? 
@pardeike
Copy link
Author

IMG_1562

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment