Guest User

Keyforge Wallpaper Generator w/ Comments

a guest
Feb 7th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.64 KB | None | 0 0
  1. import json, requests, math, sys
  2. from PIL import Image
  3.  
  4. def generate_wallpaper(argv):
  5.     #determine the number of columns and rows based off of user input
  6.     cols = int(argv[1])
  7.     rows = math.ceil(36/cols)
  8.  
  9.     #get json data about the deck from the api
  10.     response = requests.get('https://www.keyforgegame.com/api/decks/' + argv[0] + '/?links=cards')
  11.     data = json.loads(response.content)
  12.  
  13.     #get a list of card ids from the json
  14.     cardlist = data['data']['_links']['cards']
  15.     #get the data for each card in the deck
  16.     carddata = data['_linked']['cards']
  17.  
  18.     #create a blank image that matches the size of the rows and columns
  19.     wallpaper = Image.new('RGB', (300*cols,420*rows))
  20.  
  21.     #the offsets will be used to make sure each image is pasted in the correct
  22.     #place in the blank image
  23.     x_offset = 0
  24.     y_offset = 0
  25.     max_offset = 300*cols
  26.    
  27.     for card in cardlist:
  28.         #using the link to the card image, download the image
  29.         image_link = [x['front_image'] for x in carddata if x['id'] == card][0]
  30.         card_image = Image.open(requests.get(image_link, stream=True).raw)
  31.         #paste the downloaded image into the blank image
  32.         wallpaper.paste(card_image, (x_offset, y_offset))
  33.  
  34.         #update the offsets for the next card
  35.         x_offset += 300
  36.         if x_offset >= max_offset:
  37.             x_offset = 0
  38.             y_offset += 420
  39.  
  40.     #save the file
  41.     wallpaper.save('keyforge_wallpaper.jpg')
  42.  
  43. if __name__ == "__main__":
  44.     #this is where the program starts
  45.     #it passes the arguements you specified to the generate_wallpaper function
  46.     generate_wallpaper(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment