Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
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, brianmac44wrote: > 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 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 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 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" 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 -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
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, Merrickwrote: > 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 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 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" 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 -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
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, brianmac44wrote: > 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 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" 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 -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
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, Merrickwrote: > 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" 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 -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
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"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 -~--~~~~--~~--~--~---
Re: S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
>From what I can tell your not wrapping the thumbnail file in ContentFile your just returning the raw file from the string IO. To use the the ImageField django provides you must provide it with a file 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 -~--~~~~--~~--~--~---
S3Storage.py and Thumbnails using PIL (IOError / cannot identify image file)
I setup S3Storage as my default storage and can successfully upload images to S3 without overriding save on my profile_update below. Right now, when I submit a form with an image I get the original image uploaded in my S3 bucket not a thumbnail and an IO Error - "cannot identify image file" - traceback all the way down. I know its complicated but I have to imagine other people will attempt to create a thumbnail in memory and upload to S3 without saving locally. Thank you for helping. models.py class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) picture = models.ImageField(upload_to='profiles', null=True, blank=True) views.py === def resize_image(buf, size=(100, 100)): import Image as PILImage import cStringIO f = cStringIO.StringIO(buf) image = PILImage.open(f) if image.mode not in ('L', 'RGB'): image = image.convert('RGB') image.thumbnail(size, PILImage.ANTIALIAS) o = cStringIO.StringIO() image.save(o, "JPEG") return o.getvalue() @login_required(redirect_field_name='redirect_to') def profile_update(request): profile = request.user.get_profile() ... if request.method == 'POST': pform = ProfileForm(request.POST, request.FILES, instance=profile) else: pform = ProfileForm(instance=profile) if pform.is_valid() new_profile = pform.save(commit=False) if(pform.cleaned_data['picture']): raw_image_data = pform.cleaned_data['picture'].read() thumbnail_content = resize_image(raw_image_data) filename = pform.cleaned_data['picture'].name filename = os.path.splitext(filename)[0] + ".jpg" new_profile.picture.save(filename, thumbnail_content) new_profile.save() traceback === IOError at /profile/update cannot identify image file Traceback: File "/usr/lib/python2.4/site-packages/django/core/handlers/base.py" in get_response 86. response = callback(request, *callback_args, **callback_kwargs) File "/usr/lib/python2.4/site-packages/django/contrib/auth/ decorators.py" in __call__ 67. return self.view_func(request, *args, **kwargs) File "/var/mysite/views.py" in profile_update 705. thumbnail_content = resize_image (raw_image_data) File "/var/mysite/views.py" in resize_image 677. image = PILImage.open(f) File "/usr/lib/python2.4/site-packages/PIL/Image.py" in open 1745. raise IOError("cannot identify image file") Exception Type: IOError at /profile/update Exception Value: cannot identify image file --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---