Skip to content

Instantly share code, notes, and snippets.

@rafaelsq
Last active September 10, 2015 17:25
Show Gist options
  • Save rafaelsq/a619cc259e1c9707bcca to your computer and use it in GitHub Desktop.
Save rafaelsq/a619cc259e1c9707bcca to your computer and use it in GitHub Desktop.
Canvas Data to JPG
# -*- coding:utf-8 -*-
from cStringIO import StringIO
from PIL import Image
def _saveAsPNG(data, path):
output = open(path, 'wb')
output.write(data)
output.close()
def _saveAsJPG(data, path):
img = Image.open(StringIO(data))
img.save(path, 'JPEG', quality=100)
if __name__ == '__main__':
"""
source: http://stackoverflow.com/a/7774848/5277071
"""
# raw = document.getElementById('canvas').toDataURL()
raw = (
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCA'
'YAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9T'
'XL0Y4OHwAAAABJRU5ErkJggg=='
)
data = raw.split('base64,')[-1].decode('base64')
_saveAsPNG(data, '/tmp/dot.png')
_saveAsJPG(data, '/tmp/dot.jpg')
@rafaelsq
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment