|
import os |
|
import sys |
|
from PIL import Image |
|
|
|
def create_alpha_blend(colored_image_path, alpha_image_path, output_path): |
|
colored_image_path = os.path.expanduser(colored_image_path) |
|
alpha_image_path = os.path.expanduser(alpha_image_path) |
|
output_path = os.path.expanduser(output_path) |
|
|
|
colored_image = Image.open(colored_image_path) |
|
alpha_image = Image.open(alpha_image_path).convert("L") # Convert to grayscale |
|
|
|
# Ensure both images have the same size |
|
if colored_image.size != alpha_image.size: |
|
raise ValueError("Images must have the same dimensions") |
|
|
|
# Create a new RGBA image with the same size |
|
result = Image.new("RGBA", colored_image.size) |
|
|
|
for x in range(colored_image.width): |
|
for y in range(colored_image.height): |
|
# Get the RGB values from the colored image |
|
r, g, b = colored_image.getpixel((x, y)) |
|
|
|
# Get the alpha value from the alpha image |
|
alpha_value = alpha_image.getpixel((x, y)) |
|
|
|
# Create a new RGBA pixel with the same RGB values and the alpha from the alpha image |
|
result.putpixel((x, y), (r, g, b, alpha_value)) |
|
|
|
# Save the result |
|
result.save(output_path, "PNG") |
|
|
|
if __name__ == "__main__": |
|
if len(sys.argv) != 4: |
|
print("Usage: python ptblender.py <colored_image_path> <alpha_image_path> <output_image_path>") |
|
sys.exit(1) |
|
|
|
colored_image_path, alpha_image_path, output_path = sys.argv[1], sys.argv[2], sys.argv[3] |
|
create_alpha_blend(colored_image_path, alpha_image_path, output_path) |