50 lines
1.8 KiB
Python
50 lines
1.8 KiB
Python
|
#!/Users/nadineganz/anaconda3/bin/python
|
||
|
|
||
|
import argparse
|
||
|
from PIL import Image, ImageOps
|
||
|
import os
|
||
|
|
||
|
def combine_images_to_alpha_channel(rgb_image_path, alpha_image_path):
|
||
|
# Open the RGB image (first image)
|
||
|
rgb_image = Image.open(rgb_image_path).convert("RGB")
|
||
|
|
||
|
# Open the image to use as the alpha channel (second image)
|
||
|
alpha_image = Image.open(alpha_image_path).convert("RGB")
|
||
|
|
||
|
# Invert the secondary image (alpha image) using ImageOps.invert
|
||
|
inverted_alpha_image = ImageOps.invert(alpha_image.convert("RGB"))
|
||
|
|
||
|
# Extract the red channel from the inverted alpha image to use as the alpha channel
|
||
|
alpha_channel = inverted_alpha_image.getchannel('R')
|
||
|
|
||
|
# Create a new image with RGBA (RGB + alpha)
|
||
|
rgba_image = rgb_image.convert("RGBA")
|
||
|
|
||
|
# Replace the alpha channel of the new image with the extracted alpha
|
||
|
rgba_image.putalpha(alpha_channel)
|
||
|
|
||
|
# Generate output file name: append "_A" to the first image file name (without extension)
|
||
|
output_filename = os.path.splitext(rgb_image_path)[0] + "_A.png"
|
||
|
|
||
|
# Save the final image with alpha channel as PNG
|
||
|
rgba_image.save(output_filename, "PNG")
|
||
|
|
||
|
print(f"Image saved as {output_filename}")
|
||
|
|
||
|
def main():
|
||
|
# Set up argument parsing
|
||
|
parser = argparse.ArgumentParser(description="Combine two images, using the second image's inverted red channel as alpha for the first image.")
|
||
|
|
||
|
# Define the arguments
|
||
|
parser.add_argument("rgb_image", help="Path to the first RGB image (input image)")
|
||
|
parser.add_argument("alpha_image", help="Path to the second RGB image (used to extract alpha channel, inverted before use)")
|
||
|
|
||
|
# Parse the arguments
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
# Call the function to combine the images and create the output
|
||
|
combine_images_to_alpha_channel(args.rgb_image, args.alpha_image)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|