Advertisement
Guest User

HG /brg/ game table

a guest
Mar 3rd, 2017
45
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 ImageFont
  3. from PIL import ImageDraw
  4.  
  5. import glob
  6.  
  7. positions = [
  8.     (116, 151), (338, 152), (560, 152), (801, 152), (1023, 152), (1244, 151),
  9.     (117, 352), (339, 353), (560, 353), (801, 353), (1022, 353), (1244, 352),
  10.     (116, 553), (338, 554), (560, 554), (801, 554), (1022, 554), (1244, 553),
  11.     (117, 754), (339, 755), (560, 755), (801, 755), (1022, 755), (1244, 754) ]
  12.  
  13. textDW = 69
  14. textDH = 153
  15.  
  16. def getXY(pos):
  17.     return positions[pos-1]
  18.  
  19. def getScale(w, h, tw=139, th=123):
  20.     return max(tw/w, th/h)
  21.  
  22. def getNewSize(size, tw=139, th=123):
  23.     w, h = size
  24.     scale = getScale(w, h, tw=tw, th=th)
  25.     w, h = int(w*scale), int(h*scale)
  26.     return (max(tw,w), max(th,h))
  27.  
  28. def resizeImg(image, tw=139, th=123):
  29.     return image.resize(getNewSize(image.size, tw=tw, th=th))
  30.  
  31. def cropCenter(image, tw=139, th=123):
  32.     w, h = image.size
  33.     w, h = (w//2 - tw//2), (h//2 - th//2)
  34.     return image.crop((w, h, w + tw, h + th))
  35.  
  36. def resizeAndCropCenter(image, tw=139, th=123):
  37.     return cropCenter(resizeImg(image, tw=tw, th=th), tw=tw, th=th)
  38.  
  39. def addChampion(char, table):
  40.     char = resizeAndCropCenter(char, tw=95, th=86)
  41.     table.paste(char, (703, 40))
  42.  
  43. def addCharToTable(pos, char, table):
  44.     if pos==1:
  45.         addChampion(char, table)
  46.        
  47.     char = resizeAndCropCenter(char)
  48.     table.paste(char, getXY(pos))
  49.  
  50. def addChampionName(name, draw):
  51.     font = ImageFont.truetype("MTCORSVA.TTF", 18)
  52.    
  53.     w, h = draw.textsize(name, font)
  54.     draw.multiline_text((750 - w//2, 138 - h//2), name, (0,0,0), font=font, align="center")
  55.  
  56. def addNameToTable(pos, name, draw):
  57.     if pos==1:
  58.         addChampionName(name, draw)
  59.        
  60.     font = ImageFont.truetype("MTCORSVA.TTF", 20)
  61.    
  62.     w, h = draw.textsize(name, font)
  63.     cw, ch = getXY(pos)
  64.     cw += textDW
  65.     ch += textDH
  66.  
  67.     draw.multiline_text((cw - w//2, ch - h//2), name, (0,0,0), font=font, align="center")
  68.  
  69.  
  70. def main():
  71.     winners = glob.glob('images\*')
  72.  
  73.     #print("Winners list:", winners)
  74.  
  75.     table = Image.open("table.png")
  76.     draw = ImageDraw.Draw(table)
  77.  
  78.     for winner in winners:
  79.         char = Image.open(winner)
  80.        
  81.         name = winner.split('.')[0].split('\\')[1]
  82.         pos = 25
  83.         if ' ' in name:
  84.             pos, name = name.split(' ',1)
  85.             pos = int(pos)
  86.         else:
  87.             pos = int(name)
  88.             name = ''
  89.        
  90.         addCharToTable(pos, char, table)
  91.  
  92.         if name != '':
  93.             addNameToTable(pos, name, draw)
  94.  
  95.  
  96.     table.save("new_table.png", "PNG")
  97.  
  98. if __name__=="__main__":
  99.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement