Checkered background code
Browse files
utils/add_checkered_background.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
from PIL import Image
|
4 |
+
from concurrent.futures import ThreadPoolExecutor
|
5 |
+
|
6 |
+
def create_gray_checkerboard(shape: tuple, square_size: int):
|
7 |
+
"""
|
8 |
+
Create a gray-scale checkerboard pattern array with the given shape.
|
9 |
+
The pattern alternates between 0.8 and 1.0 values in a square_size grid.
|
10 |
+
"""
|
11 |
+
# shape = (height, width)
|
12 |
+
x, y = np.meshgrid(np.arange(shape[1]), np.arange(shape[0]))
|
13 |
+
# Scale 0.2 + 0.8 => (0.2, 1.0) can also be used, but in this example we'll keep 0.8 and 1.0
|
14 |
+
# (depending on your desired brightness).
|
15 |
+
board = ((x // square_size + y // square_size) % 2) * 0.2 + 0.8
|
16 |
+
return board
|
17 |
+
|
18 |
+
def add_checkered_background_to_image(image_path, output_path, square_size=20):
|
19 |
+
"""
|
20 |
+
Add a gray checkerboard background to an image and save it as PNG.
|
21 |
+
"""
|
22 |
+
with Image.open(image_path) as img:
|
23 |
+
img = img.convert("RGBA")
|
24 |
+
|
25 |
+
# Create checkerboard pattern.
|
26 |
+
# Using the size (height, width) from the image.
|
27 |
+
checkerboard_array = create_gray_checkerboard((img.height, img.width), square_size)
|
28 |
+
|
29 |
+
# Convert checkerboard_array into an RGBA image (gray -> R=G=B, alpha=255)
|
30 |
+
# First convert float values into 8-bit grayscale.
|
31 |
+
# Expand dims to make it into (height, width, 1).
|
32 |
+
checkerboard_gray = (checkerboard_array * 255).astype(np.uint8)
|
33 |
+
checkerboard_gray = np.expand_dims(checkerboard_gray, axis=2)
|
34 |
+
|
35 |
+
# Stack 3 copies (for R,G,B) plus one alpha channel of 255.
|
36 |
+
alpha_channel = np.full_like(checkerboard_gray, 255)
|
37 |
+
checkerboard_rgba = np.concatenate([checkerboard_gray]*3 + [alpha_channel], axis=2)
|
38 |
+
|
39 |
+
background = Image.fromarray(checkerboard_rgba, mode="RGBA")
|
40 |
+
|
41 |
+
# Composite the image over the checkerboard background
|
42 |
+
combined = Image.alpha_composite(background, img)
|
43 |
+
combined.save(output_path, "PNG")
|
44 |
+
|
45 |
+
def process_image_file(input_path, output_path, square_size):
|
46 |
+
"""
|
47 |
+
Process a single image file to add a checkerboard background.
|
48 |
+
"""
|
49 |
+
if not os.path.exists(output_path):
|
50 |
+
add_checkered_background_to_image(input_path, output_path, square_size)
|
51 |
+
print(f"Processed (checkerboard): {input_path} -> {output_path}")
|
52 |
+
else:
|
53 |
+
print(f"Skipped (checkerboard): {output_path} already exists")
|
54 |
+
|
55 |
+
def process_directory(input_dir, output_dir, square_size=20):
|
56 |
+
"""
|
57 |
+
Recursively process a directory to add a checkerboard background to all images and convert them to PNG.
|
58 |
+
"""
|
59 |
+
if not os.path.exists(output_dir):
|
60 |
+
os.makedirs(output_dir)
|
61 |
+
|
62 |
+
tasks = []
|
63 |
+
with ThreadPoolExecutor() as executor:
|
64 |
+
for root, _, files in os.walk(input_dir):
|
65 |
+
for file in files:
|
66 |
+
if file.lower().endswith(('.png', '.jpg', '.jpeg')):
|
67 |
+
input_path = os.path.join(root, file)
|
68 |
+
relative_path = os.path.relpath(input_path, input_dir)
|
69 |
+
output_path = os.path.join(output_dir, os.path.splitext(relative_path)[0] + '.png')
|
70 |
+
|
71 |
+
# Ensure the output directory exists
|
72 |
+
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
73 |
+
|
74 |
+
# Submit the task to the executor
|
75 |
+
tasks.append(executor.submit(process_image_file, input_path, output_path, square_size))
|
76 |
+
|
77 |
+
# Wait for all tasks to complete
|
78 |
+
for task in tasks:
|
79 |
+
task.result()
|