Upload image file, resize using PIL, then save into ImageField - what to save to ImageField?
I can save an uploaded image to a FileField like this (where "ProductFile" is a model) and "TempFile" is an ImageField: uploadedfile = request.FILES['uploadfile'] ProductFile.objects.create(FileName=UploadDate=datetime.datetime.now(), TempFile=uploadedfile) But, how do I manipulate the image size and then save to this model? I am working with PIL, but I can't save a PIL Image to a ImageField. Can I save the file to disk using PIL and then pass in the file path and name to the model? If so, what is the syntax for saving the ImageFile when you are no longer working with the original uploadedfile object? -- 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.
Re: Upload image file, resize using PIL, then save into ImageField - what to save to ImageField?
On Mar 18, 6:10 am, robinne wrote: > I can save an uploaded image to a FileField like this (where > "ProductFile" is a model) and "TempFile" is an ImageField: > > uploadedfile = request.FILES['uploadfile'] > ProductFile.objects.create(FileName=UploadDate=datetime.datetime.now(), > TempFile=uploadedfile) > > But, how do I manipulate the image size and then save to this model? I > am working with PIL, but I can't save a PIL Image to a ImageField. Not a direct answer, but you may want to have a look at image-kit: http://bitbucket.org/jdriscoll/django-imagekit/wiki/Home Does what you need (and more), and it's a breeze to use. -- 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.
Re: Upload image file, resize using PIL, then save into ImageField - what to save to ImageField?
I think Satchmo uses http://code.google.com/p/sorl-thumbnail/ I think it uses PIL underneath a layer of abstraction. That might work for you if you are just wanting to generate alternate versions of uploaded images. Alex On Mar 18, 12:10 am, robinne wrote: > I can save an uploaded image to a FileField like this (where > "ProductFile" is a model) and "TempFile" is an ImageField: > > uploadedfile = request.FILES['uploadfile'] > ProductFile.objects.create(FileName=UploadDate=datetime.datetime.now(), > TempFile=uploadedfile) > > But, how do I manipulate the image size and then save to this model? I > am working with PIL, but I can't save a PIL Image to a ImageField. Can > I save the file to disk using PIL and then pass in the file path and > name to the model? If so, what is the syntax for saving the ImageFile > when you are no longer working with the original uploadedfile object? -- 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.
Re: Upload image file, resize using PIL, then save into ImageField - what to save to ImageField?
The Imagefile is just a pointer to file. Here is what I do to upload and resize an image. I hope it helps. file_to_open = settings.MEDIA_ROOT+'//profiles//'+ user.username+'-'+file.name fd = open(file_to_open, 'wb+') if file.multiple_chunks(): for chunk in file.chunks(): fd.write(chunk) else: fd.write(file.read()) fd.close() # Resize the image ms = Image.open(file_to_open) size = 145,132 ms.thumbnail(size, Image.ANTIALIAS) ms.save(file_to_open, "JPEG") On Mar 18, 10:03 am, Alex Robbins wrote: > I think Satchmo useshttp://code.google.com/p/sorl-thumbnail/ > I think it uses PIL underneath a layer of abstraction. That might work > for you if you are just wanting to generate alternate versions of > uploaded images. > > Alex > > On Mar 18, 12:10 am, robinne wrote: > > > I can save an uploaded image to a FileField like this (where > > "ProductFile" is a model) and "TempFile" is an ImageField: > > > uploadedfile = request.FILES['uploadfile'] > > ProductFile.objects.create(FileName=UploadDate=datetime.datetime.now(), > > TempFile=uploadedfile) > > > But, how do I manipulate the image size and then save to this model? I > > am working with PIL, but I can't save a PIL Image to a ImageField. Can > > I save the file to disk using PIL and then pass in the file path and > > name to the model? If so, what is the syntax for saving the ImageFile > > when you are no longer working with the original uploadedfile object? -- 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.
Re: Upload image file, resize using PIL, then save into ImageField - what to save to ImageField?
Thanks for all your replies. I ended up using sorl-thumbnail, which I had already installed, but I didn't think it kept aspect ratio of image because you are required to enter w and h for thumbnail image size using ThumbnailField. A quick test shows that it resizes and keeps same aspect ratio. So, I am able to upload a file and save it to a ThumbnailField as smaller version, which is all I needed. Thanks! On Mar 18, 7:03 am, Alex Robbins wrote: > I think Satchmo useshttp://code.google.com/p/sorl-thumbnail/ > I think it uses PIL underneath a layer of abstraction. That might work > for you if you are just wanting to generate alternate versions of > uploaded images. > > Alex > > On Mar 18, 12:10 am, robinne wrote: > > > I can save an uploaded image to a FileField like this (where > > "ProductFile" is a model) and "TempFile" is an ImageField: > > > uploadedfile = request.FILES['uploadfile'] > > ProductFile.objects.create(FileName=UploadDate=datetime.datetime.now(), > > TempFile=uploadedfile) > > > But, how do I manipulate the image size and then save to this model? I > > am working with PIL, but I can't save a PIL Image to a ImageField. Can > > I save the file to disk using PIL and then pass in the file path and > > name to the model? If so, what is the syntax for saving the ImageFile > > when you are no longer working with the original uploadedfile object? -- 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.