Try Replacing thumbnails = ImageWithThumbsField(upload_to='your preferred location ', sizes=( your preferred size) For second thumbnail use different size. This might work
Amitt Bhardwj Blog: amittbhardwj.wordpress.com Github: https://github.com/amittbhardwj On Jul 22, 2015 12:52 AM, "divyanshi kathuria" <[email protected]> wrote: > 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. > -- 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/CAJWhTLpo3v%2BV9h-C8m6xibdDws%2BoLKQvGB1gQZuxvSHtrDh6OA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.

