-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathimg_to_base64.py
executable file
·94 lines (72 loc) · 2.59 KB
/
img_to_base64.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
Image to BASE64
===============
Take an image file and encode it with BASE64. Put the encoded data in
an "img" HTML tag.
Author: Laszlo Szathmary, 2011 ([email protected])
Website: https://ubuntuincident.wordpress.com/2011/04/17/embed-images-in-html-pages/
GitHub: https://github.com/jabbalaci/Bash-Utils
Usage:
------
./img_to_base64.py <image_file>
By default, the data is nested in an HTML tag and the output
is wrapped. These settings can be customized.
The output is printed to the standard output.
Sample output:
--------------
<img class='inline-image' src='data:image/gif;base64,R0lGODlhIgAbAPMPAGxsbNbW1v
/rhf/ge//3kf/Ub9/f3/b29oeHh/7LZv/0juazTktLS8WSLf//mf///yH5BAAAAAAALAAAAAAiABsAA
ASA8MlJq7046827/2AojiTVnI1xlFZjBisruU7tPCiqjg2h/L9KA2HgCQS5pE7UGLgwAhyCWWjYrrWE
owFgJqyEsDi82HZDja/jyGaXuV7rYE6fv8+gtLXA7/OtcCEGSoQMUyEHAQgAjI2OAAgBIwcGAZaXmAE
7Mpydnp+goaKjFBEAOw==' />
Last update: 2017-01-08 (yyyy-mm-dd)
"""
import base64
import imghdr
import sys
import textwrap
from pathlib import Path
# you can change the 'class' attribute or you can add more attributes
TEMPLATE = "<img class='inline-image'" + \
" src='data:image/{0};base64,{1}' />"
# format options
HTML = 1 # one line, nested in TEMPLATE
BASE64 = 2 # one line, pure base64 encoded output
HTML_WRAP = 3 # wrapped HTML output, nested in TEMPLATE
# width fot text wrap
HTML_WRAP_WIDTH = 79
def convert_to_base64(filename, image_type, format=HTML):
"""Read the image file and encode it with base64.
Return the image file either in an HTML img tag or as plain base64 text.
"""
img = open(filename, 'rb')
data = base64.b64encode(img.read())
img.close()
if format in [HTML, HTML_WRAP]:
text = TEMPLATE.format(image_type, data)
if format == HTML_WRAP:
text = '\n'.join(textwrap.wrap(text, HTML_WRAP_WIDTH))
return text
# else
if format == BASE64:
return data
# else
return ''
def main(args):
"""Verify the format of the input file and print the base64 encoded text.
Supported file formats: 'png' and 'jpeg'.
"""
filename = args[0]
image_type = imghdr.what(filename)
if image_type not in ['png', 'jpeg', 'gif']:
print("{0}: image file should be PNG, JPG or GIF.".format(sys.argv[0]))
sys.exit(1)
# else
print(convert_to_base64(filename, image_type, format=HTML_WRAP))
if __name__ == "__main__":
if len(sys.argv) == 1:
print("{0}: missing image file argument".format(Path(sys.argv[0]).name))
sys.exit(0)
else:
main(sys.argv[1:])