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

  • <tfoot id='xV1zz'></tfoot>
    <legend id='xV1zz'><style id='xV1zz'><dir id='xV1zz'><q id='xV1zz'></q></dir></style></legend>

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

        使用 base64 编码图像文件

        Encoding an image file with base64(使用 base64 编码图像文件)

              <legend id='S28yi'><style id='S28yi'><dir id='S28yi'><q id='S28yi'></q></dir></style></legend>
            1. <small id='S28yi'></small><noframes id='S28yi'>

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

                <bdo id='S28yi'></bdo><ul id='S28yi'></ul>

                  <tfoot id='S28yi'></tfoot>

                • 本文介绍了使用 base64 编码图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想使用 base64 模块将图像编码为字符串.我遇到了一个问题.如何指定要编码的图像?我尝试使用图像的目录,但这只会导致目录被编码.我希望对实际的图像文件进行编码.

                  I want to encode an image into a string using the base64 module. I've ran into a problem though. How do I specify the image I want to be encoded? I tried using the directory to the image, but that simply leads to the directory being encoded. I want the actual image file to be encoded.

                  编辑

                  我试过这个片段:

                  with open("C:Python26seriph1.BMP", "rb") as f:
                      data12 = f.read()
                      UU = data12.encode("base64")
                      UUU = base64.b64decode(UU)
                  
                      print UUU
                  
                      self.image = ImageTk.PhotoImage(Image.open(UUU))
                  

                  但我收到以下错误:

                  Traceback (most recent call last):
                    File "<string>", line 245, in run_nodebug
                    File "C:Python26GUI1.2.9.py", line 473, in <module>
                      app = simpleapp_tk(None)
                    File "C:Python26GUI1.2.9.py", line 14, in __init__
                      self.initialize()
                    File "C:Python26GUI1.2.9.py", line 431, in initialize
                      self.image = ImageTk.PhotoImage(Image.open(UUU))
                    File "C:Python26libsite-packagesPILImage.py", line 1952, in open
                      fp = __builtin__.open(fp, "rb")
                  TypeError: file() argument 1 must be encoded string without NULL bytes, not str
                  

                  我做错了什么?

                  推荐答案

                  我不确定我是否理解你的问题.我假设你正在做一些事情:

                  I'm not sure I understand your question. I assume you are doing something along the lines of:

                  import base64
                  
                  with open("yourfile.ext", "rb") as image_file:
                      encoded_string = base64.b64encode(image_file.read())
                  

                  当然,您必须先打开文件并读取其内容 - 您不能简单地将路径传递给编码函数.

                  You have to open the file first of course, and read its contents - you cannot simply pass the path to the encode function.

                  好的,这是您编辑原始问题后的更新.

                  Ok, here is an update after you have edited your original question.

                  首先,请记住在 Windows 上使用路径分隔符时使用原始字符串(字符串前缀为 'r'),以防止意外碰到转义字符.其次,PIL 的 Image.open 要么接受文件名,要么接受文件名(即对象必须提供 read、seek 和 tell 方法).

                  First of all, remember to use raw strings (prefix the string with 'r') when using path delimiters on Windows, to prevent accidentally hitting an escape character. Second, PIL's Image.open either accepts a filename, or a file-like (that is, the object has to provide read, seek and tell methods).

                  话虽如此,您可以使用 cStringIO 从内存缓冲区创建这样的对象:

                  That being said, you can use cStringIO to create such an object from a memory buffer:

                  import cStringIO
                  import PIL.Image
                  
                  # assume data contains your decoded image
                  file_like = cStringIO.StringIO(data)
                  
                  img = PIL.Image.open(file_like)
                  img.show()
                  

                  这篇关于使用 base64 编码图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                • <small id='BBk2j'></small><noframes id='BBk2j'>

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

                            <tbody id='BBk2j'></tbody>
                        • <legend id='BBk2j'><style id='BBk2j'><dir id='BBk2j'><q id='BBk2j'></q></dir></style></legend>