3D_Image_Composer / create_anaglyph.py
gexu13's picture
Upload 16 files
f74ae4b verified
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"
)