from PIL import Image
import os
def combine_channels():
# Define filenames
r_path = 'red.tif'
g_path = 'green.tif'
b_path = 'blue.tif'
output_path = 'rgb.tif'
# Check if all files exist to avoid errors
for file in [r_path, g_path, b_path]:
if not os.path.exists(file):
print(f"Error: Could not find {file} in the current folder.")
return
try:
# Open the source images and convert to grayscale (L mode)
# just in case they aren't already formatted correctly
r = Image.open(r_path).convert('L')
g = Image.open(g_path).convert('L')
b = Image.open(b_path).convert('L')
# Merge the three images into one RGB image
rgb_image = Image.merge('RGB', (r, g, b))
# Save the result
rgb_image.save(output_path)
print(f"Success! Saved combined image as {output_path}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
combine_channels()