File size: 1,284 Bytes
f74ae4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from PIL import Image
import numpy as np

def create_anaglyph(left_img, right_img, output_path=""):
    if left_img is None or right_img is None:
        raise FileNotFoundError("Left or right image not found.")

    # Ensure both images are the same size
    left_img = left_img.resize(right_img.size)

    # Convert images to NumPy arrays in RGB format
    left_np = np.array(left_img.convert("RGB"))
    right_np = np.array(right_img.convert("RGB"))

    # Extract color channels
    r_left = left_np[:, :, 0]
    g_right = right_np[:, :, 1]
    b_right = right_np[:, :, 2]

    # Create anaglyph image: Red from left image, Green/Blue from right image
    anaglyph_np = np.stack((r_left, g_right, b_right), axis=2).astype(np.uint8)
    anaglyph_img = Image.fromarray(anaglyph_np)

    # Save output (optional)
    if output_path:
        anaglyph_img.save(output_path)
        print(f"Anaglyph image saved to: {output_path}")

    return anaglyph_img


if __name__ == "__main__":
    from PIL import Image

    left = Image.open("stereo_close_left_with_person.png").convert("RGB")
    right = Image.open("stereo_close_right_with_person.png").convert("RGB")

    create_anaglyph(
        left_img=left,
        right_img=right,
        output_path="anaglyph_with_person.png"
    )