84 lines
3.7 KiB
Python
84 lines
3.7 KiB
Python
|
|
from PIL import Image
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
import glob
|
||
|
|
|
||
|
|
def create_thumbnail(input_image_path, background_path, rainy_still_path):
|
||
|
|
try:
|
||
|
|
print(f"Processing input image: {input_image_path}")
|
||
|
|
# Open the images and ensure they are in RGBA mode
|
||
|
|
input_image = Image.open(input_image_path).convert("RGBA")
|
||
|
|
background_image = Image.open(background_path).convert("RGBA")
|
||
|
|
rainy_still_image = Image.open(rainy_still_path).convert("RGBA")
|
||
|
|
|
||
|
|
# Scale the input image up by 5%
|
||
|
|
scaled_width = int(input_image.width * 1.05)
|
||
|
|
scaled_height = int(input_image.height * 1.05)
|
||
|
|
input_image = input_image.resize((scaled_width, scaled_height), Image.LANCZOS)
|
||
|
|
|
||
|
|
# Create a new image with the same size as the background
|
||
|
|
combined_image = Image.new('RGBA', background_image.size)
|
||
|
|
|
||
|
|
# Paste the background
|
||
|
|
combined_image.paste(background_image, (0, 0))
|
||
|
|
|
||
|
|
# Calculate position to center the input image
|
||
|
|
input_x = (combined_image.width - input_image.width) // 2
|
||
|
|
input_y = (combined_image.height - input_image.height) // 2
|
||
|
|
|
||
|
|
# Paste the input image on top
|
||
|
|
combined_image.paste(input_image, (input_x, input_y), input_image)
|
||
|
|
|
||
|
|
# Move RainyStill down and to the right by 783 pixels
|
||
|
|
rainy_still_position = (783, 783) # Adjust as needed
|
||
|
|
combined_image.paste(rainy_still_image, rainy_still_position, rainy_still_image)
|
||
|
|
|
||
|
|
return combined_image # Return the combined image
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"Error: {e}") # Print the error message for debugging
|
||
|
|
return None # Return None if there was an error
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
# Define the paths for the background and RainyStill images
|
||
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
||
|
|
background_path = os.path.join(script_dir, "background.png")
|
||
|
|
rainy_still_path = os.path.join(script_dir, "RainyStill.png")
|
||
|
|
|
||
|
|
# Check if the background and RainyStill images exist
|
||
|
|
if not os.path.exists(background_path):
|
||
|
|
print(f"Background image not found: {background_path}")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
if not os.path.exists(rainy_still_path):
|
||
|
|
print(f"RainyStill image not found: {rainy_still_path}")
|
||
|
|
sys.exit(1)
|
||
|
|
|
||
|
|
# Find all output* files in the current directory
|
||
|
|
output_files = glob.glob(os.path.join(script_dir, "output*"))
|
||
|
|
|
||
|
|
# Process each output file
|
||
|
|
for input_image_path in output_files:
|
||
|
|
# Generate a unique output image path in the script directory
|
||
|
|
output_image_path = os.path.join(script_dir, "output_thumb.jpg") # Start with output_thumb.jpg
|
||
|
|
|
||
|
|
# 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_thumb_{counter}.jpg")
|
||
|
|
counter += 1
|
||
|
|
|
||
|
|
# Call the function to create the thumbnail
|
||
|
|
combined_image = create_thumbnail(input_image_path, background_path, rainy_still_path)
|
||
|
|
|
||
|
|
# Save the combined image if it was created successfully
|
||
|
|
if combined_image is not None:
|
||
|
|
# Convert to RGB mode for JPG (JPG doesn't support alpha)
|
||
|
|
rgb_image = Image.new("RGB", combined_image.size, (255, 255, 255))
|
||
|
|
rgb_image.paste(combined_image, mask=combined_image.split()[3]) # Use alpha as mask
|
||
|
|
|
||
|
|
# Save as JPG with quality parameter to control file size
|
||
|
|
rgb_image.save(output_image_path, format='JPEG', quality=100) # Higher quality for better image fidelity
|
||
|
|
print(f"JPG saved successfully at: {output_image_path}") # Log success
|
||
|
|
else:
|
||
|
|
print(f"Failed to process {input_image_path}.")
|