Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python imghdr.what()在字节缓冲区上不起作用_Python_Python 3.x_Image_Png_Imghdr - Fatal编程技术网

Python imghdr.what()在字节缓冲区上不起作用

Python imghdr.what()在字节缓冲区上不起作用,python,python-3.x,image,png,imghdr,Python,Python 3.x,Image,Png,Imghdr,我正在开发一项功能,允许用户将图像上传到Python Flask web应用程序。上载的映像将转换为BytesIO缓冲区,并且永远不会保存到磁盘。我想使用imghdr.what()来确定图像类型(png、jpg等),并查看它是否是允许用户上传的格式。如果格式不允许,则上载将被拒绝 接下来,我写了下面的代码 image\u数据搜索(0) image\u type=imghdr.what(无,h=image\u data.read()) 图像_数据搜索(0) 不幸的是,当我用png图像调用它时,对

我正在开发一项功能,允许用户将图像上传到Python Flask web应用程序。上载的映像将转换为BytesIO缓冲区,并且永远不会保存到磁盘。我想使用
imghdr.what()
来确定图像类型(png、jpg等),并查看它是否是允许用户上传的格式。如果格式不允许,则上载将被拒绝

接下来,我写了下面的代码

image\u数据搜索(0)
image\u type=imghdr.what(无,h=image\u data.read())
图像_数据搜索(0)
不幸的是,当我用png图像调用它时,对于
image\u type
,它返回
None
。我用同样的图像尝试了下面的变化

image\u数据搜索(0)
image\u type=imghdr.what(“”,h=image\u data.read())
图像_数据搜索(0)
同样,对于
image\u type
,上面返回的
None

image\u数据搜索(0)
图像类型=imghdr.what(无,h=图像数据)
图像_数据搜索(0)
上面返回一个错误,
TypeError:“\u io.BytesIO”对象不可订阅

image\u数据搜索(0)
image\u type=imghdr.what(“”,h=image\u data.read)
图像_数据搜索(0)
上面返回相同的错误,
TypeError:“\u io.BytesIO”对象不可订阅

下面是我创建模拟png图像的
conftest.py
中的代码

@pytest.fixture
def test_image_BytesIO():
test_dir=os.path.dirname(os.path.realpath(_文件__))
local_path=os.path.join(test_dir'images/204Cat.png')
img_bytes=Pimage.open(本地路径).tobytes()
返回字节(img_字节)
我已经看过这些资源:



TLDR:如何让
imghdr.what()
处理BytesIO格式的图像?

为什么要删除
import
语句,这样就没有人可以运行代码了?
Pimage
来自哪里?
image\u data
的类型是什么?它来自哪里?它是如何创建的?为什么要删除
import
语句,这样就没有人可以运行您的代码?
Pimage
来自哪里?图像数据的类型是什么?它来自哪里?如何创建?