将Base64字符串加载到Python图像库中

时间:2013-11-11 14:39:52

标签: python django image base64 python-imaging-library

我通过ajax将图像作为base64字符串发送到django。在我的django视图中,我需要调整图像大小并将其保存在文件系统中。

这是一个base64字符串(简化):

data:image/jpeg;base64,/9j/4AAQSkZJRg-it-keeps-going-for-few-more-lines=

我尝试使用下面的python代码在PIL中打开它:

img = cStringIO.StringIO(request.POST['file'].decode('base64'))
image = Image.open(img)
return HttpResponse(image, content_type='image/jpeg')

我正在尝试显示上传的图片,但是firefox抱怨'The image cannot be displayed because it contains error'

我无法弄清楚我的错误。

SOLUTION:

pic = cStringIO.StringIO()

image_string = cStringIO.StringIO(base64.b64decode(request.POST['file']))

image = Image.open(image_string)

image.save(pic, image.format, quality = 100)

pic.seek(0)

return HttpResponse(pic, content_type='image/jpeg')

4 个答案:

答案 0 :(得分:13)

<强> SOLUTION:

将打开的PIL图像保存到类似文件的对象可以解决问题。

pic = cStringIO.StringIO()
image_string = cStringIO.StringIO(base64.b64decode(request.POST['file']))
image = Image.open(image_string)
image.save(pic, image.format, quality = 100)
pic.seek(0)
return HttpResponse(pic, content_type='image/jpeg')

答案 1 :(得分:2)

POSTed字符串(data:image/jpeg;base64,)的开头是标题,应在解码前删除。 否则图像会损坏。

photo = request.POST['photo'].partition('base64,')[2]
image_data = b64decode(photo)
someobject.photo.save('user.jpg', ContentFile(image_data), save=True)

答案 2 :(得分:0)

class PhotoHandler(webapp2.RequestHandler):
    def get(self):
        rawbase64 = 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAIs0lEQVR4Xu2aeXRTVR7Hf/dma9KmpbvaNm9LLVAodgSlHpXFsjnCMFWcgcGhUxcYQJCl1LHK5qAwg4Ds6gEFjsczCmfKSEFZZY7itAdGpaU0kISELsomFJomTfPunfPKNKRY2pTmhQLNf8n73d/9fj/53fvuve8huMs/6C73D10AuirgLifQNQTu8gLomgS7hsCtHAJp8fGhR8+ccdxKDbeyArDAMPstdvvAOw4An8T3RQqS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YDC8HN7/MUf0ijVrMWDvXaA1sVcKt0NDzcUDh2dPaxPYzZj2bSM7AOk9IBfPF2h7p2d6KiqAUnpDzUihAI3xfoAQDcRMnXVwd1Kk7K/PyA6gcQ4QhNEqVsgPHTRchZCovhEBqlDQusP/iY4YOaYoYvDQWV8mRZg7+g+31T44ABguN3zM7++JnfmXX+54rlNYU7AVqKOuqjh3aqvPENsy5u/1oAAwGo0aFSd8royKHVJvNgGtv35bTwGH6SGkZyqoWR70f8wp3c/G9fbXREfiggKgUSClKHXU0+/rBg0i4aOy0hBSNOu7tnB7lFh75WLkhInLlCp0cGdcWEuv1XTEa4ttgwfg6lwQh+9L+se9efMG4pjGVwMAIQRuqxlq9++GmPyFR/exsX0C7rKVhEEFIOngMzMNYQ9kfIFdjh6NADACNcOB/qmsGhSme3ZPQrfddzSAJnMDqy/HaImy8bFRPQUafVl77rNUFPAzv7ZgBr0C2hIU7OtdAIJNvLP111UBne0fCbaergoINvHO1l9XBXS2fyTYeroqINjEO1t//wNQk+JfjM4XQQAAAABJRU5ErkJggg=='
        pic = cStringIO.StringIO()
        image_string = cStringIO.StringIO(base64.b64decode(rawbase64))
        image = Image.open(image_string)
        image.save(pic, image.format, quality = 100)
        pic.seek(0)
        self.response.headers['Content-Type'] = 'image/png'
        self.response.write(pic.read())

答案 3 :(得分:0)

感谢Praveen的python2解决方案,但我发现python3版本将是:

import io
import base64
pic = io.BytesIO()
image_string = io.BytesIO(base64.b64decode(base64_str))
image = Image.open(image_string)
image.save(pic, image.format, quality=100)
pic.seek(0)
return HttpResponse(pic, content_type='image/jpeg')

请注意data:image/jpeg;base64,部分实际上不是base64字符串,所以如果你的base64字符串包含你应该删除它:

base64_str = base64_uri[23:]