I found the issue: it is the path to the image. When I change the problem
line to

image = Image.open(os.path.realpath('C:\Users\Lloyd\Desktop\Lloyd.png'))

it works, but I cannot hard code the path and I cannot predict where the
user will try to upload a file from. Isn't there a way to dynamically get
the path from the file upload dialogue where the user browses to it and
selects it?

Thanks...

On Fri, Nov 26, 2010 at 3:49 PM, Sithembewena Lloyd Dube
<zebr...@gmail.com>wrote:

> Hi all,
>
> I have a Photo model which should take an image, generate a thumbnail and
> save both the original and the thumbnail images. I registered the model in
> the admin site, but when I select an image to upload and click 'Save', I get
> an error as follows:
>
> Request Method:     POST
> Request URL:     http://127.0.0.1:8000/admin/browse/photo/add/
> Django Version:     1.2.3
> Exception Type:     IOError
> Exception Value:
>
> [Errno 2] No such file or directory: u'Lloyd.png'
>
> Exception Location:     C:\Python26\lib\site-packages\PIL\Image.py in open,
> line 1952
> Python Executable:     C:\Python26\python.exe
> Python Version:     2.6.5
>
> The model looks like this (the line where the error occurs is highlighted):
>
> class Photo(models.Model):
>      """
>      An avatar for a site member
>      """
>      title =                       models.CharField(max_length=50)
>      photo =                       models.ImageField(upload_to='photos/')
>      thumbnail =
> models.ImageField(upload_to='thumbnails/', editable=False)
>
>      def save(self):
>           from PIL import Image
>           from cStringIO import StringIO
>           from django.core.files.uploadedfile import SimpleUploadedFile
>
>           # Set our max thumbnail size in a tuple (max width, max height)
>           THUMBNAIL_SIZE = (65, 65)
>
>           # Open original photo which we want to thumbnail using PIL's
> Image
>           # object
>           image = Image.open(self.photo.name)
>
>           # Convert to RGB if necessary
>           # Thanks to Limodou on DjangoSnippets.org
>           # http://www.djangosnippets.org/snippets/20/
>           if image.mode not in ('L', 'RGB'):
>                image = image.convert('RGB')
>
>           # We use our PIL Image object to create the thumbnail, which
> already
>           # has a thumbnail() convenience method that contrains
> proportions.
>           # Additionally, we use Image.ANTIALIAS to make the image look
> better.
>           # Without antialiasing the image pattern artifacts may result.
>           image.thumbnail(THUMBNAIL_SIZE, Image.ANTIALIAS)
>
>           # Save the thumbnail
>           temp_handle = StringIO()
>           image.save(temp_handle, 'png')
>           temp_handle.seek(0)
>
>           # Save to the thumbnail field
>           suf = SimpleUploadedFile(os.path.split(self.photo.name)[-1],
>                temp_handle.read(), content_type='image/png')
>           self.thunbnail.save(suf.name+'.png', suf, save=False)
>
>           # Save this photo instance
>           super(Photo, self).save()
>
>           class Admin:
>                pass
>
>           def __str__(self):
>                return self.title
>
> What could the issue be? Any ideas welcome.
>
> Thanks.
> --
> Regards,
> Sithembewena Lloyd Dube
> http://www.lloyddube.com
>



-- 
Regards,
Sithembewena Lloyd Dube
http://www.lloyddube.com

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to