You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.9 KiB
Python

import os
import random
from PIL import Image
def randomly_insert_wally(directory_path, wally_path, output_directory):
# Get all image files in the directory
image_files = [f for f in os.listdir(directory_path) if f.endswith(('.jpg', '.jpeg', '.png'))]
# If no images found, exit
if not image_files:
print("No images found in the specified directory.")
return
# Select a random image from the directory
random_image_path = os.path.join(directory_path, random.choice(image_files))
# Open the background (randomly selected) image and Wally image
background = Image.open(random_image_path).convert("RGBA")
wally = Image.open(wally_path).convert("RGBA")
# Resize Wally if it's too large for the background
if wally.size[0] > background.size[0] or wally.size[1] > background.size[1]:
wally = wally.resize((background.size[0] // 4, background.size[1] // 4)) # Resize to 1/4th of background size
# Get background and Wally dimensions
bg_width, bg_height = background.size
wally_width, wally_height = wally.size
# Generate random coordinates within the background for Wally placement
x = random.randint(0, bg_width - wally_width)
y = random.randint(0, bg_height - wally_height)
# Create a copy of the background to paste Wally onto it
combined = background.copy()
# Paste Wally onto the background at the random coordinates with transparency
combined.paste(wally, (x, y), wally)
# Convert back to RGB if saving as JPEG (JPEG does not support alpha channel)
final_image = combined.convert("RGB")
# Ensure output directory exists
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# Save the result
output_path = os.path.join(output_directory, f"output_{os.path.basename(random_image_path)}")
final_image.save(output_path, format="JPEG")
print(f"Saved combined image with Wally at {output_path}")
# Usage
directory_path = "images"
# Make sure this is a transparent .png file of Wally
wally_paths = ['symposium/waldo_transparent.png', 'symposium/woof_transparent.png', 'symposium/waldina_transparent.png']
# Run the function for each character
for postava in wally_paths:
randomly_insert_wally(directory_path, postava, directory_path)
def append_hex_to_random_images(input_folder, output_folder, wally):
# Check if the input folder exists
if not os.path.exists(input_folder):
print(f"The folder '{input_folder}' does not exist.")
return
# Ensure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Get all images in the input folder with allowed extensions
images = [f for f in os.listdir(input_folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]
# Check if there are at least two images to proceed
if len(images) < 2:
print("Not enough images in the folder to perform the operation. Need at least two images.")
return
# Select two random images
first_image_name = random.choice(images)
first_image_path = os.path.join(input_folder, first_image_name)
second_image_path = wally
# Ensure the second image is different from the first
while first_image_path == second_image_path:
second_image_path = os.path.join(input_folder, random.choice(images))
# Read the first image in binary mode
with open(first_image_path, 'rb') as first_image_file:
first_image_data = first_image_file.read()
# Read the second image in binary mode
with open(second_image_path, 'rb') as second_image_file:
second_image_data = second_image_file.read()
# Find the JPEG end marker (FFD9 in hex) in the first image data
jpeg_end_marker = b'\xFF\xD9'
end_marker_index = first_image_data.rfind(jpeg_end_marker)
if end_marker_index == -1:
print("End of JPEG marker not found in the first image.")
return
# Split the first image data at the end marker
first_image_main = first_image_data[:end_marker_index + 2] # Include the marker
first_image_remainder = first_image_data[end_marker_index + 2:]
# Concatenate: first image main data + hex of the second image + remainder of first image (if any)
combined_data = first_image_main + second_image_data + first_image_remainder
# Construct the output file path, using the same name as the first image
output_image_path = os.path.join(output_folder, first_image_name)
# Write the combined data to the output file
with open(output_image_path, 'wb') as output_file:
output_file.write(combined_data)
print(f"Hex data of '{second_image_path}' appended to '{first_image_path}', saved as '{output_image_path}'.")
odlaw_path = 'symposium/odlaw_transparent.png'
#append_hex_to_random_images('images', 'images', odlaw_path)