I am always amazed by the support here, thank you. After seeing Brian's code I switched resize_image() to be a model method and then I switched from saving the form to saving the profile
profile = request.user.get_profile() profile.save_picture(pform.cleaned_data['picture'].read()) instead of new_profile = pform.save(commit=False) new_profile.picture.save(filename, thumbnail_content) Merrick On Dec 22, 1:10 pm, brianmac44 <anon1...@gmail.com> wrote: > With my code, what are you using as "content"? > > I'm using: form.cleaned_data['source'].read() > > So my code looks something like this: > PHOTO_MEDIUM_SIZE = 400,400 > source_image = form.cleaned_data['source'].read() > resized_image = resize_photo(source_image,PHOTO_MEDIUM_SIZE)) > photos.save(image_name,resized_image) > > You may want to also want to check the image file you are using for > testing, interlaced PNG's are not supported by > PIL.http://www.pythonware.com/library/pil/handbook/format-png.htm > > -Brian > > On Dec 22, 2:46 pm,Merrick<merr...@gmail.com> wrote: > > > I got the same result with brianmac44's code. I also verified that the > > my resize_image() works when opening the file from the local > > filesystem - so I am somehow not passing the file in a manner that > > Image.open likes. > > > On Dec 22, 4:45 am, brianmac44 <anon1...@gmail.com> wrote: > > > > 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<merr...@gmail.com> 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, "j...@zigzap.com" <jlb2...@gmail.com> 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 django-users@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 -~----------~----~----~----~------~----~------~--~---