118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
|
|
import os
|
||
|
|
import sys
|
||
|
|
from PIL import Image
|
||
|
|
|
||
|
|
def process_image(input_image_path, frame_image_path, output_path):
|
||
|
|
"""
|
||
|
|
Process the input image and frame, creating a simple composite PNG
|
||
|
|
|
||
|
|
Args:
|
||
|
|
input_image_path: Path to the input image
|
||
|
|
frame_image_path: Path to the frame image (PNG with transparency)
|
||
|
|
output_path: Path where the output will be saved
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
True if successful, False otherwise
|
||
|
|
"""
|
||
|
|
try:
|
||
|
|
# Open the images
|
||
|
|
print(f"Opening input image: {input_image_path}")
|
||
|
|
input_image = Image.open(input_image_path).convert("RGBA")
|
||
|
|
|
||
|
|
print(f"Opening frame image: {frame_image_path}")
|
||
|
|
frame_image = Image.open(frame_image_path).convert("RGBA")
|
||
|
|
|
||
|
|
# Get dimensions of the frame
|
||
|
|
frame_width, frame_height = frame_image.size
|
||
|
|
print(f"Frame dimensions: {frame_width}x{frame_height}")
|
||
|
|
|
||
|
|
# Resize input image to 904x904
|
||
|
|
input_image = input_image.resize((904, 904), Image.LANCZOS)
|
||
|
|
|
||
|
|
# Calculate position to center the input image
|
||
|
|
input_width, input_height = input_image.size
|
||
|
|
input_x = (frame_width - input_width) // 2
|
||
|
|
input_y = (frame_height - input_height) // 2
|
||
|
|
|
||
|
|
# Position the input image on a transparent background
|
||
|
|
positioned_input = Image.new("RGBA", (frame_width, frame_height), (0, 0, 0, 0))
|
||
|
|
positioned_input.paste(input_image, (input_x, input_y))
|
||
|
|
|
||
|
|
# Create a composite image
|
||
|
|
composite = Image.new("RGBA", (frame_width, frame_height), (0, 0, 0, 0))
|
||
|
|
composite.paste(positioned_input, (0, 0))
|
||
|
|
composite.paste(frame_image, (0, 0), frame_image)
|
||
|
|
|
||
|
|
# Save the composite image
|
||
|
|
composite.save(output_path, "PNG")
|
||
|
|
print(f"Saved composite image: {output_path}")
|
||
|
|
|
||
|
|
return True
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error processing image: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|
||
|
|
return False
|
||
|
|
|
||
|
|
def main():
|
||
|
|
# Print command line arguments for debugging
|
||
|
|
print(f"Command line arguments: {sys.argv}")
|
||
|
|
|
||
|
|
# Check if at least one input image is provided
|
||
|
|
if len(sys.argv) < 3:
|
||
|
|
print("Usage: python FrameTool.py <input_image_path> <frame_image_path>")
|
||
|
|
print(" or: python FrameTool.py <input_image_path1> <input_image_path2> ... <frame_image_path>")
|
||
|
|
return
|
||
|
|
|
||
|
|
# Get the frame image path from the last argument
|
||
|
|
frame_image_path = sys.argv[-1] # The last argument is the frame image
|
||
|
|
print(f"Frame image path: {frame_image_path}")
|
||
|
|
|
||
|
|
# Check if the frame image exists
|
||
|
|
if not os.path.exists(frame_image_path):
|
||
|
|
print(f"Frame image not found: {frame_image_path}")
|
||
|
|
return
|
||
|
|
|
||
|
|
# Get the directory of the script for output
|
||
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
print(f"Script directory: {script_dir}")
|
||
|
|
|
||
|
|
# Process each input image
|
||
|
|
input_images = sys.argv[1:-1] # Exclude the last argument (frame image path)
|
||
|
|
print(f"Input images to process: {input_images}")
|
||
|
|
|
||
|
|
for input_image_path in input_images:
|
||
|
|
# Check if the input image exists
|
||
|
|
if not os.path.exists(input_image_path):
|
||
|
|
print(f"Input image not found: {input_image_path}")
|
||
|
|
continue
|
||
|
|
|
||
|
|
print(f"Processing input image: {input_image_path}")
|
||
|
|
|
||
|
|
# Generate a unique output image path in the script directory
|
||
|
|
output_image_path = os.path.join(script_dir, "output.png") # Start with output.png
|
||
|
|
|
||
|
|
# Ensure the output file does not overwrite existing files
|
||
|
|
counter = 1
|
||
|
|
while os.path.exists(output_image_path):
|
||
|
|
output_image_path = os.path.join(script_dir, f"output_{counter}.png")
|
||
|
|
counter += 1
|
||
|
|
|
||
|
|
print(f"Output image path: {output_image_path}")
|
||
|
|
|
||
|
|
# Process the image
|
||
|
|
success = process_image(input_image_path, frame_image_path, output_image_path)
|
||
|
|
|
||
|
|
if success:
|
||
|
|
print(f"Successfully processed and saved {input_image_path}.")
|
||
|
|
else:
|
||
|
|
print(f"Failed to process {input_image_path}.")
|
||
|
|
|
||
|
|
# Add a try-except block to catch and print any errors
|
||
|
|
if __name__ == "__main__":
|
||
|
|
try:
|
||
|
|
main()
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error in main: {e}")
|
||
|
|
import traceback
|
||
|
|
traceback.print_exc()
|