how can I export multiple images using zipfile and urllib2

2024/10/14 19:24:04

I am trying to add multiple image files into my zip. I have searched around and knows how to add a single one. I tried to loop through multiple images then write into it but it didn't work.

I kind of did the same thing with txt format and it works that I can compress a few files into the zip but somehow not when with image.

# get all photos in db which will be a queryset as result
photos = Photo.objects.all()# loop through the queryset
for photo in photos:# open the image urlurl = urllib2.urlopen(photo.image.url)# get the image filename including extensionfilename = str(photo.image).split('/')[-1]f = StringIO()zip = ZipFile(f, 'w')zip.write(filename, url.read())
zip.close()
response = HttpResponse(f.getvalue(), content_type="application/zip")
response['Content-Disposition'] = 'attachment; filename=image-test.zip'
return response

This would give me the last image which in a way I can see why.

Answer

Don't create a new zip file in every iteration. Instead, write all the files to the same archive (which you instantiate before the loop):

f = StringIO()
zip = ZipFile(f, 'w')for photo in photos:url = urllib2.urlopen(photo.image.url)filename = str(photo.image).split('/')[-1]zip.write(filename, url.read())
zip.close()
https://en.xdnf.cn/q/117917.html

Related Q&A

XPATH for Scrapy

So i am using SCRAPY to scrape off the books of a website. I have the crawler working and it crawls fine, but when it comes to cleaning the HTML using the select in XPATH it is kinda not working out ri…

Kivy Removing elements from a Stack- / GridLayout

I made a pop-up. It is basically some rows of options (max. 5 rows).If I press the + button, there will be a new line of options.If I press the - button the last row should diasappear. Unfortunatelly i…

Bad timing when playing audio files with PyGame

When I play a sound every 0.5 second with PyGame:import pygame, timepygame.mixer.init() s = pygame.mixer.Sound("2.wav")for i in range(8):pygame.mixer.Channel(i).play(s)time.sleep(0.5)it doesn…

Using read_batch_record_features with an Estimator

(Im using tensorflow 1.0 and Python 2.7)Im having trouble getting an Estimator to work with queues. Indeed, if I use the deprecated SKCompat interface with custom data files and a given batch size, the…

How to insert integers into a list without indexing using python?

I am trying to insert values 0 - 9 into a list without indexing. For example if I have the list [4, 6, X, 9, 0, 1, 5, 7] I need to be able to insert the integers 0 - 9 into the placeholder X and test i…

Separate/reposition/translate shapes in image with pillow in python

I need to separate or translate or replace pixels in an image with python so as to all the shapes to share the same distance between each other and the limits of the canvas.background is white, shapes …

django.db.utils.OperationalError: (1045, Access denied for user user@localhost

I cant get my Django project to load my database correctly. It throws this error. Im running MariaDB with Django, and I uninstalled all MySQL I added the user by running:create database foo_db; create …

Paho Python Client with HiveMQ

i am developing a module in python that will allow me to connect my raspberry pi to a version of hivemq hosted on my pc.it connects normally but when i add hivemqs file auth plugin it doesnt seem to wo…

Comparison of value items in a dictionary and counting matches

Im using Python 2.7. Im trying to compare the value items in a dictionary.I have two problems. First is the iteration of values in a dictionary with a length of 1. I always get an error, because python…

how to send cookies inside post request

trying to send Post request with the cookies on my pc from get request #! /usr/bin/python import re #regex import urllib import urllib2 #get request x = urllib2.urlopen("http://www.example.com) #…