Created
October 12, 2022 05:33
-
-
Save AdamQuixote/1256026be82c395a081f7fa31ff0a510 to your computer and use it in GitHub Desktop.
Creates fun ransom notes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Serialtypist.py - Break a string into words, generate an image using a random font for each word, and output | |
| an image with the words in the correct order, but slightly misaligned. | |
| Usage: serialtypist.py [options] string | |
| Options: | |
| -h, --help show this help message and exit | |
| -v, --verbose print extra information | |
| -s, --size=SIZE set the size of the output image in the format WIDTHxHEIGHT | |
| -o FILE, --output FILE output file (default: serialtypist.jpg) | |
| """ | |
| import argparse | |
| import os | |
| import random | |
| import shutil | |
| from PIL import Image | |
| from PIL import ImageDraw | |
| from PIL import ImageFont | |
| # get the command line arguments | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument("-v", "--verbose", help="print extra information", action="store_true") | |
| parser.add_argument("-o", "--output", help="output file", default="serialtypist.jpg") | |
| parser.add_argument("-s", "--size", help="set the size of the output image in the format WIDTHxHEIGHT", | |
| default="200x100") | |
| parser.add_argument("string", help="string to use") | |
| args = parser.parse_args() | |
| def gather_fonts(): | |
| """ | |
| checks for a 'serialtypist_fonts' folder with true type fonts in it | |
| if not found, copies all .ttf fonts from c:\windows\fonts to the folder | |
| """ | |
| if not os.path.exists('serialtypist_fonts'): | |
| os.makedirs('serialtypist_fonts') | |
| for font in os.listdir('c:\\windows\\fonts'): | |
| if font.endswith('.ttf') and not font.startswith('web') and not font.startswith('wing'): | |
| shutil.copy('c:\\windows\\fonts\\' + font, 'serialtypist_fonts') | |
| return True | |
| else: | |
| for font in os.listdir('c:\\windows\\fonts'): | |
| if font.endswith('.ttf') and not font.startswith('web') and not font.startswith('wing'): | |
| if not os.path.exists('serialtypist_fonts\\' + font): | |
| shutil.copy('c:\\windows\\fonts\\' + font, 'serialtypist_fonts') | |
| return True | |
| def generate_tiles(string): | |
| fonts = os.listdir('serialtypist_fonts') | |
| words = string.split() | |
| images = [] | |
| for word in words: | |
| image = Image.new("RGB", (len(word) * 20, 35), "white") | |
| font = random.choice(fonts) | |
| draw = ImageDraw.Draw(image) | |
| draw.text((0, 0), word, font=ImageFont.truetype('serialtypist_fonts\\' + font, 20), fill="black") | |
| images.append(image) | |
| return images | |
| def create_letter(images): | |
| width, height = args.size.split('x') | |
| width = int(width) | |
| height = int(height) | |
| letter = Image.new("RGB", (width, height), "white") | |
| ImageDraw.Draw(letter) | |
| x = 10 | |
| y = 6 | |
| for image in images: | |
| image_width, image_height = image.size | |
| offset_x = random.randint(0, 6) | |
| offset_y = random.randint(0, 6) | |
| rotation = random.randint(-5, 5) | |
| scale = random.randint(90, 100) / 100 | |
| image = image.resize((int(image_width * scale), int(image_height * scale))) | |
| image = image.rotate(rotation) | |
| letter.paste(image, (x + offset_x, y + offset_y)) | |
| x += image_width + 10 | |
| if x + image_width > width: | |
| x = 10 | |
| y += image_height + 10 | |
| return letter | |
| def main(): | |
| if args.verbose: | |
| print("Generating tiles...") | |
| gather_fonts() | |
| images = generate_tiles(args.string) | |
| if args.verbose: | |
| print("Creating letter...") | |
| letter = create_letter(images) | |
| letter.save(args.output) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment