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: 6125 $" 
 25   
 26  directory = os.path.split(os.path.abspath(__file__))[0] 
 27  fontpath = os.path.join(directory, 'Vera.ttf') 
 28  logopath = directory 
 29   
 30  fluendoLogoPath = os.path.join(logopath, 'fluendo.36x36.png') 
 31  ccLogoPath = os.path.join(logopath, 'cc.36x36.png') 
 32  xiphLogoPath = os.path.join(logopath, 'xiph.36x36.png') 
 33   
 34  TEXT_XOFFSET = 6 
 35  TEXT_YOFFSET = 6 
 36  WIDTH = 36 
 37  BORDER = 8 
 38  FONT_SIZE = 22 
 39   
40 -def generate_overlay(filename, text, show_fluendo, show_cc, show_xiph, 41 width, height):
42 from PIL import Image 43 from PIL import ImageChops 44 from PIL import ImageDraw 45 from PIL import ImageFont 46 from PIL import ImageOps 47 48 image = Image.new("RGBA", (width, height)) 49 draw = ImageDraw.Draw(image) # inheriting color mode 50 51 if text: 52 font = ImageFont.truetype(fontpath, FONT_SIZE) 53 draw.text((TEXT_XOFFSET+2, TEXT_YOFFSET+2), 54 text, font=font, fill='black') 55 draw.text((TEXT_XOFFSET, TEXT_YOFFSET), 56 text, font=font) 57 58 # How many logos we're going to show 59 logos = len([i for i in (show_fluendo, show_cc, show_xiph) if i]) - 1 60 61 # This is really *NOT* the optimal way of doing this. 62 # We should really find out a better way of adding an image on 63 # another image (with an offset) 64 65 imax = max(width, height) 66 y_corr = -(abs(width - height) + WIDTH + BORDER) 67 68 if show_xiph: 69 xiph = Image.open(xiphLogoPath) 70 xiph = ImageOps.expand(xiph, imax) 71 xiph = ImageChops.offset(xiph, -width + (WIDTH*logos), y_corr) 72 image = ImageChops.add_modulo(image, xiph) 73 logos -= 1 74 75 if show_cc: 76 cc = Image.open(ccLogoPath) 77 cc = ImageOps.expand(cc, imax) 78 cc = ImageChops.offset(cc, -width + (WIDTH*logos), y_corr) 79 image = ImageChops.add_modulo(image, cc) 80 logos -= 1 81 82 if show_fluendo: 83 fluendo = Image.open(fluendoLogoPath) 84 fluendo = ImageOps.expand(fluendo, imax) 85 fluendo = ImageChops.offset(fluendo, -width, y_corr) 86 image = ImageChops.add_modulo(image, fluendo) 87 88 if os.path.exists(filename): 89 os.unlink(filename) 90 91 image.save(filename, 'png') 92 93 if text: 94 return (draw.textsize(text, font=font)[0] + TEXT_XOFFSET > width) 95 else: 96 return False
97 98 if __name__ == '__main__': 99 #generate_overlay('test.png', 'Testing', True, True, True, 320, 240) 100 generate_overlay('test.png', 'Testing', True, True, True, 320, 240) 101