<tfoot id='jRR56'></tfoot>
    1. <small id='jRR56'></small><noframes id='jRR56'>

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

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

        • <bdo id='jRR56'></bdo><ul id='jRR56'></ul>

        如何在 Python 中查找 base64 编码图像的文件扩展名

        How to find file extension of base64 encoded image in Python(如何在 Python 中查找 base64 编码图像的文件扩展名)

        <legend id='Bw5xG'><style id='Bw5xG'><dir id='Bw5xG'><q id='Bw5xG'></q></dir></style></legend>
            <bdo id='Bw5xG'></bdo><ul id='Bw5xG'></ul>

                <tbody id='Bw5xG'></tbody>

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

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

                  本文介绍了如何在 Python 中查找 base64 编码图像的文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个 base64 编码的图像,我将其解码并保存到 Django 中的 ImageField 中.我想给文件一个随机的名字,但我不知道文件扩展名.

                  I have a base64 encoded image that I decode and save into an ImageField in Django. I want to give the file a random name, but I don't know the file extension.

                  我在字符串前面添加了data:image/png;base64",我知道我可以做一些正则表达式来提取 mimetype,但我想知道是否有最佳实践方法可以从"data:image/png;base64," 可靠地转换为 ".png".当有人突然想要上传我不支持的奇怪图像文件类型时,我不想让我的手动功能中断.

                  I have "data:image/png;base64," prepended to the string and I know I could do some regex to extract the mimetype, but I'd like to know if there is a best practices way to go from "data:image/png;base64," to ".png" reliably. I don't want to have my handspun function break when someone suddenly wants to upload a strange image filetype that I don't support.

                  推荐答案

                  最好检查文件的内容,而不是依赖文件外部的东西.例如,许多电子邮件攻击依赖于错误识别 mime 类型,以便毫无戒心的计算机执行它不应该执行的文件.幸运的是,大多数图像文件扩展名可以通过查看前几个字节来确定(解码 base64 之后).不过,最佳实践可能是使用 file magic 可以通过python 包,例如 这个 或 这个.

                  It is best practices to examine the file's contents rather than rely on something external to the file. Many emails attacks, for example, rely on mis-identifying the mime type so that an unsuspecting computer executes a file that it shouldn't. Fortunately, most image file extensions can be determined by looking at the first few bytes (after decoding the base64). Best practices, though, might be to use file magic which can be accessed via a python packages such as this one or this one.

                  大多数图像文件的扩展名都可以从 mimetype 中看出.对于 gif、pxc、png、tiff 和 jpeg,文件扩展名就是 mime 类型的image/"部分之后的任何内容.为了处理晦涩的类型,python 确实提供了一个标准包:

                  Most image file extensions are obvious from the mimetype. For gif, pxc, png, tiff, and jpeg, the file extension is just whatever follows the 'image/' part of the mime type. To handle the obscure types also, python does provide a standard package:

                  >>> from mimetypes import guess_extension
                  >>> guess_extension('image/x-corelphotopaint')
                  '.cpt'
                  >>> guess_extension('image/png')
                  '.png'
                  

                  这篇关于如何在 Python 中查找 base64 编码图像的文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                      <i id='zMF25'><tr id='zMF25'><dt id='zMF25'><q id='zMF25'><span id='zMF25'><b id='zMF25'><form id='zMF25'><ins id='zMF25'></ins><ul id='zMF25'></ul><sub id='zMF25'></sub></form><legend id='zMF25'></legend><bdo id='zMF25'><pre id='zMF25'><center id='zMF25'></center></pre></bdo></b><th id='zMF25'></th></span></q></dt></tr></i><div id='zMF25'><tfoot id='zMF25'></tfoot><dl id='zMF25'><fieldset id='zMF25'></fieldset></dl></div>

                          <tbody id='zMF25'></tbody>
                        <legend id='zMF25'><style id='zMF25'><dir id='zMF25'><q id='zMF25'></q></dir></style></legend>
                      • <tfoot id='zMF25'></tfoot>
                      • <small id='zMF25'></small><noframes id='zMF25'>

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