Thumbnails In Django

138 views
Skip to first unread message

divyanshi kathuria

unread,
Jul 21, 2015, 3:22:27 PM7/21/15
to django...@googlegroups.com
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?

Sadaf Noor

unread,
Jul 21, 2015, 3:37:11 PM7/21/15
to django...@googlegroups.com
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. 

--
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 django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
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.
For more options, visit https://groups.google.com/d/optout.



--
 Md. Sadaf Noor (@sadaf2605)
 www.sadafnoor.com

Robin Lery

unread,
Jul 21, 2015, 5:42:47 PM7/21/15
to django...@googlegroups.com

Yes. I would like to add easy-thumbnails too. Both are great!

Ezequiel Bertti

unread,
Jul 21, 2015, 5:51:59 PM7/21/15
to django...@googlegroups.com

easy-thumbnails +1


--
Ezequiel Bertti
E-Mail: ebe...@gmail.com
Cel: (21) 99188-4860

divyanshi kathuria

unread,
Jul 23, 2015, 5:09:57 PM7/23/15
to Django users, robi...@gmail.com


On Wednesday, July 22, 2015 at 3:12:47 AM UTC+5:30, Robin Lery wrote:

Yes. I would like to add easy-thumbnails too. Both are great!

I used easy-thumbnails. But it doesn't store the thumnails in my database.
I want three fields to get populated on loading the image : image, thumbnail_large,thumbnail_small. These apps just store the thumbnails in folders.

Amitt Bhardwj

unread,
Jul 24, 2015, 5:53:20 AM7/24/15
to django...@googlegroups.com

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

--

Nkansah Rexford

unread,
Jul 24, 2015, 6:55:56 AM7/24/15
to Django users, divyansh...@gmail.com
Might be unrelated to your specific problem, but generating thumbnails, I do mine in template directly, using ImageKit. Upon upload, I resize the image to a good size or convert the uploaded image to format of my taste, say 2048x1080.

Then in template, I just do {% thumbnail '400x500' model.image_field %} and the thumbnail is generated on the fly. The on-fly-generated-thumbnail is cached, so no performance issue is encountered upon second request.


Tao Huang

unread,
Jul 26, 2015, 7:17:11 AM7/26/15
to Django users, em...@sadafnoor.com
I also came across some problems with sorl-thumbnail. It cannot generate "cache" folder and thumbnail_kvstore is always empty. Please see more detail in this question .
Reply all
Reply to author
Forward
0 new messages