]> git.proxmox.com Git - mirror_novnc.git/blob - utils/img2js.py
Fix chinese translation for "Disconnect"
[mirror_novnc.git] / utils / img2js.py
1 #!/usr/bin/env python
2
3 #
4 # Convert image to Javascript compatible base64 Data URI
5 # Copyright (C) 2018 The noVNC Authors
6 # Licensed under MPL 2.0 (see docs/LICENSE.MPL-2.0)
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))