I had the same problem two weeks ago. This is what I wrote:
def resize_photo(self,content,size):
img = Image.open(ContentFile(content))
img.thumbnail(size, Image.ANTIALIAS)
io = StringIO.StringIO()
img.save(io, 'PNG')
return ContentFile(io.getvalue())
Hope this helps.
-Brian
On Dec 22, 4:41 am, Merrick <[email protected]> wrote:
> Thank you I tried that and I still get the same error.
>
> I spent a little more time looking at PIL / Image.py and cleaning up
> the code. From what I can tell the Image.open method is having trouble
> with what I am passing to it.
>
> def resize_image(file, size=(50, 50)):
> from PIL import Image
> from cStringIO import StringIO
> from django.core.files.base import ContentFile
>
> image_data = StringIO(file.read())
>
> ### this line below is where the issue is ###
> image = Image.open(image_data)
>
> if image.mode not in ('L', 'RGB'):
> image = image.convert('RGB')
> image.thumbnail(size, Image.ANTIALIAS)
> o = StringIO()
> image.save(o, "JPEG")
> return ContentFile(o.getvalue())
>
> This is how I call it:
> picture = pform.cleaned_data['picture']
> thumbnail_content = resize_image(picture)
>
> Thank you for looking at this.
>
> On Dec 21, 3:08 pm, "[email protected]" <[email protected]> wrote:
>
> > From what I can tell your not wrapping the thumbnailfilein
> > ContentFile your just returning the rawfilefrom the string IO. To
> > use the the ImageField django provides you must provide it with afile
> > that is in a special wrapper called ContentFile. I would suggest
> > trying this:
>
> > from django.core.files.base import ContentFile (import this function
> > at the top of your view or where ever you put def resize_image)
>
> > change the last line of def resize_image from "return o.getvalue()" to
> > "return ContentFile(o.getvalue())"
>
> > I would also recommend changing "new_profile.picture.save(filename,
> > thumbnail_content)" to "new_profile.picture.save(filename,
> > thumbnail_content, save=False)" so that you are not hitting the
> > database twice.
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---