Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rmcdaniel/3d12df49b6554b723c0fca10162e95be to your computer and use it in GitHub Desktop.
Save rmcdaniel/3d12df49b6554b723c0fca10162e95be to your computer and use it in GitHub Desktop.
from PIL import Image
import numpy as np
import os
def load_image_blocks(path, block_size=8):
image = Image.open(path).convert('RGB')
img_array = np.array(image)
h, w = img_array.shape[:2]
h_trim, w_trim = h - h % block_size, w - w % block_size
img_array = img_array[:h_trim, :w_trim]
blocks = []
for y in range(0, h_trim, block_size):
for x in range(0, w_trim, block_size):
block = img_array[y:y+block_size, x:x+block_size]
blocks.append(block)
return blocks
def block_distance(b1, b2):
return np.mean(np.abs(b1.astype(int) - b2.astype(int)))
def rebuild_with_target(source_blocks, target_img_path, block_size=8, size=(256, 256)):
target_img = Image.open(target_img_path).resize(size).convert('RGB')
target_array = np.array(target_img)
h_blocks = size[1]
w_blocks = size[0]
result = []
for y in range(h_blocks):
for x in range(w_blocks):
target_block = target_array[y*block_size:(y+1)*block_size, x*block_size:(x+1)*block_size]
best = min(source_blocks, key=lambda b: block_distance(b, target_block))
result.append(best)
return result, size
def reconstruct_image(blocks, shape, block_size, output_path):
h, w = shape
output = np.zeros((h, w, 3), dtype=np.uint8)
idx = 0
for y in range(0, h, block_size):
for x in range(0, w, block_size):
output[y:y+block_size, x:x+block_size] = blocks[idx]
idx += 1
img = Image.fromarray(output)
img.save(output_path, format='JPEG', quality=95)
if __name__ == "__main__":
source_path = "original.jpg"
target_path = "target.jpg"
output_path = "reconstructed.jpg"
block_size = 8
output_size = (256, 256)
if not os.path.exists(source_path):
print(f"❌ Missing: {source_path}")
elif not os.path.exists(target_path):
print(f"❌ Missing: {target_path}")
else:
source_blocks = load_image_blocks(source_path, block_size)
reconstructed_blocks, shape = rebuild_with_target(source_blocks, target_path, block_size, output_size)
reconstruct_image(reconstructed_blocks, shape, block_size, output_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment