Load an html5 canvas into a PIL Image with Django

前端 未结 4 1579
孤独总比滥情好
孤独总比滥情好 2020-12-15 09:54

I\'m trying to get the contents of an html5 canvas and pass it to my django server, where it will then be manipulated with PIL and saved as a PNG. Here\'s what I have so far

4条回答
  •  旧时难觅i
    2020-12-15 10:27

    import re
    
    datauri = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=='
    
    imgstr = re.search(r'base64,(.*)', datauri).group(1)
    
    output = open('output.png', 'wb')
    
    output.write(imgstr.decode('base64'))
    
    output.close()
    

    or if you need to load it into PIL :

    import cStringIO
    
    tempimg = cStringIO.StringIO(imgstr.decode('base64'))
    
    im = Image.open(tempimg)
    

提交回复
热议问题