Leg med Stereogrammer

Midlertidig kode indtil jeg er færdig med mine eksaminer…

def load_image(image_path):
return Image.open(image_path).convert(‘RGB’)

def load_height_map(height_map_path, target_size):
height_map = Image.open(height_map_path).convert(‘L’)
height_map = height_map.resize(target_size)
return height_map

def create_stereogram(image, height_map, eye_distance=50, num_strips=20):
image_np = np.array(image)
height_map_np = np.array(height_map)
height, width, _ = image_np.shape
strip_width = width // num_strips


stereogram = np.zeros_like(image_np)

for y in range(height):
    for strip in range(num_strips):
        strip_start = strip * strip_width
        strip_end = strip_start + strip_width
        if strip == num_strips - 1:
            strip_end = width  # Make sure the last strip goes to the end of the image

        depth_value = height_map_np[y, strip_start:strip_end].mean() / 255.0
        shift = int(depth_value * eye_distance * 100)

        for x in range(strip_start, strip_end):
            if x >= width:
                continue

            shifted_x = x - shift
            if shifted_x >= 0 and shifted_x < width:
                stereogram[y, x] = image_np[y, shifted_x]

for y in range(height):
    if y % 2 == 0:
        for strip in range(num_strips - 1):
            strip_start = strip * strip_width
            strip_end = strip_start + strip_width

            for x in range(strip_start, strip_end):
                if x >= width:
                    continue

                shifted_x = x + strip_width
                if shifted_x >= width:
                    shifted_x = width - 1
                stereogram[y, x] = image_np[y, shifted_x]
    else:
        for strip in range(1, num_strips):
            strip_start = strip * strip_width
            strip_end = strip_start + strip_width

            for x in range(strip_start, strip_end):
                if x >= width:
                    continue

                shifted_x = x - strip_width
                if shifted_x < 0:
                    shifted_x = 0
                stereogram[y, x] = image_np[y, shifted_x]

return Image.fromarray(stereogram)

if name == “main“:
image_path = “x”
height_map_path = “y”


image = load_image(image_path)
height_map = load_height_map(height_map_path, image.size)

stereogram = create_stereogram(image, height_map)

output_path = "z"
stereogram.save(output_path)
print(f"Stereogram saved to: {output_path}")

Skriv en kommentar

Din e-mailadresse vil ikke blive publiceret. Krævede felter er markeret med *