To achieve your request using Python and the Pillow library, follow these steps. This script will create a composite image with a background, two images (transparent PNGs) side-by-side, and titles above each image. The script assumes you have the Pillow library installed. If not, you can install it using pip install Pillow.
Here's how you can do it:
from PIL import Image, ImageDraw, ImageFont
# Load the background image
background = Image.open("/content/background.png")
# Load the transparent images
image1 = Image.open("/content/image1.png")
image2 = Image.open("/content/image.png")
# Resize transparent images if necessary
# For example, to resize to height of 100 pixels while maintaining aspect ratio
base_height = 100
image1_height_percent = (base_height / float(image1.size[1]))
image2_height_percent = (base_height / float(image2.size[1]))
image1_width_size = int((float(image1.size[0]) * float(image1_height_percent)))
image2_width_size = int((float(image2.size[0]) * float(image2_height_percent)))
image1 = image1.resize((image1_width_size, base_height), Image.ANTIALIAS)
image2 = image2.resize((image2_width_size, base_height), Image.ANTIALIAS)
# Calculate positions for images side by side with a gap
gap = 10 # Gap between images
total_width = image1_width_size + image2_width_size + gap
x_offset = (background.width - total_width) // 2 # Centering the composition
y_offset = (background.height - base_height) // 2 # Adjust vertical position as needed
# Create drawing context
draw = ImageDraw.Draw(background)
# Load the font
font = ImageFont.truetype("/content/simfang.ttf", 24) # Adjust font size as needed
# Calculate text positions and draw text
text1 = "reference image"
text2 = "target image"
text1_width, text1_height = draw.textsize(text1, font=font)
text2_width, text2_height = draw.textsize(text2, font=font)
draw.text(((x_offset - text1_width // 2) + image1_width_size // 2, y_offset - text1_height), text1, font=font, fill="black")
draw.text(((x_offset + total_width - text2_width // 2) - image2_width_size // 2, y_offset - text2_height), text2, font=font, fill="black")
# Paste the images onto the background
background.paste(image1, (x_offset, y_offset), image1)
background.paste(image2, (x_offset + image1_width_size + gap, y_offset), image2)
# Save or show the final image
background.show() # This will display the image if possible
# background.save("/content/final_image.png") # Save the image to a file
Please adjust the x_offset and y_offset values as per your background and how you want the images and text to be aligned. The same goes for the base_height to resize the transparent images according to your needs. The font size and path to the font are also essential to match your design preferences. Remember, this script assumes the background is sufficiently large to accommodate the images and text; adjust the positions and sizes as needed for your specific case.