Created
April 4, 2023 23:14
-
-
Save tslater2006/902a46ad7f9172b4862b4d46927e9b29 to your computer and use it in GitHub Desktop.
Extracts vallejo colors from their released color charts and puts into Real Color Mixer JSON format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import pytesseract | |
from pytesseract import Output | |
import numpy as np | |
import re | |
import json | |
# Create a JSON object with a JSON array | |
palette = { | |
"name": "Vallejo Panzer Aces", | |
"colors": [ | |
], | |
"colorsToMix": [] | |
} | |
# Load the image | |
# image = cv2.imread('CC070-Model_Color-Rev18-baja-2.png') | |
# image = cv2.imread('CC064-MechaColor-Rev03-baja-1.png') | |
# image = cv2.imread('CC072-Game_Color-Rev20-baja-1.png') | |
# image = cv2.imread('CC082-Game_Air-Rev05-baja-2.png') | |
image = cv2.imread('CC077-Panzer_Aces-Rev12-baja-1.png') | |
# Convert the image to grayscale | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
# Apply adaptive thresholding to the grayscale image | |
thresh = cv2.adaptiveThreshold( | |
gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) | |
# Find contours in the thresholded image | |
contours, _ = cv2.findContours( | |
thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) | |
# Set pytesseract configuration | |
custom_config = r'--oem 3 --psm 6' | |
# Iterate through the contours | |
count = 0 | |
named = 0 | |
for contour in contours: | |
# Get the bounding rectangle for each contour | |
x, y, w, h = cv2.boundingRect(contour) | |
# Check if the contour is likely to be a rectangle based on its aspect ratio | |
aspect_ratio = float(w) / h | |
if w > 80 and h > 10: | |
# Extract the color region | |
color_region = image[y + 5:y + h - 10, x + 5:x + w - 10] | |
# Calculate the average BGR values of the color inside the rectangle | |
avg_color_bgr = np.mean( | |
np.mean(color_region, axis=0), axis=0).astype(int) | |
# Convert the average color to RGB format | |
avg_color_rgb = np.flip(avg_color_bgr).tolist() | |
print(f"Average RGB values: {avg_color_rgb}") | |
# Extract the text region | |
text_region = gray[y + h + 3:y + h + int(1 * h) + 3, x:x + w] | |
# Recognize the text using pytesseract | |
text = pytesseract.image_to_string( | |
text_region, config=custom_config).strip() | |
text_lines = text.splitlines() | |
while ("" in text_lines): | |
text_lines.remove("") | |
if (len(text_lines) > 1): | |
# Print the recognized text | |
color_name = re.sub(r'[^a-zA-Z0-9 ]', '', text_lines[1]) | |
print(f"Color name: {color_name}") | |
# Draw the rectangle on the original image | |
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2) | |
count = count + 1 | |
if len(text_lines[1].strip()) == 0: | |
cv2.rectangle(image, (x, y + h + 3), (x+w, y + | |
h + 3 + int(1 * h)), (0, 0, 255), 2) | |
print(f"Found lines: {text_lines}") | |
input("Press Enter to continue...") | |
else: | |
cv2.rectangle(image, (x, y + h + 3), (x+w, y + | |
h + 3 + int(1 * h)), (255, 0, 0), 2) | |
named = named + 1 | |
new_color = { | |
"r": avg_color_rgb[0], | |
"g": avg_color_rgb[1], | |
"b": avg_color_rgb[2], | |
"group": "basic", | |
"name": text_lines[1].strip() | |
} | |
palette["colors"].append(new_color) | |
cv2.imwrite("rectangles.png", image) | |
print(f"Found {count}, Named {named}") | |
cv2.waitKey(0) | |
cv2.destroyAllWindows() | |
with open('vallejo_panzer_aces.json', 'w') as file: | |
json.dump(palette, file, indent=4) | |
print("Done!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment