Guest User

Untitled

a guest
May 26th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # --------------------------------------------------------------------
  3. # test stuff
  4.  
  5. from optparse import OptionParser
  6. import Image, ImageFont, ImageDraw, sys, codecs
  7.  
  8. if __name__ == "__main__":
  9. parser = OptionParser()
  10. parser.add_option("-l", "--label")
  11. parser.add_option("-o", "--out")
  12. parser.add_option("--font")
  13. parser.add_option("-c", "--color") # font color
  14. parser.add_option("-s", "--size") # font size
  15.  
  16. parser.add_option("-t", "--theme") # light or dark
  17. parser.add_option("-b", "--box") # big, medium, small
  18.  
  19. (options, args) = parser.parse_args()
  20.  
  21. theme = (options.theme, "light")[options.theme is None]
  22. box = (options.box, "big")[options.box is None]
  23.  
  24. if box == 'big':
  25. size = 22
  26. offset = 40
  27. elif box == 'medium':
  28. size = 18
  29. offset = 20
  30. else:
  31. size = 12
  32. offset = 10
  33.  
  34. if theme == 'light':
  35. color = 'black'
  36. else:
  37. color = 'white'
  38. #optional values
  39. base_image = "base_%s_%s.png" % (theme, box)
  40. logo_image = "logo_%s_%s.png" % (theme, box)
  41. end_image = "base_right_%s_%s.png" % (theme, box)
  42.  
  43.  
  44. size = (options.size, size)[options.size is None]
  45. font = (options.font, "fonts/DejaVuSans.ttf")[options.font is None]
  46. color = (options.color, color)[options.color is None]
  47.  
  48. #required values
  49. path = options.out
  50. label = options.label.decode(sys.getfilesystemencoding())#.encode('utf-8')
  51.  
  52. font = ImageFont.truetype(font, int(size))
  53.  
  54. #draw font
  55. text_image = Image.new("RGBA", (10000, 100), (0,0,0,0))
  56. draw = ImageDraw.Draw(text_image)
  57. draw.text((0, 0), label, font = font, fill = color)
  58. l,t,r,b = text_image.getbbox()
  59. text_image = text_image.crop((l,t,r,b))
  60.  
  61. #get end image
  62. end_image = Image.open(end_image)
  63. el, et, er, eb = end_image.getbbox()
  64.  
  65. end_width = er-el
  66.  
  67. #get logo image
  68. logo_image = Image.open(logo_image)
  69. ll, lt, lr, lb = logo_image.getbbox()
  70.  
  71. logo_width = lr - ll
  72.  
  73. #get base image
  74. base_image = Image.open(base_image)
  75. bl, bt, br, bb = base_image.getbbox()
  76. base_image = base_image.resize((r + offset + logo_width + end_width, bb))
  77. bl, bt, br, bb = base_image.getbbox()
  78.  
  79. draw = ImageDraw.Draw(base_image)
  80. draw.text((logo_width + offset/2, (bb - (b-t))/2.2), label, font = font, fill = color)
  81.  
  82. base_image.paste(logo_image, (0,0))
  83. base_image.paste(end_image, (br-end_width, 0))
  84.  
  85. base_image.save(path, "PNG")
Add Comment
Please, Sign In to add comment