python - 将文件字段编码和解码为字节
问题描述
我的目标是将文件对象(用户上传的视频)更改为字节,然后将其分块,然后再次将这些块更改为图像或帧。以下代码是来自 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})
我不知道如何将字节解码为帧作为真实图像。
解决方案
推荐阅读
- android - 我需要在 Kotlin 中使用按钮在 List 中添加输入
- ruby - 无法让 Cocoapods 在混合 Ruby 环境(OSX Sierra)中工作
- python - 由于“未定义名称'结果'”而出现错误
- javascript - 这些 javascript 函数以什么顺序运行?
- javascript - React - 调用history.push后错误的组件渲染
- angular - Angularjs条件要求不起作用
- python - 如何遍历特定字段的所有键值对的json对象?
- python - 如何将列表中已有的单词分解为单个字符?
- python - 如何将此信息放在列中?
- swift - 使用 @fetchRequest(entity:) 处理 SwiftUI macOS 应用程序崩溃