简体   繁体   English

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

[英]How to find file extension of base64 encoded image in Python

I have a base64 encoded image that I decode and save into an ImageField in Django.我有一个 base64 编码的图像,我将其解码并保存到 Django 中的 ImageField 中。 I want to give the file a random name, but I don't know the file extension.我想给文件一个随机名称,但我不知道文件扩展名。

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

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.例如,许多电子邮件攻击依赖于错误识别 mime 类型,以便毫无戒心的计算机执行它不应该执行的文件。 Fortunately, most image file extensions can be determined by looking at the first few bytes (after decoding the base64).幸运的是,大多数图像文件扩展名可以通过查看前几个字节(在解码 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 .但是,最佳实践可能是使用文件魔法,它可以通过 python 包访问,例如this onethis one

Most image file extensions are obvious from the mimetype.大多数图像文件扩展名在 mimetype 中是显而易见的。 For gif, pxc, png, tiff, and jpeg, the file extension is just whatever follows the 'image/' part of the mime type.对于 gif、pxc、png、tiff 和 jpeg,文件扩展名就是 mime 类型的“image/”部分之后的任何内容。 To handle the obscure types also, python does provide a standard package:为了处理晦涩的类型,python 确实提供了一个标准包:

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

It looks like mimetypes stdlib module supports data urls even in Python 2:看起来mimetypes stdlib 模块即使在 Python 2 中也支持数据 url:

>>> from mimetypes import guess_extension, guess_type
>>> guess_extension(guess_type("data:image/png;base64,")[0])
'.png'

Assume having base64 encoded in variable encoded_string , below code works for me:假设 base64 编码在变量encoded_string ,下面的代码对我encoded_string

from base64 import b64decode
import imghdr

encoded_string = 'image base64 encoded'

decoded_string = b64decode(var)
extension = imghdr.what(None, h=decoded_string)

You can use the mimetypes module - http://docs.python.org/2/library/mimetypes.html您可以使用 mimetypes 模块 - http://docs.python.org/2/library/mimetypes.html

Basically mimetypes.guess_extension(mine) should do the job.基本上mimetypes.guess_extension(mine)应该可以完成这项工作。

I have Written code in Lambda which will find the type of an image and also checks base64 is image or not.我在 Lambda 中编写了代码,它会查找图像的类型并检查 base64 是否为图像。

The following code will sure help someone.以下代码肯定会帮助某人。

import base64
import imghdr
def lambda_handler(event, context):
    image_data = event['img64']    # crate "json event" in lambda 
                                   # Sample JSON Event ========>  { "img64" : BASE64 of an Image }
                                   # Get BASE64 Data of image in image_data variable.
    sample = base64.b64decode(image_data)      # Decode the base64 data and assing to sample.

    for tf in imghdr.tests:
        res = tf(sample, None)
        if res:
            break;
    print("Extension OR Type of the Image =====>",res)
    if(res==None): # if res is None then BASE64 is of not an image.
            return {
            'status': 'False',
           'statusCode': 400,
           'message': 'It is not image, Only images allowed'
          }
    else:
        return 'It is image'  

Note :- The Above code is written Lambda (AWS) in python, You can copy and paste the following code to your local machine and test it as follows.注意:-以上代码是用python编写的Lambda(AWS),您可以将以下代码复制并粘贴到您的本地机器并进行如下测试。

import base64
import imghdr
image_data = "BASE64 OF AN IMAGE"
sample = base64.b64decode(image_data)      # Decode the base64 data and     assing to sample.

for tf in imghdr.tests:
    res = tf(sample, None)
    if res:
        break;
print("Extension OR Type of the Image =====>",res)
if(res==None):
    print('It is not image, Only images allowed')
else:
    print('It is image')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM