tags:

views:

28

answers:

2

Hi there

I am saving a image path to my model image field with custom save function. then i realised that I am just saving the path and not an actual object. my question is how to convert that path to a object using PIL.

A: 

how to convert that path to a object using PIL

If by object you mean reading the image using PIL then you can do the following:

from PIL import Image
image = Image.open(<path>)

I don't know how if you mean something else.

Manoj Govindan
That is what I tried but I get an error:Exception Value:_committedI am getting images form youtube videos with urllib.urlretrieve then once thats done I Image.open(the url of the saved image) and get that error.
Harry
I assumed that you meant file path of the images rather than URLs. I don't think `open()` is meant for URLs. You'll have to read the image data from the URL and then use `fromstring` or similar.
Manoj Govindan
The image is already saved to a path on my machine. then I use that path of the image to open()
Harry
Can you post the complete error trace?
Manoj Govindan
A: 

The image saved correctly with urllib.urlretrieve(image_name, image_path). then i want to do a

Image.open(image_path)

but get the error:

AttributeError at /admin/featured/item/6/

_committed

Request Method:     POST
Request URL:    http://localhost:8000/admin/featured/item/6/
Django Version:     1.3 pre-alpha SVN-13431
Exception Type:     AttributeError
Exception Value:    

_committed

Exception Location:     /Library/Python/2.6/site-packages/PIL/Image.py in __getattr__, line 512
Python Executable:  /usr/bin/python
Python Version:     2.6.1

this is my code

        regex = re.compile(r"^(http://)?(www\.)?(youtube\.com/watch\?v=)?(?P&lt;id&gt;[A-Za-z0-9\-=_]{11})")
        match = regex.match(self.link)
        if not match:
            video_id = ''
        video_id = match.group('id')
        img_url = 'http://img.youtube.com/vi/%s/0.jpg' % video_id        
        urllib.urlretrieve(img_url, os.path.join(settings.MEDIA_ROOT, 'featured/%s.jpg' % video_id))

        image = Image.open(os.path.join(settings.MEDIA_ROOT, 'featured/%s.jpg' % video_id))

        self.image=image
        super(Item, self).save()
Harry
Is `image` of type `models.ImageField`? http://docs.djangoproject.com/en/dev/ref/models/fields/#imagefield
Manoj Govindan
Take a look at this answer: http://stackoverflow.com/questions/1308386/programmatically-saving-image-to-django-imagefield/1309682#1309682
Manoj Govindan