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

时间:2021-02-12 00:22:25

I'm sending images as base64 string through ajax to django. In my django view I need to resize the image and save it in the file system.

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

Here is a base64 string(simplified):

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

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

I tried to open this in PIL using the below python code:

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

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

I'm trying to display the uploaded image back, but firefox complains that 'The image cannot be displayed because it contains error'

我正在尝试显示上传的图像,但是firefox抱怨说“图像不能显示,因为它包含错误”

I couldn't figure out my mistake.

我想不出我的错误。

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 个解决方案

#1


12  

SOLUTION:

解决方案:

Saving the opened PIL image to a file-like object solves the issue.

将打开的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')

#2


2  

The beginning of the POSTed string (data:image/jpeg;base64,) is a header and should be removed before the decoding. The image is corrupted otherwise.

发布的字符串的开头(数据:image/jpeg;base64)是一个头,应该在解码之前删除。否则图像会被破坏。

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

#3


0  

class PhotoHandler(webapp2.RequestHandler):
    def get(self):
        rawbase64 = 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAIs0lEQVR4Xu2aeXRTVR7Hf/dma9KmpbvaNm9LLVAodgSlHp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+5+7YclUGoH6my/fERUgPeP78yg1mf605toGScuCotdqEEsmAriqvD6/LvFA0XFyKW+NKVJu80GZBKVOBIabGh4Gzz/eW+HdzWGsgu4Z2XD80EagPiu/sxcBhvVF5uxFJ5PvCAB8Ip+MsCc/LilKl2LUDm7LlFKlJGfO1znK6qIftuze3a7HZW3lbum67HOAYDC8HN7/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())

#4


0  

Thanks to Praveen's solution for python2, but I find out python3 version would be:

感谢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')

Please notice data:image/jpeg;base64, part is not actually the base64 string so if your base64 string contains that you should remove it:

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

base64_str = base64_uri[23:]

#1


12  

SOLUTION:

解决方案:

Saving the opened PIL image to a file-like object solves the issue.

将打开的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')

#2


2  

The beginning of the POSTed string (data:image/jpeg;base64,) is a header and should be removed before the decoding. The image is corrupted otherwise.

发布的字符串的开头(数据:image/jpeg;base64)是一个头,应该在解码之前删除。否则图像会被破坏。

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

#3


0  

class PhotoHandler(webapp2.RequestHandler):
    def get(self):
        rawbase64 = 'iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAIs0lEQVR4Xu2aeXRTVR7Hf/dma9KmpbvaNm9LLVAodgSlHpXFsjnCMFWcgcGhUxcYQJCl1LHK5qAwg4Ds6gEFjsczCmfKSEFZZY7itAdGpaU0kISELsomFJomTfPunfPKNKRY2pTmhQLNf8n73d/9fj/53fvuve8huMs/6C73D10AuirgLifQNQTu8gLomgS7hsCtHAJp8fGhR8+ccdxKDbeyArDAMPstdvvAOw4An8T3RQqSaLHZClox5xcAgWV/Rz24zFppLZEDlGwVICSx25BGOclsNp+7gfA2AfA8b8AifctsPzVeDvNSTtkA8AzzJCCUZrXZFt8sACPLLqCUfmWx2w/cdgAkwQLLfioCTLPZbD+1YABxDLvzlN02oiVzLMuymNI3rXb7c3KZl7UCpORGlh1AANKtNtuK9poQGC4XgHxjsdsPtbdte+JlGwKtiUhJSdEjl6tXA6UYITWhSmqxWCxn2yNcmh+sVuvp9rRpKbbDAHgD/6ibuI9UVlY6Wx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9fbXREfiggKgUSClKHXU0+/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())

#4


0  

Thanks to Praveen's solution for python2, but I find out python3 version would be:

感谢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')

Please notice data:image/jpeg;base64, part is not actually the base64 string so if your base64 string contains that you should remove it:

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

base64_str = base64_uri[23:]