Yes. I would like to add easy-thumbnails too. Both are great!
On 22 Jul 2015 01:05, "Sadaf Noor" <[email protected]> wrote:

> In one of my projects I used sorl (
> https://github.com/mariocesar/sorl-thumbnail) instead, it was super easy
> and best fit for my project. Every time it asks for a new thumbnail BUT
> if it exists in its cache, then that one is returned. If it doesn't, a
> new one is generated and stored, then returned. So ultimately it saves. I
> don't know your use case. I am just saying it can be an option.
>
> 2015-07-22 1:22 GMT+06:00 divyanshi kathuria <[email protected]>
> :
>
>> I am using Django Rest framework. I have three fields in my Pin Model :
>> image,thumbnail_medium and thumbnail_small. I want to create two thumbnails
>> from the image. How to do that? I tried the following code : But it's not
>> working.
>> from django.db import models
>> from django.contrib.auth.models import User
>> from thumbs import ImageWithThumbsField
>> #from taggit.managers import TaggableManager
>> from PIL import Image
>> from cStringIO import StringIO
>> from django.core.files.uploadedfile import SimpleUploadedFile
>> import os
>>
>>
>> class Pin(models.Model):
>>
>>     url = models.SlugField(blank=True, null=True)
>>     title = models.TextField(blank=False,null=False,default='Untitled')
>>     description = models.TextField(blank=True, null=True)
>>     is_pro = models.BooleanField(default=False)
>>     is_hidden = models.BooleanField(default=False)
>>     is_for_sale = models.BooleanField(default=False)
>>     is_pro = models.BooleanField(default=False)
>>     price = models.DecimalField(blank=True, null=True, max_digits=10,
>> decimal_places=2 ,default=0)
>>     price_in_rs = models.BooleanField(default=False)
>>     size_in_inches = models.BooleanField(default=True)
>>     medium = models.TextField(blank=True, null=True)
>>     length = models.DecimalField(blank=True, null=True, max_digits=10,
>> decimal_places=2 ,default=0)
>>     width = models.DecimalField(blank=True, null=True, max_digits=10,
>> decimal_places=2 ,default=0)
>>     hearts = models.IntegerField(blank=True, null=True, default=0)
>>     image = models.ImageField(upload_to='pins/pin/originals/')
>>     thumbnail = models.ImageField(upload_to='pins/pin/thumbnails/')
>>     published = models.DateTimeField(auto_now_add=True)
>>     #tags = TaggableManager()
>>
>>     def __unicode__(self):
>>         return self.url
>>
>>     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 = (50, 50)
>>
>>         # Open original photo which we want to thumbnail using PIL's Image
>>         # object
>>         image = Image.open(self.image.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.image.name)[-1],
>>                 temp_handle.read(), content_type='image/png')
>>         self.thumbnail.save(suf.name+'.png', suf, save=False)
>>
>>         # Save this photo instance
>>         super(Pin, self).save()
>>
>> Can anyone figure out the problem here? Why is it not creating thumbnails?
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/django-users.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/13e46569-3641-438d-9201-fa079f7f89b2%40googlegroups.com
>> <https://groups.google.com/d/msgid/django-users/13e46569-3641-438d-9201-fa079f7f89b2%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
>   Md. Sadaf Noor (@sadaf2605 <https://twitter.com/sadaf2605>)
>  www.sadafnoor.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAAJ2eVoCTd36h-ofX7aSjY1vVzecSjjFzuUYbLUiwjRQvqbBKg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAAJ2eVoCTd36h-ofX7aSjY1vVzecSjjFzuUYbLUiwjRQvqbBKg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CA%2B4-nGp0n1-YOYX%2BUzoZ03wk1sUX1WPyhWVOfC3CEofR1gheqQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to