Advertisement
Python253

webdriver_torso_emulator

May 11th, 2024
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: webdriver_torso_emulator.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. The Webdriver Torso Emulator is designed to mimic the style of the renowned YouTube channel, WebDriver Torso.
  9. It generates a sequence of images featuring randomly placed red and blue boxes.
  10. Additionally, it applies a customizable watermark to each image before assembling them into a video.
  11. This script aims to replicate the distinct aesthetic of WebDriver Torso's videos.
  12.  
  13. Imports:
  14.    - random: Provides functions for generating random numbers.
  15.    - imageio: Library for reading and writing a wide range of image data.
  16.    - PIL (Python Imaging Library): Library for opening, manipulating, and saving many different image file formats.
  17.    - argparse: Library for parsing command-line arguments.
  18.    - warnings: Provides functions for issuing warning messages.
  19.    
  20. Global Constant Variable:
  21.    mp4_title (str): The title of the generated MP4 video.
  22.  
  23. Requirements:
  24.    - Python 3.x
  25.    - PIL (Python Imaging Library)
  26.    - imageio library
  27.  
  28. Functions:
  29.    1. generate_image(width, height, num_images): Generates a series of images with random red and blue boxes.
  30.    2. add_watermark(image, watermark_text, font_size=24, text_color=(0, 0, 0)): Adds a watermark to the given image with specified text, font size, and color.
  31.    3. main(): Main function to parse command-line arguments, generate images, add watermarks, and create a video.
  32.  
  33. Usage:
  34.    - Run the script with optional command-line arguments to specify image dimensions and the number of images to generate.
  35.    - The script will generate images, add watermarks, and create a video named 'webdriver_torso.mp4'.
  36.  
  37. Additional Notes:
  38.    - Make sure to have the required dependencies installed before running the script.
  39.    - Customize the watermark text, font size, and color as needed.
  40. """
  41.  
  42. import random
  43. import imageio
  44. from PIL import Image, ImageDraw, ImageFont
  45. import argparse
  46. import warnings
  47.  
  48. # Suppress DeprecationWarning
  49. warnings.filterwarnings("ignore", category=DeprecationWarning)
  50.  
  51. # Global Constant Variable
  52. mp4_title = "webdriver_torso.mp4"
  53.  
  54. def generate_image(width, height, num_images):
  55.     images = []
  56.     for _ in range(num_images):
  57.         # Creating image with specified background color
  58.         image = Image.new("RGB", (width, height), (255, 255, 255))
  59.  
  60.         # Randomly determine size and position of red box
  61.         red_width = random.randint(width // 2, width // 2)
  62.         red_height = random.randint(height // 8, height // 2)
  63.         red_x = random.randint(0, width - red_width)
  64.         red_y = random.randint(0, height - red_height)
  65.  
  66.         # Randomly determine size and position of blue box
  67.         blue_width = random.randint(width // 2, width // 1)
  68.         blue_height = random.randint(height // 4, height // 4)
  69.         blue_x = random.randint(0, width - blue_width)
  70.         blue_y = random.randint(0, height - blue_height)
  71.  
  72.         # Drawing red box on top of the background
  73.         red_box = Image.new("RGB", (red_width, red_height), (255, 0, 0))
  74.         image.paste(red_box, (red_x, red_y))
  75.  
  76.         # Drawing blue box on top of the background
  77.         blue_box = Image.new("RGB", (blue_width, blue_height), (0, 0, 255))
  78.         image.paste(blue_box, (blue_x, blue_y))
  79.  
  80.         images.append(image)
  81.  
  82.     return images
  83.  
  84. # Function to add a watermark to the images
  85. def add_watermark(image, watermark_text, font_size=24, text_color=(0, 0, 0)):
  86.     font = ImageFont.truetype("arial.ttf", font_size)
  87.     draw = ImageDraw.Draw(image)
  88.     text_bbox = draw.textbbox((0, 0), watermark_text, font=font)
  89.     text_height = text_bbox[3] - text_bbox[1]
  90.     draw.text(
  91.         (10, image.height - text_height - 10),
  92.         watermark_text,
  93.         fill=text_color,
  94.         font=font,
  95.     )
  96.     return image
  97.  
  98. def main():
  99.     # Argument parsing
  100.     parser = argparse.ArgumentParser(description="Generate video with watermark.")
  101.     parser.add_argument(
  102.         "-W", "--width", type=int, default=600, help="Width of the images"
  103.     )
  104.     parser.add_argument(
  105.         "-H", "--height", type=int, default=600, help="Height of the images"
  106.     )
  107.     parser.add_argument(
  108.         "-n", "--num_images", type=int, default=10, help="Number of images to generate"
  109.     )
  110.     args = parser.parse_args()
  111.  
  112.     # Generate images
  113.     images = generate_image(args.width, args.height, args.num_images)
  114.  
  115.     # Add watermark and save images
  116.     image_filenames = []
  117.     for i, image in enumerate(images):
  118.         watermark_text = f"pyro.flv - slide {i+1:03d}"
  119.         image_with_watermark = add_watermark(image, watermark_text)
  120.         filename = f"img_{i+1}_{args.width}x{args.height}.png"
  121.         image_with_watermark.save(filename)
  122.         image_filenames.append(filename)
  123.  
  124.     # Create video from images
  125.     with imageio.get_writer(
  126.         "webdriver_torso.mp4",
  127.         fps=1,
  128.         quality=8,
  129.         macro_block_size=1,
  130.         ffmpeg_log_level="quiet",
  131.     ) as _:
  132.         for filename in image_filenames:
  133.             image = imageio.imread(filename)
  134.             _.append_data(image)
  135.  
  136.     print(
  137.         f"[{mp4_title}]: Video created successfully in the current working directory."
  138.     )
  139.  
  140. if __name__ == "__main__":
  141.     main()
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement