首页 > 解决方案 > 将文件字段编码和解码为字节

问题描述

我的目标是将文件对象(用户上传的视频)更改为字节,然后将其分块,然后再次将这些块更改为图像或帧。以下代码是来自 django 应用程序的片段。

def handle_uploaded_file(f):
    with open('./chunks_made.txt', 'wb+') as destination:
        for chunk in f.chunks():
            print(type(chunk))
            print(len(chunk))
            destination.write(chunk)
            chunk_length = len(chunk)
    read_batches(len(chunk))

def read_batches(chunk_size):
    with open('./chunks_made.txt', 'rb') as file:
        content = file.read(chunk_size)
        frame = cv2.imdecode(content, cv2.IMREAD_COLOR)
        plt.imshow(frame)
        plt.show()

调用这些函数的进程视图:

def process(request):
    
    video_file = request.FILES['video']
    handle_uploaded_file(video_file)
    data = 'some_data'
    return render(request, 'video/result.html', {'data':video_file})

我不知道如何将字节解码为帧作为真实图像。

标签: pythondjangobyte

解决方案


推荐阅读