Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 92 lines (70 sloc) 2.572 kb
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
1 #!/usr/bin/env python
2
3 """
4 Image to BASE64
5 ===============
6
7 Take an image file and encode it with BASE64. Put the encoded data in
8 an "img" HTML tag.
9
10 Author: Laszlo Szathmary, 2011 (jabba.laci@gmail.com)
11 Website: https://ubuntuincident.wordpress.com/2011/04/17/embed-images-in-html-pages/
12 GitHub: https://github.com/jabbalaci/Bash-Utils
13
14 Usage:
15 ------
16
17 ./img_to_base64.py <image_file>
18 By default, the data is nested in an HTML tag and the output
19 is wrapped. These settings can be customized.
20 The output is printed to the standard output.
5f5974e Laszlo Szathmary linted
authored
21
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
22 Sample output:
23 --------------
24
25 <img class='inline-image' src='data:image/gif;base64,R0lGODlhIgAbAPMPAGxsbNbW1v
26 /rhf/ge//3kf/Ub9/f3/b29oeHh/7LZv/0juazTktLS8WSLf//mf///yH5BAAAAAAALAAAAAAiABsAA
27 ASA8MlJq7046827/2AojiTVnI1xlFZjBisruU7tPCiqjg2h/L9KA2HgCQS5pE7UGLgwAhyCWWjYrrWE
28 owFgJqyEsDi82HZDja/jyGaXuV7rYE6fv8+gtLXA7/OtcCEGSoQMUyEHAQgAjI2OAAgBIwcGAZaXmAE
29 7Mpydnp+goaKjFBEAOw==' />
30 """
31
32 import sys
33 import imghdr
34 import base64
35 import textwrap
36
37 # you can change the 'class' attribute or you can add more attributes
38 TEMPLATE = "<img class='inline-image'" + \
5f5974e Laszlo Szathmary linted
authored
39 " src='data:image/{0};base64,{1}' />"
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
40
41 # format options
5f5974e Laszlo Szathmary linted
authored
42 HTML = 1 # one line, nested in TEMPLATE
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
43 BASE64 = 2 # one line, pure base64 encoded output
44 HTML_WRAP = 3 # wrapped HTML output, nested in TEMPLATE
45
46 # width fot text wrap
47 HTML_WRAP_WIDTH = 79
48
5f5974e Laszlo Szathmary linted
authored
49
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
50 def convert_to_base64(filename, image_type, format=HTML):
51 """Read the image file and encode it with base64.
5f5974e Laszlo Szathmary linted
authored
52
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
53 Return the image file either in an HTML img tag or as plain base64 text.
54 """
55 img = open(filename, 'rb')
5f5974e Laszlo Szathmary linted
authored
56 data = base64.b64encode(img.read())
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
57 img.close()
5f5974e Laszlo Szathmary linted
authored
58
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
59 if format in [HTML, HTML_WRAP]:
60 text = TEMPLATE.format(image_type, data)
61 if format == HTML_WRAP:
5f5974e Laszlo Szathmary linted
authored
62 text = '\n'.join(textwrap.wrap(text, HTML_WRAP_WIDTH))
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
63 return text
64 # else
65 if format == BASE64:
66 return data
67 # else
68 return ''
5f5974e Laszlo Szathmary linted
authored
69
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
70
71 def main(args):
72 """Verify the format of the input file and print the base64 encoded text.
5f5974e Laszlo Szathmary linted
authored
73
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
74 Supported file formats: 'png' and 'jpeg'.
75 """
76 filename = args[0]
77 image_type = imghdr.what(filename)
78
79 if image_type not in ['png', 'jpeg', 'gif']:
80 print "{0}: image file should be PNG, JPG or GIF.".format(sys.argv[0])
81 sys.exit(1)
82 # else
83 print convert_to_base64(filename, image_type, format=HTML_WRAP)
5f5974e Laszlo Szathmary linted
authored
84
a1da780 Laszlo Szathmary img_to_base64 added (really)
authored
85
86 if __name__ == "__main__":
87 if len(sys.argv) == 1:
88 print "{0}: missing image file argument.".format(sys.argv[0])
89 sys.exit(0)
90 else:
91 main(sys.argv[1:])
Something went wrong with that request. Please try again.