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