File: gddemo.py

package info (click to toggle)
python-gd 0.52debian-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 128 kB
  • ctags: 153
  • sloc: ansic: 1,605; python: 157; makefile: 45
file content (64 lines) | stat: -rw-r--r-- 1,465 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python

import gd, os, cStringIO, urllib2, sys

fontlist = [
    '/usr/lib/python2.4/site-packages/reportlab/fonts/PenguinAttack.ttf'
    '/usr/share/fonts/truetype/freefont/FreeSans.ttf',
    '/usr/share/fonts/truetype/ttf-bitstream-vera/Vera.ttf',
    ]

fontpath = '.'
for f in fontlist:
    if os.path.exists(f):
        fontpath = fontpath + ':' + os.path.dirname(f)
        FONT = os.path.basename(f)
        break

os.environ["GDFONTPATH"] = fontpath

try:
    FONT
except NameError:
    print "no fonts found"
    sys.exit(1)

def simple():
    im = gd.image((200, 200))

    white = im.colorAllocate((255, 255, 255))
    black = im.colorAllocate((0, 0, 0))
    red = im.colorAllocate((255, 0, 0))
    blue = im.colorAllocate((0, 0, 255))

    im.colorTransparent(white)
    im.interlace(1)

    im.rectangle((0,0),(199,199),black)
    im.arc((100,100),(195,175),0,360,blue)
    im.fill((100,100),red)

    print im.get_bounding_rect(FONT, 12.0, 0.0, (10, 100), "Hello Python")

    im.string_ttf(FONT, 20.0, 0.0, (10, 100), "Hello Python", black)

    f=open("xx.png","w")
    im.writePng(f)
    f.close()

    f=open("xx.jpg", "w")
    im.writeJpeg(f,100)
    f.close()

    f=cStringIO.StringIO()
    im.writePng(f)
    print "PNG size:", len(f.getvalue())
    f.close()
    
    f = urllib2.urlopen("http://www.gnu.org/graphics/gnu-head-sm.jpg")
    im = gd.image(f, "jpg")
    f.close()
    print "GNU Image Size:", im.size()

simple()