summaryrefslogtreecommitdiff
path: root/utils/img2js.py
blob: ceab6bf7543132fa6202eb6368823b6f79b25544 (plain)
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
#!/usr/bin/env python

#
# Convert image to Javascript compatible base64 Data URI
# Copyright (C) 2018 The noVNC Authors
# Licensed under MPL 2.0 (see docs/LICENSE.MPL-2.0)
#

import sys, base64

try:
    from PIL import Image
except:
    print "python PIL module required (python-imaging package)"
    sys.exit(1)


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

fname = sys.argv[1]
var   = sys.argv[2]

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

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

raw = open(fname).read()

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