1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)
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