Created
February 17, 2013 13:29
-
-
Save theskumar/4971496 to your computer and use it in GitHub Desktop.
sublimetext 2 plugin to open the image file as data uri
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#encoding=utf8 | |
import sublime, sublime_plugin | |
import os, base64 | |
class Image2Base64(sublime_plugin.EventListener): | |
def on_load(self, view): | |
if view.file_name(): | |
fileName, fileExtension = os.path.splitext(view.file_name()) | |
if fileExtension.lower() in conv_extensions: | |
base64image = convert_image(view) | |
image = get_data_uri(base64image, fileExtension) | |
if image: | |
view.run_command("i2b64_change", {"image": image}) | |
class I2b64Change(sublime_plugin.TextCommand): | |
def run(self, edit, image): | |
view = self.view | |
view.replace(edit, sublime.Region(0, view.size()), image) | |
view.set_read_only(True) | |
view.run_command("select_all") | |
conv_extensions = [ | |
".jpg", | |
".jpeg", | |
".gif", | |
".png", | |
".bmp", | |
# ".ico" | |
] | |
def convert_image(view): | |
with open(view.file_name(), "rb") as image_file: | |
encoded_string = base64.b64encode(image_file.read()) | |
return encoded_string | |
return None | |
def get_data_uri(base64image, ext): | |
return 'data: image/' + ext.replace('.', '') + '; base64, ' + base64image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment