• <i id='8lrSl'><tr id='8lrSl'><dt id='8lrSl'><q id='8lrSl'><span id='8lrSl'><b id='8lrSl'><form id='8lrSl'><ins id='8lrSl'></ins><ul id='8lrSl'></ul><sub id='8lrSl'></sub></form><legend id='8lrSl'></legend><bdo id='8lrSl'><pre id='8lrSl'><center id='8lrSl'></center></pre></bdo></b><th id='8lrSl'></th></span></q></dt></tr></i><div id='8lrSl'><tfoot id='8lrSl'></tfoot><dl id='8lrSl'><fieldset id='8lrSl'></fieldset></dl></div>
    1. <small id='8lrSl'></small><noframes id='8lrSl'>

    2. <legend id='8lrSl'><style id='8lrSl'><dir id='8lrSl'><q id='8lrSl'></q></dir></style></legend>
      <tfoot id='8lrSl'></tfoot>

        • <bdo id='8lrSl'></bdo><ul id='8lrSl'></ul>
      1. 将 Base64 字符串加载到 Python 图像库中

        时间:2023-08-30
        <i id='mLzD6'><tr id='mLzD6'><dt id='mLzD6'><q id='mLzD6'><span id='mLzD6'><b id='mLzD6'><form id='mLzD6'><ins id='mLzD6'></ins><ul id='mLzD6'></ul><sub id='mLzD6'></sub></form><legend id='mLzD6'></legend><bdo id='mLzD6'><pre id='mLzD6'><center id='mLzD6'></center></pre></bdo></b><th id='mLzD6'></th></span></q></dt></tr></i><div id='mLzD6'><tfoot id='mLzD6'></tfoot><dl id='mLzD6'><fieldset id='mLzD6'></fieldset></dl></div>

        <small id='mLzD6'></small><noframes id='mLzD6'>

      2. <tfoot id='mLzD6'></tfoot>
        • <bdo id='mLzD6'></bdo><ul id='mLzD6'></ul>

              <tbody id='mLzD6'></tbody>

              <legend id='mLzD6'><style id='mLzD6'><dir id='mLzD6'><q id='mLzD6'></q></dir></style></legend>
                  本文介绍了将 Base64 字符串加载到 Python 图像库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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字符串(简体):

                  Here is a base64 string(simplified):

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

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

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

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

                  我正在尝试将上传的图片显示回来,但是firefox抱怨'图片无法显示,因为它包含错误'

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

                  我无法弄清楚我的错误.

                  I couldn't figure out my mistake.

                  解决方案:

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

                  推荐答案

                  解决方案:

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

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

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

                  这篇关于将 Base64 字符串加载到 Python 图像库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:检查字符串是否使用 Python 以 base64 编码 下一篇:如何在python中将文本编码为base64

                  相关文章

                  <legend id='CmB4r'><style id='CmB4r'><dir id='CmB4r'><q id='CmB4r'></q></dir></style></legend>

                    <small id='CmB4r'></small><noframes id='CmB4r'>

                      <bdo id='CmB4r'></bdo><ul id='CmB4r'></ul>
                  1. <tfoot id='CmB4r'></tfoot>

                  2. <i id='CmB4r'><tr id='CmB4r'><dt id='CmB4r'><q id='CmB4r'><span id='CmB4r'><b id='CmB4r'><form id='CmB4r'><ins id='CmB4r'></ins><ul id='CmB4r'></ul><sub id='CmB4r'></sub></form><legend id='CmB4r'></legend><bdo id='CmB4r'><pre id='CmB4r'><center id='CmB4r'></center></pre></bdo></b><th id='CmB4r'></th></span></q></dt></tr></i><div id='CmB4r'><tfoot id='CmB4r'></tfoot><dl id='CmB4r'><fieldset id='CmB4r'></fieldset></dl></div>