Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / public / vendor / noVNC / utils / img2js.py @ 5fcb9de8

History | View | Annotate | Download (914 Bytes)

1
#!/usr/bin/env python
2

    
3
#
4
# Convert image to Javascript compatible base64 Data URI
5
# Copyright 2011 Joel Martin
6
# Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
7
#
8

    
9
import sys, base64
10

    
11
try:
12
    from PIL import Image
13
except:
14
    print "python PIL module required (python-imaging package)"
15
    sys.exit(1)
16

    
17

    
18
if len(sys.argv) < 3:
19
    print "Usage: %s IMAGE JS_VARIABLE" % sys.argv[0]
20
    sys.exit(1)
21

    
22
fname = sys.argv[1]
23
var   = sys.argv[2]
24

    
25
ext = fname.lower().split('.')[-1]
26
if   ext == "png":            mime = "image/png"
27
elif ext in ["jpg", "jpeg"]:  mime = "image/jpeg"
28
elif ext == "gif":            mime = "image/gif"
29
else:
30
    print "Only PNG, JPEG and GIF images are supported"
31
    sys.exit(1)
32
uri = "data:%s;base64," % mime
33

    
34
im = Image.open(fname)
35
w, h = im.size
36

    
37
raw = open(fname).read()
38

    
39
print '%s = {"width": %s, "height": %s, "data": "%s%s"};' % (
40
        var, w, h, uri, base64.b64encode(raw))