deadlock
deadlock

Reputation: 7310

Downloading Image with urllib.urlretrieve() in Django

I've been attempting to work at this for hours but decided to turn to the experts here on stackoverflow.

I'm trying to download an image from a url:

import urllib    

originalphoto = urllib.urlretrieve(bundle.obj.url)
#originalphoto is being saved to the tmp directory in Ubuntu

This works and it saves the image in the tmp directory, but I need to modify this image by resizing it to a 250px by 250px image and then save it to a folder on my Desktop: /home/ubuntu/Desktop/resizedshots

The name of the original image is in bundle.obj.url, for example if bundle.obj.url is: http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg the name of the image is "09-09-201315-47-571378756077.jpg"

After the image is resized, I need to save is to this folder as 09-09-201315-47-571378756077small.jpg

As you can see, I'm adding in the word "small" to the end the file name. Once all of this is done, I would like to delete the temporary image file that was downloaded so that it doesn't take up the disk.

Any ideas on how this can be done?

Thanks

Upvotes: 0

Views: 2405

Answers (1)

User
User

Reputation: 14873

This is the definition:

def urlretrieve(url, filename=None, reporthook=None, data=None):

You can set the second argument to something you know and then do

import os
os.remove(something_you_know)

If you do not set the second argument you do this:

import urllib, os

url = 'http://photographs.500px.com/kyle/09-09-201315-47-571378756077.jpg'
file, headers = urllib.urlretrieve(url)
# do something
os.remove(file)

if os.remove does not work you still have the file open.

Upvotes: 2

Related Questions