Guest User

Untitled

a guest
Mar 11th, 2024
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.61 KB | None | 0 0
  1. from PIL import Image
  2. from PIL import ImageDraw
  3. from PIL import ImageFont
  4. import csv
  5. from tqdm import tqdm
  6.  
  7. def generate_card(mg_name, mg_family, mg_type, mg_nature,
  8.                  portrait_filename, portrait_x_offset, portrait_y_offset,
  9.                  nature_x_offset, nature_y_offset):
  10.     family_font = ImageFont.truetype('StrongGirls DEMO.otf', 90)
  11.     nature_font = ImageFont.truetype('StrongGirls DEMO.otf', 60)
  12.     dot_font = ImageFont.truetype('dejavu-sans.book.ttf', 90)
  13.     name_font = ImageFont.truetype('StrongGirls DEMO.otf', 200)
  14.     card = Image.open(r"base.png")
  15.     underline = Image.open(r"brown_underline.png")
  16.     portrait = Image.open(portrait_filename)
  17.     card_width = card.size[0]
  18.     portrait_width = portrait.size[0]
  19.     x_offset = int((0.5)*(card_width - portrait_width)) + portrait_x_offset
  20.     Image.Image.paste(card, portrait, (x_offset, 1100 + portrait_x_offset), portrait)
  21.     draw = ImageDraw.Draw(card)
  22.     name_anchor = (550, 200)
  23.     draw.text(name_anchor, mg_name, font=name_font, fill=(0, 0, 0), stroke_width=0, stroke_fill="black")
  24.     family_type_anchor = (450,550)
  25.     draw.text(family_type_anchor, "Family: " + mg_family + " ", font=family_font, fill=(0, 0, 0), stroke_width=0, stroke_fill="black")
  26.     end = draw.textbbox(text="Family: " + mg_family + " ", font=family_font, xy=family_type_anchor)[2]
  27.     draw.text((end,family_type_anchor[1]-10), u"\u2022", font=dot_font, fill=(0, 0, 0))
  28.     end = draw.textbbox(text=u"\u2022", font=dot_font, xy=(end,family_type_anchor[1]-10))[2]
  29.     draw.text((end,family_type_anchor[1]), " Type: " + mg_type, font=family_font, fill=(0, 0, 0), stroke_width=0, stroke_fill="black")
  30.     nature_anchor = (200,900)
  31.     draw.text(nature_anchor, " Nature: " + mg_nature, font=nature_font, fill=(0, 0, 0), stroke_width=0, stroke_fill="black")
  32.     Image.Image.paste(card, underline, (nature_anchor[0]+14,nature_anchor[1]+60))
  33.     card.save(mg_name + ".png")
  34.    
  35. with open('data_sheet.csv') as f:
  36.     reader = csv.DictReader(f, delimiter=';')
  37.     for row in tqdm(reader):
  38.         mg_name = row['name']
  39.         mg_family = row['family']
  40.         mg_type = row['type']
  41.         mg_nature = row['nature']
  42.         portrait_filename = row['portrait_filename']
  43.         portrait_x_offset = int(row['portrait_x_offset'])
  44.         portrait_y_offset = int(row['portrait_y_offset'])
  45.         nature_x_offset = int(row['nature_x_offset'])
  46.         nature_y_offset = int(row['nature_y_offset'])
  47.         generate_card(mg_name, mg_family, mg_type, mg_nature, portrait_filename,
  48.                      portrait_x_offset, portrait_y_offset, nature_x_offset, nature_y_offset)
  49.  
Advertisement
Add Comment
Please, Sign In to add comment