在我的 django 服务器上,我通过命令行使用 imagemagick 创建了一个图像文件。
cmd = local_file + " -crop 50%x50%+10+10 " + temp_final_file
out, err = subprocess.Popen( cmd.split(), 
                  stdin = subprocess.PIPE,
                  stdout = subprocess.PIPE,
                  stderr = subprocess.PIPE ).communicate()
然后我将文件返回给客户端:
    mt = mimetypes.guess_type( temp_final_file )[0]
    with open( temp_final_file, "rb" ) as f:
       response = HttpResponse( f.read(), mimetype=mt )
    return response
我想知道是否可以避免编写本地文件而直接返回字节?