Advertisement
opexxx

img_to_base64.py

Jun 3rd, 2014
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  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.
  21.  
  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 =  "![ChangeMe](data:image/{0};base64,{1})"
  39.  
  40. # format options
  41. HTML = 1        # one line, nested in TEMPLATE
  42. BASE64 = 2      # one line, pure base64 encoded output
  43. HTML_WRAP = 3   # wrapped HTML output, nested in TEMPLATE
  44.  
  45. # width fot text wrap
  46. HTML_WRAP_WIDTH = 79
  47.  
  48.  
  49. def convert_to_base64(filename, image_type, format=HTML):
  50.     """Read the image file and encode it with base64.
  51.  
  52.    Return the image file either in an HTML img tag or as plain base64 text.
  53.    """
  54.     img = open(filename, 'rb')
  55.     data = base64.b64encode(img.read())
  56.     img.close()
  57.  
  58.     if format in [HTML, HTML_WRAP]:
  59.         text = TEMPLATE.format(image_type, data)
  60.         if format == HTML_WRAP:
  61.             text = '\n'.join(textwrap.wrap(text, HTML_WRAP_WIDTH))
  62.         return text
  63.     # else
  64.     if format == BASE64:
  65.         return data
  66.     # else
  67.     return ''
  68.  
  69.  
  70. def main(args):
  71.     """Verify the format of the input file and print the base64 encoded text.
  72.  
  73.    Supported file formats: 'png' and 'jpeg'.
  74.    """
  75.     filename = args[0]
  76.     image_type = imghdr.what(filename)
  77.  
  78.     if image_type not in ['png', 'jpeg', 'gif']:
  79.         print "{0}: image file should be PNG, JPG or GIF.".format(sys.argv[0])
  80.         sys.exit(1)
  81.     # else
  82.     print convert_to_base64(filename, image_type, format=HTML_WRAP)
  83.  
  84.  
  85. if __name__ == "__main__":
  86.     if len(sys.argv) == 1:
  87.         print "{0}: missing image file argument.".format(sys.argv[0])
  88.         sys.exit(0)
  89.     else:
  90.         main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement