【问题标题】:django content_type of a image while testing测试时图像的 django content_type
【发布时间】:2015-11-22 22:38:06
【问题描述】:

我想获取图片的内容类型,我正在做

image = open(PATH_TO_IMAGE)

print(image.name.content_type)

告诉我 str 对象没有属性 content_type 的错误

如何获取内容类型?

我在测试中这样做,在我看来,我得到了图像的 content_type。我可以将图像传递给视图,但它没有给我内容类型

【问题讨论】:

标签: python django identification imghdr


【解决方案1】:

您可以使用Magic 库来猜测任何文件的类型。

import magic
image_type = magic.from_buffer(image.read(), mime = True)

或者

您可以按照其他答案中的建议使用Imghdr

【讨论】:

  • 我只是不想要 content_type。我想要我想要在库中传递的图像,并在库中检查 content_type.. 我想要一个在我的库中提供内容类型的图像类型
  • 你可以自己分配 content_type 变量,通过找出来
【解决方案2】:

尚未对此进行测试,但据我所知,您可以使用内置的 imghdr 模块并使用 print(imghdr.what(image)) 获取图像的格式类型

【讨论】:

    猜你喜欢
    • 2017-10-28
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 2019-02-12
    • 1970-01-01
    • 2015-08-18
    • 2012-01-26
    相关资源
    最近更新 更多