如何将 Django uploadedfile.InMemory 转换为 numpy 数组或图像
How to convert Django uploadedfile.InMemory into numpy array or Image
#Views.py-- 我想将上传的图片文件转换成numpy数组(cv2.imread)
def upload(request):
if request.method == 'POST' and request.FILES['image_file']:
f = request.FILES['image_file']
myfile = str(f.read())
array_np = cv2.imread(myfile)
您可以将字节转换为 int 并使用 cv2.imdecode
对其进行解码。然后你会得到一个cv2图像数组。
image = cv2.imdecode(numpy.frombuffer(myfile , numpy.uint8), cv2.IMREAD_UNCHANGED)
#Views.py-- 我想将上传的图片文件转换成numpy数组(cv2.imread)
def upload(request):
if request.method == 'POST' and request.FILES['image_file']:
f = request.FILES['image_file']
myfile = str(f.read())
array_np = cv2.imread(myfile)
您可以将字节转换为 int 并使用 cv2.imdecode
对其进行解码。然后你会得到一个cv2图像数组。
image = cv2.imdecode(numpy.frombuffer(myfile , numpy.uint8), cv2.IMREAD_UNCHANGED)