Advertisement
Guest User

bpg image script, python

a guest
Nov 25th, 2020
3,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. from PIL import ImageFont, ImageDraw, Image
  2. from random import randint
  3.  
  4. import argparse
  5.  
  6. def get_rand_color(hi, lo):
  7.     return (randint(hi, lo), randint(hi, lo), randint(hi,lo))
  8.  
  9. def get_rand_light():
  10.     return get_rand_color(25, 75)
  11.  
  12. def get_rand_dark():
  13.     return get_rand_color(175, 225)
  14.  
  15. def create_bpg_image(filename, fg_color, bg_color, thread_num, font_path):
  16.     dimensions = (500, 500)
  17.  
  18.     image = Image.new("RGBA", dimensions, bg_color)
  19.     image_center = (dimensions[0] / 2, dimensions[1] / 2)
  20.  
  21.     draw = ImageDraw.Draw(image)
  22.  
  23.     # Figure out locations of the /bpg/ and thread number texts
  24.     bpg_font = ImageFont.truetype(font_path, 100)
  25.     bpg_str = "/bpg/"
  26.     bpg_strsize = draw.textsize(bpg_str, bpg_font)
  27.     bpg_location = (image_center[0] - bpg_strsize[0] / 2, image_center[1] - bpg_strsize[1], )
  28.  
  29.     num_font = ImageFont.truetype(font_path, 60)
  30.     threadnum_str = f"#{thread_num}"
  31.     threadnum_strsize = draw.textsize(threadnum_str, num_font)
  32.  
  33.     threadnum_location = (image_center[0] - threadnum_strsize[0] / 2 - 10,
  34.                           bpg_location[1] + bpg_strsize[1] + threadnum_strsize[1]/ 2)
  35.  
  36.     # Draw the text at the appropriate locations
  37.     draw.text(bpg_location, bpg_str, font=bpg_font, fill=fg_color, align="center")
  38.     draw.text(threadnum_location, threadnum_str, font=num_font, fill=fg_color, align="center")
  39.  
  40.     image.save(filename + ".png", format="png")
  41.  
  42. def setup_args():
  43.     parser = argparse.ArgumentParser(description="Output the /bpg/ image")
  44.     parser.add_argument("--threadnum", type=int, help='Display the thread number in the image.', required=True)
  45.     parser.add_argument("--out", type=str, help='Output filename for image.', default="out")
  46.     parser.add_argument("--font", type=str, help="Name of Font used to display text.", default="LiberationSerif-Bold.ttf")
  47.  
  48.     return parser.parse_args()
  49.  
  50. args = setup_args()
  51.  
  52. create_bpg_image(args.out + "_light", get_rand_light(), get_rand_dark(), args.threadnum, args.font)
  53. create_bpg_image(args.out + "_dark", get_rand_dark(), get_rand_light(), args.threadnum, args.font)
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement