Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import random
- from PIL import Image, ImageDraw, ImageFont
- from random_word import RandomWords
- def generate_random_word():
- r = RandomWords()
- return r.get_random_word()
- def draw_stickman(draw, position, size):
- x, y = position
- # Random stickman color
- stickman_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
- # Head
- draw.ellipse((x - size, y - size, x + size, y + size), fill=stickman_color)
- # Body
- draw.line((x, y + size, x, y + 3*size), fill=stickman_color, width=size//10)
- # Arms
- draw.line((x, y + 2*size, x - 2*size, y + size), fill=stickman_color, width=size//10)
- draw.line((x, y + 2*size, x + 2*size, y + size), fill=stickman_color, width=size//10)
- # Legs
- draw.line((x, y + 3*size, x - 2*size, y + 4*size), fill=stickman_color, width=size//10)
- draw.line((x, y + 3*size, x + 2*size, y + 4*size), fill=stickman_color, width=size//10)
- def create_random_meme(image_filename, output_filename):
- # Get the script's directory
- script_directory = os.path.dirname(os.path.abspath(__file__))
- # Open the image
- image_path = os.path.join(script_directory, image_filename)
- img = Image.open(image_path)
- # Calculate the appropriate font size for the horizontal dimensions of the image
- font_size = max(100, img.width // (len(generate_random_word()) + 1))
- # Use default font
- font = ImageFont.load_default()
- # Create a drawing object
- draw = ImageDraw.Draw(img)
- # Generate random top and bottom text
- text_top = generate_random_word()
- text_bottom = generate_random_word()
- # Calculate text size and position for top text
- text_width, text_height = draw.textsize(text_top, font=font)
- x = (img.width - text_width) / 2
- y = max(10, img.height // 20)
- # Add top text
- draw.text((x, y), text_top, font=font, fill="white")
- # Calculate text size and position for bottom text
- text_width, text_height = draw.textsize(text_bottom, font=font)
- x = (img.width - text_width) / 2
- y = img.height - text_height - max(100, img.height // 20)
- # Add bottom text
- draw.text((x, y), text_bottom, font=font, fill="white")
- # Create a new image for the stickmen
- stickman_img = Image.new("RGBA", img.size, (0, 0, 0, 0))
- stickman_draw = ImageDraw.Draw(stickman_img)
- # Draw stickmen
- for _ in range(3):
- stickman_size = random.randint(img.height // 20, img.height // 8)
- draw_stickman(stickman_draw, (random.randint(20, img.width - 20), random.randint(20, img.height - 20)), stickman_size)
- # Composite the stickman image onto the main image
- img = Image.alpha_composite(img.convert("RGBA"), stickman_img)
- # Convert the image to RGB mode before saving as JPEG
- img = img.convert("RGB")
- # Save the meme in the same directory as the script
- output_path = os.path.join(script_directory, output_filename)
- img.save(output_path, "JPEG") # Save as JPEG format
- if __name__ == "__main__":
- image_filename = "your_image.jpg" # Replace with your image filename
- output_filename = "composite_meme.jpg"
- create_random_meme(image_filename, output_filename)
- """
- Copyright (c) 2023 Zeromega
- Drop a link or a Sub on one of my videos if this script help you, copy the link below
- https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement