Skip to content

Instantly share code, notes, and snippets.

@arnydo
Last active December 21, 2024 13:27
Show Gist options
  • Save arnydo/5dc85343eca9b8eb98a0f157b9d4d719 to your computer and use it in GitHub Desktop.
Save arnydo/5dc85343eca9b8eb98a0f157b9d4d719 to your computer and use it in GitHub Desktop.
import os
import numpy as np
from PIL import Image
def load_images(folder):
images = []
filenames = sorted(os.listdir(folder))
for filename in filenames:
if filename.endswith('.png') or filename.endswith('.jpg'):
img = Image.open(os.path.join(folder, filename)).convert('RGB')
images.append(np.array(img))
return images
def calculate_difference(slice1, slice2):
# Calculate the sum of squared differences between the right edge of slice1 and the left edge of slice2
return np.sum((slice1[:, -1] - slice2[:, 0]) ** 2)
def find_best_match(slices):
n = len(slices)
matched_slices = [slices[0]]
slices.pop(0)
while slices:
last_slice = matched_slices[-1]
differences = [calculate_difference(last_slice, s) for s in slices]
best_match_index = np.argmin(differences)
matched_slices.append(slices.pop(best_match_index))
return matched_slices
def save_image(images, output_path):
heights, widths, _ = zip(*(i.shape for i in images))
total_width = sum(widths)
max_height = max(heights)
new_image = Image.new('RGB', (total_width, max_height))
x_offset = 0
for img in images:
pil_img = Image.fromarray(img)
new_image.paste(pil_img, (x_offset, 0))
x_offset += pil_img.width
new_image.save(output_path)
def main():
input_folder = './slices'
output_path = './assembled_image.png'
slices = load_images(input_folder)
matched_slices = find_best_match(slices)
save_image(matched_slices, output_path)
if __name__ == '__main__':
main()
@JollyFrogs
Copy link

Who else is here from HHC 2024? :)

@astra1dev
Copy link

hi hhc people :)

@SWaters-3000
Copy link

Hello fellow HHC spinsters!

@msandy1
Copy link

msandy1 commented Nov 27, 2024

hello HHC players

@nolan-codes
Copy link

This is HotCocoaSeller from HHC2024 ;)

@YarelysRR
Copy link

HHC2024! Thanks!!!

@WaywardDar1ing
Copy link

Who else is here from HHC 2024? :)

oy!

@WaywardDar1ing
Copy link

Hello fellow HHC spinsters!

hi! Is there a spinsters cohort I can join?

@frank-da-tank
Copy link

Definitely here for the 2024 HHC

@kevans7
Copy link

kevans7 commented Dec 12, 2024

Hello HHC2024 friends!

@rundas-r00t
Copy link

whatup peeps!? y'all are awesome! stay kewl

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