Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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 使用Django从服务器获取多个映像_Python_Django_Web - Fatal编程技术网

Python 使用Django从服务器获取多个映像

Python 使用Django从服务器获取多个映像,python,django,web,Python,Django,Web,我正在尝试从服务器下载多个图像文件。我正在使用Django作为后端。 关于单幅图像的问题已经解决了,我尝试了代码,它可以在单幅图像上工作。在我的应用程序中,我希望在一个HTTP连接中下载多个图像 from PIL import Image img = Image.open('test.jpg') img2 = Image.open('test2.png') response = HttpResponse(content_type = 'image/jpeg') response2 = Htt

我正在尝试从服务器下载多个图像文件。我正在使用Django作为后端。 关于单幅图像的问题已经解决了,我尝试了代码,它可以在单幅图像上工作。在我的应用程序中,我希望在一个HTTP连接中下载多个图像

from PIL import Image

img = Image.open('test.jpg')
img2 = Image.open('test2.png')

response = HttpResponse(content_type = 'image/jpeg')
response2 = HttpResponse(content_type = 'image/png')

img.save(response, 'JPEG')
img2.save(response2, 'PNG')

return response #SINGLE

如何同时获取
img
img2
。我想的一种方法是压缩这两个图像,并根据客户端大小进行解压缩,但我认为这不是一个好的解决方案。有没有办法处理这个问题?

您可以将文件/图像添加到ZIP文件中,并在响应中返回该文件/图像。我认为这是最好的办法

以下是一些示例代码,说明了如何实现(从):



否则(如果您不喜欢ZIP文件),您可以从客户端发出单独的请求。

您可以将文件/图像添加到ZIP文件中,并在响应中返回该文件/图像。我认为这是最好的办法

以下是一些示例代码,说明了如何实现(从):



否则(如果您不喜欢ZIP文件),您可以从客户端发出单独的请求。

我四处查看,找到了一个使用磁盘上临时ZIP文件的旧解决方案:

它需要一些更新,这应该可以工作(在django 2.0上测试)


现在,这将获取my.png并在my.zip中写入10次,然后发送。

我四处寻找,找到了一个使用磁盘上临时zip文件的旧解决方案:

它需要一些更新,这应该可以工作(在django 2.0上测试)


现在,这将获取my.png并在my.zip中写入10次,然后发送。

如果使用for循环遍历图像所在的目录会怎么样?如果使用for循环遍历图像所在的目录会怎么样?
def zipFiles(files):
    outfile = StringIO() # io.BytesIO() for python 3
    with zipfile.ZipFile(outfile, 'w') as zf:
        for n, f in enumarate(files):
            zf.writestr("{}.csv".format(n), f.getvalue())
    return outfile.getvalue()

zipped_file = zip_files(myfiles)
response = HttpResponse(zipped_file, content_type='application/octet-stream')
response['Content-Disposition'] = 'attachment; filename=my_file.zip'
import tempfile, zipfile
from django.http import HttpResponse
from wsgiref.util import FileWrapper

def send_zipfile(request):
    """                                                                         
    Create a ZIP file on disk and transmit it in chunks of 8KB,                 
    without loading the whole file into memory. A similar approach can          
    be used for large dynamic PDF files.                                        
    """
    temp = tempfile.TemporaryFile()
    archive = zipfile.ZipFile(temp, 'w', zipfile.ZIP_DEFLATED)
    for index in range(10):
        filename = 'C:/Users/alex1/Desktop/temp.png' # Replace by your files here.  

        archive.write(filename, 'file%d.png' % index) # 'file%d.png' will be the
                                                      # name of the file in the
                                                      # zip
    archive.close()

    temp.seek(0)
    wrapper = FileWrapper(temp)

    response = HttpResponse(wrapper, content_type='application/zip')
    response['Content-Disposition'] = 'attachment; filename=test.zip'

    return response