Package flumotion :: Package component :: Package converters :: Package overlay :: Module genimg
[hide private]

Source Code for Module flumotion.component.converters.overlay.genimg

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  import os 
 23   
 24  __version__ = "$Rev: 7875 $" 
 25  directory = os.path.split(os.path.abspath(__file__))[0] 
 26  fontpath = os.path.join(directory, 'Vera.ttf') 
 27  logopath = directory 
 28  TEXT_XOFFSET = 6 
 29  TEXT_YOFFSET = 6 
 30  WIDTH = 36 
 31  BORDER = 4 
 32  FONT_SIZE = 22 
 33   
 34   
35 -def generateOverlay(text, 36 showFlumotion, 37 showCC, 38 showXiph, 39 width, height):
40 """Generate an transparent image with text + logotypes rendered on top 41 of it suitable for mixing into a video stream 42 @param text: text to put in the top left corner 43 @type text: str 44 @param showFlumotion: if we should show the flumotion logo 45 @type showFlumotion: bool 46 @param showCC: if we should show the Creative Common logo 47 @type showCC: bool 48 @param showXiph: if we should show the xiph logo 49 @type showXiph: bool 50 @param width: width of the image to generate 51 @type width: int 52 @param height: height of the image to generate 53 @type height: int 54 @returns: raw image and if images or if text overflowed 55 @rtype: 3 sized tuple of string and 2 booleans 56 """ 57 from PIL import Image 58 from PIL import ImageDraw 59 from PIL import ImageFont 60 61 image = Image.new("RGBA", (width, height)) 62 draw = ImageDraw.Draw(image) # inheriting color mode 63 64 subImages = [] 65 if showXiph: 66 subImages.append(os.path.join(logopath, 'xiph.36x36.png')) 67 if showCC: 68 subImages.append(os.path.join(logopath, 'cc.36x36.png')) 69 if showFlumotion: 70 subImages.append(os.path.join(logopath, 'fluendo.36x36.png')) 71 72 imagesOverflowed = False 73 74 offsetX = BORDER 75 for subPath in subImages: 76 sub = Image.open(subPath) 77 subX, subY = sub.size 78 offsetY = height - subY - BORDER 79 image.paste(sub, (offsetX, offsetY), sub) 80 if (offsetX + subX) > width: 81 imagesOverflowed = True 82 offsetX += subX + BORDER 83 84 textOverflowed = False 85 if text: 86 font = ImageFont.truetype(fontpath, FONT_SIZE) 87 draw.text((TEXT_XOFFSET+2, TEXT_YOFFSET+2), 88 text, font=font, fill='black') 89 draw.text((TEXT_XOFFSET, TEXT_YOFFSET), 90 text, font=font) 91 textWidth = draw.textsize(text, font=font)[0] + TEXT_XOFFSET 92 if textWidth > width: 93 textOverflowed = True 94 95 buf = image.tostring() 96 97 return buf, imagesOverflowed, textOverflowed
98 99 if __name__ == '__main__': 100 print generateOverlay('Testing', True, True, True, 128, 196)[0] 101