Advertisement
zeromega64twenty

An attempt at a MEME maker

Aug 27th, 2023 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | Jokes | 0 0
  1. import os
  2. import random
  3. from PIL import Image, ImageDraw, ImageFont
  4. from random_word import RandomWords
  5.  
  6. def generate_random_word():
  7.     r = RandomWords()
  8.     return r.get_random_word()
  9.  
  10. def draw_stickman(draw, position, size):
  11.     x, y = position
  12.     # Random stickman color
  13.     stickman_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  14.     # Head
  15.     draw.ellipse((x - size, y - size, x + size, y + size), fill=stickman_color)
  16.     # Body
  17.     draw.line((x, y + size, x, y + 3*size), fill=stickman_color, width=size//10)
  18.     # Arms
  19.     draw.line((x, y + 2*size, x - 2*size, y + size), fill=stickman_color, width=size//10)
  20.     draw.line((x, y + 2*size, x + 2*size, y + size), fill=stickman_color, width=size//10)
  21.     # Legs
  22.     draw.line((x, y + 3*size, x - 2*size, y + 4*size), fill=stickman_color, width=size//10)
  23.     draw.line((x, y + 3*size, x + 2*size, y + 4*size), fill=stickman_color, width=size//10)
  24.  
  25. def create_random_meme(image_filename, output_filename):
  26.     # Get the script's directory
  27.     script_directory = os.path.dirname(os.path.abspath(__file__))
  28.    
  29.     # Open the image
  30.     image_path = os.path.join(script_directory, image_filename)
  31.     img = Image.open(image_path)
  32.    
  33.     # Calculate the appropriate font size for the horizontal dimensions of the image
  34.     font_size = max(100, img.width // (len(generate_random_word()) + 1))
  35.    
  36.     # Use default font
  37.     font = ImageFont.load_default()
  38.    
  39.     # Create a drawing object
  40.     draw = ImageDraw.Draw(img)
  41.    
  42.     # Generate random top and bottom text
  43.     text_top = generate_random_word()
  44.     text_bottom = generate_random_word()
  45.    
  46.     # Calculate text size and position for top text
  47.     text_width, text_height = draw.textsize(text_top, font=font)
  48.     x = (img.width - text_width) / 2
  49.     y = max(10, img.height // 20)
  50.    
  51.     # Add top text
  52.     draw.text((x, y), text_top, font=font, fill="white")
  53.    
  54.     # Calculate text size and position for bottom text
  55.     text_width, text_height = draw.textsize(text_bottom, font=font)
  56.     x = (img.width - text_width) / 2
  57.     y = img.height - text_height - max(100, img.height // 20)
  58.    
  59.     # Add bottom text
  60.     draw.text((x, y), text_bottom, font=font, fill="white")
  61.    
  62.     # Create a new image for the stickmen
  63.     stickman_img = Image.new("RGBA", img.size, (0, 0, 0, 0))
  64.     stickman_draw = ImageDraw.Draw(stickman_img)
  65.    
  66.     # Draw stickmen
  67.     for _ in range(3):
  68.         stickman_size = random.randint(img.height // 20, img.height // 8)
  69.         draw_stickman(stickman_draw, (random.randint(20, img.width - 20), random.randint(20, img.height - 20)), stickman_size)
  70.    
  71.     # Composite the stickman image onto the main image
  72.     img = Image.alpha_composite(img.convert("RGBA"), stickman_img)
  73.    
  74.     # Convert the image to RGB mode before saving as JPEG
  75.     img = img.convert("RGB")
  76.    
  77.     # Save the meme in the same directory as the script
  78.     output_path = os.path.join(script_directory, output_filename)
  79.     img.save(output_path, "JPEG")  # Save as JPEG format
  80.  
  81. if __name__ == "__main__":
  82.     image_filename = "your_image.jpg"  # Replace with your image filename
  83.     output_filename = "composite_meme.jpg"
  84.    
  85.     create_random_meme(image_filename, output_filename)
  86.  
  87. """
  88. Copyright (c) 2023 Zeromega
  89. Drop a link or a Sub on one of my videos if this script help you, copy the link below
  90. https://www.youtube.com/channel/UCfqUJ4rmk6W-ZAjDtkBZ1CA?sub_confirmation=1
  91. """
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement