Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: webdriver_torso_emulator.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- The Webdriver Torso Emulator is designed to mimic the style of the renowned YouTube channel, WebDriver Torso.
- It generates a sequence of images featuring randomly placed red and blue boxes.
- Additionally, it applies a customizable watermark to each image before assembling them into a video.
- This script aims to replicate the distinct aesthetic of WebDriver Torso's videos.
- Imports:
- - random: Provides functions for generating random numbers.
- - imageio: Library for reading and writing a wide range of image data.
- - PIL (Python Imaging Library): Library for opening, manipulating, and saving many different image file formats.
- - argparse: Library for parsing command-line arguments.
- - warnings: Provides functions for issuing warning messages.
- Global Constant Variable:
- mp4_title (str): The title of the generated MP4 video.
- Requirements:
- - Python 3.x
- - PIL (Python Imaging Library)
- - imageio library
- Functions:
- 1. generate_image(width, height, num_images): Generates a series of images with random red and blue boxes.
- 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.
- 3. main(): Main function to parse command-line arguments, generate images, add watermarks, and create a video.
- Usage:
- - Run the script with optional command-line arguments to specify image dimensions and the number of images to generate.
- - The script will generate images, add watermarks, and create a video named 'webdriver_torso.mp4'.
- Additional Notes:
- - Make sure to have the required dependencies installed before running the script.
- - Customize the watermark text, font size, and color as needed.
- """
- import random
- import imageio
- from PIL import Image, ImageDraw, ImageFont
- import argparse
- import warnings
- # Suppress DeprecationWarning
- warnings.filterwarnings("ignore", category=DeprecationWarning)
- # Global Constant Variable
- mp4_title = "webdriver_torso.mp4"
- def generate_image(width, height, num_images):
- images = []
- for _ in range(num_images):
- # Creating image with specified background color
- image = Image.new("RGB", (width, height), (255, 255, 255))
- # Randomly determine size and position of red box
- red_width = random.randint(width // 2, width // 2)
- red_height = random.randint(height // 8, height // 2)
- red_x = random.randint(0, width - red_width)
- red_y = random.randint(0, height - red_height)
- # Randomly determine size and position of blue box
- blue_width = random.randint(width // 2, width // 1)
- blue_height = random.randint(height // 4, height // 4)
- blue_x = random.randint(0, width - blue_width)
- blue_y = random.randint(0, height - blue_height)
- # Drawing red box on top of the background
- red_box = Image.new("RGB", (red_width, red_height), (255, 0, 0))
- image.paste(red_box, (red_x, red_y))
- # Drawing blue box on top of the background
- blue_box = Image.new("RGB", (blue_width, blue_height), (0, 0, 255))
- image.paste(blue_box, (blue_x, blue_y))
- images.append(image)
- return images
- # Function to add a watermark to the images
- def add_watermark(image, watermark_text, font_size=24, text_color=(0, 0, 0)):
- font = ImageFont.truetype("arial.ttf", font_size)
- draw = ImageDraw.Draw(image)
- text_bbox = draw.textbbox((0, 0), watermark_text, font=font)
- text_height = text_bbox[3] - text_bbox[1]
- draw.text(
- (10, image.height - text_height - 10),
- watermark_text,
- fill=text_color,
- font=font,
- )
- return image
- def main():
- # Argument parsing
- parser = argparse.ArgumentParser(description="Generate video with watermark.")
- parser.add_argument(
- "-W", "--width", type=int, default=600, help="Width of the images"
- )
- parser.add_argument(
- "-H", "--height", type=int, default=600, help="Height of the images"
- )
- parser.add_argument(
- "-n", "--num_images", type=int, default=10, help="Number of images to generate"
- )
- args = parser.parse_args()
- # Generate images
- images = generate_image(args.width, args.height, args.num_images)
- # Add watermark and save images
- image_filenames = []
- for i, image in enumerate(images):
- watermark_text = f"pyro.flv - slide {i+1:03d}"
- image_with_watermark = add_watermark(image, watermark_text)
- filename = f"img_{i+1}_{args.width}x{args.height}.png"
- image_with_watermark.save(filename)
- image_filenames.append(filename)
- # Create video from images
- with imageio.get_writer(
- "webdriver_torso.mp4",
- fps=1,
- quality=8,
- macro_block_size=1,
- ffmpeg_log_level="quiet",
- ) as _:
- for filename in image_filenames:
- image = imageio.imread(filename)
- _.append_data(image)
- print(
- f"[{mp4_title}]: Video created successfully in the current working directory."
- )
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement