Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8926863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:05:15+00:00 2026-06-15T08:05:15+00:00

Possible Duplicate: resize image on save Am trying to create a thumbnail in django,

  • 0

Possible Duplicate:
resize image on save

Am trying to create a thumbnail in django, am trying to build a custom class specifically to be used for generating thumbnails. As following

from cStringIO import StringIO
from PIL import Image

class Thumbnail(object):

    SIZE = (50, 50)

    def __init__(self, source):
        self.source = source
        self.output = None

    def generate(self, size=None, fit=True):
        if not size:
            size = self.SIZE

        if not isinstance(size, tuple):
            raise TypeError('Thumbnail class: The size parameter must be an instance of a tuple.')

        # resize properties
        box = size
        factor = 1
        image = Image.open(self.source)
        # Convert to RGB if necessary
        if image.mode not in ('L', 'RGB'): 
            image = image.convert('RGB')
        while image.size[0]/factor > 2*box[0] and image.size[1]*2/factor > 2*box[1]:
            factor *=2
        if factor > 1:
            image.thumbnail((image.size[0]/factor, image.size[1]/factor), Image.NEAREST)

        #calculate the cropping box and get the cropped part
        if fit:
            x1 = y1 = 0
            x2, y2 = image.size
            wRatio = 1.0 * x2/box[0]
            hRatio = 1.0 * y2/box[1]
            if hRatio > wRatio:
                y1 = int(y2/2-box[1]*wRatio/2)
                y2 = int(y2/2+box[1]*wRatio/2)
            else:
                x1 = int(x2/2-box[0]*hRatio/2)
                x2 = int(x2/2+box[0]*hRatio/2)
            image = image.crop((x1,y1,x2,y2))

        #Resize the image with best quality algorithm ANTI-ALIAS
        image.thumbnail(box, Image.ANTIALIAS)

        # save image to memory
        temp_handle = StringIO()
        image.save(temp_handle, 'png')
        temp_handle.seek(0)

        self.output = temp_handle

        return self

    def get_output(self):
        self.output.seek(0)
        return self.output.read()

the purpose of the class is so i can use it inside different locations to generate thumbnails on the fly. The class works perfectly, I’ve tested it directly under a view.. I’ve implemented the thumbnail class inside the save method of the forms to resize the original images on saving.

in my design, I have two fields for thumbnails. I was able to generate one thumbnail, if I try to generate two it crashes and I’ve been stuck for hours not sure whats the problem.

Here is my model

class Image(models.Model):
    article         = models.ForeignKey(Article)
    title           = models.CharField(max_length=100, null=True, blank=True)
    src             = models.ImageField(upload_to='publication/image/')
    r128            = models.ImageField(upload_to='publication/image/128/', blank=True, null=True)
    r200            = models.ImageField(upload_to='publication/image/200/', blank=True, null=True)

    uploaded_at     = models.DateTimeField(auto_now=True)

Here is my forms

class ImageForm(models.ModelForm):
    """

    """
    class Meta:
        model = Image
        fields = ('src',)


    def save(self, commit=True):
        instance = super(ImageForm, self).save(commit=True)


        instance.r128 = SimpleUploadedFile(
                    instance.src.name,
                    Thumbnail(instance.src).generate((128, 128)).get_output(),
                    content_type='image/png'
                )


        instance.r200 = SimpleUploadedFile(
            instance.src.name,
            Thumbnail(instance.src).generate((200, 200)).get_output(),
            content_type='image/png'
        )

        if commit:
            instance.save()
        return instance

the strange part is, when i remove the line which contains instance.r200 in the form save. It works fine, and it does the thumbnail and stores it successfully. Once I add the second thumbnail it fails..

Any ideas what am doing wrong here?

Thanks

Update:

as per the comment request, am appending the error trace

IOError at /en/publication/new/

cannot identify image file

Request Method:     POST
Request URL:    http://127.0.0.1:8000/en/publication/new/?image-extra=
Django Version:     1.4.2
Exception Type:     IOError
Exception Value:    

cannot identify image file

Exception Location:     /Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/PIL/Image.py in open, line 1980
Python Executable:  /Users/mo/Projects/pythonic/snowflake-env/bin/python
Python Version:     2.7.2

Update

Tried to create print statement and below is the output

Source: publication/image/tumblr_m9o7244nZM1rykg1io1_1280_11.jpg
Source: publication/image/tumblr_m9o7244nZM1rykg1io1_1280_11.jpg
ERROR:root:cannot identify image file
ERROR:django.request:Internal Server Error: /en/publication/new/
Traceback (most recent call last):
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 20, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/db/transaction.py", line 209, in inner
    return func(*args, **kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/publication/views.py", line 69, in new
    formset.save()
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 497, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 628, in save_new_objects
    self.new_objects.append(self.save_new(form, commit=commit))
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 727, in save_new
    obj = form.save(commit=False)
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/publication/forms.py", line 113, in save
    Thumbnail(instance.src).generate((200, 200)).get_output(),
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/core/utils.py", line 23, in generate
    image = Image.open(self.source)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/PIL/Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file

As seen, the first image is printed and processed successfully the second image is failing.

update

traceback error update after applying the copy() in the thumbnail class

ERROR:root:cannot identify image file
ERROR:django.request:Internal Server Error: /en/publication/new/
Traceback (most recent call last):
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 20, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/db/transaction.py", line 209, in inner
    return func(*args, **kwargs)
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/publication/views.py", line 69, in new
    formset.save()
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 497, in save
    return self.save_existing_objects(commit) + self.save_new_objects(commit)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 628, in save_new_objects
    self.new_objects.append(self.save_new(form, commit=commit))
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/django/forms/models.py", line 727, in save_new
    obj = form.save(commit=False)
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/publication/forms.py", line 113, in save
    f128.write(Thumbnail(instance.src).generate((128, 128)).get_output())
  File "/Users/mo/Projects/pythonic/snowflake-env/snowflake/snowflake/apps/core/utils.py", line 15, in __init__
    self._pilImage = Image.open(self.source)
  File "/Users/mo/Projects/pythonic/snowflake-env/lib/python2.7/site-packages/PIL/Image.py", line 1980, in open
    raise IOError("cannot identify image file")
IOError: cannot identify image file

Update

Finally, I managed to get it to work, but I had to stream the file into self.source as belo

def __init__(self, source):
    self.source = StringIO(file(source.path, "rb").read())
    self.output = None

    self._pilImage = Image.open(self.source)

is the above ideal approach? is it a good idea to read the file at each hit? if no, what are my alternatives?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:05:16+00:00Added an answer on June 15, 2026 at 8:05 am

    The problem I see is in the way you have designed your Thumbnail class. It is using class attributes to store instance variables, meaning that you will have conflicts when you try to use the class more than once.

    There is no need for the static load method, as once you move the attributes to the instance, it does the exact same thing as the constructor of the class. And by requiring a source in the constructor, you ensure a crash will not occur later in generate when it looks for empty string values.

    Also, one of the major problems I think you are facing is when you are using the file-like object wrappers that your django model is returning for the ImageField‘s. While you would not see this if you were passing in string paths, when you pass in the file object, the generate method reads it to the end. Then you call generate a second time with the same source object, but it is at the end and you get an IOError. Now one approach would be to make sure to seek the source back to 0 before calling Thumbnail again with it, but instead you can save yourself the trouble and just have your Thumbnail class open and cache the PIL image once in the constructor. Then generate does not need to constantly re-read it again each time.

    # Example from your code #
    def generate(self, size=None, fit=True):
        ...
        # The first time you do this, it will read
        # self.source to the end, because in Django, you
        # are passing a file-like object.
        image = Image.open(self.source)
    
    # this will work the first time
    generate()
    # uh oh. self.source was a file object that is at the end
    generate() # crash
    

    Re-written Thumbnail Class

    from cStringIO import StringIO
    from PIL import Image
    
    class Thumbnail(object):
    
        SIZE = (50, 50)
    
        def __init__(self, source):
            self.source = source
            self.output = None
    
            self._pilImage = Image.open(self.source)
    
        def generate(self, size=None, fit=True):
            if not size:
                size = self.SIZE
    
            if not isinstance(size, tuple):
                raise TypeError('Thumbnail class: The size parameter must be an instance of a tuple.')
    
            # resize properties
            box = size
            factor = 1
            image = self._pilImage.copy()
    
            # Convert to RGB if necessary
            if image.mode not in ('L', 'RGB'): 
                image = image.convert('RGB')
            while image.size[0]/factor > 2*box[0] and image.size[1]*2/factor > 2*box[1]:
                factor *=2
            if factor > 1:
                image.thumbnail((image.size[0]/factor, image.size[1]/factor), Image.NEAREST)
    
            #calculate the cropping box and get the cropped part
            if fit:
                x1 = y1 = 0
                x2, y2 = image.size
                wRatio = 1.0 * x2/box[0]
                hRatio = 1.0 * y2/box[1]
                if hRatio > wRatio:
                    y1 = int(y2/2-box[1]*wRatio/2)
                    y2 = int(y2/2+box[1]*wRatio/2)
                else:
                    x1 = int(x2/2-box[0]*hRatio/2)
                    x2 = int(x2/2+box[0]*hRatio/2)
                image = image.crop((x1,y1,x2,y2))
    
            #Resize the image with best quality algorithm ANTI-ALIAS
            image.thumbnail(box, Image.ANTIALIAS)
    
            # save image to memory
            temp_handle = StringIO()
            image.save(temp_handle, 'png')
            temp_handle.seek(0)
    
            self.output = temp_handle
    
            return self
    
        def get_output(self):
            self.output.seek(0)
            return self.output.read()
    

    Usage: Thumbnail(src).generate((200, 200)).get_output()

    The source and output need to be unique for each instance. But in your version you would set output to the class level, which means that two instances of the Thumbnail use the shared most recent version of output.

    # your code #
        # this is assigning the most recently processed
        # object to the class level. shared among all.
        self.output = temp_handle
    
        return self
    
    def get_output(self):
        # always read the shared class level
        return self.output.read()
    

    Also, I feel there is an easier way to perform your resize/fit/crop. If you explain the exact transformation you want to do for the image, I can probably simplify that as well.

    Update

    I forgot to specifically mention that with my suggestions for saving the source image once, your usage should look like this:

    def save(self, commit=True):
        instance = super(ImageForm, self).save(commit=True)
    
        thumb = Thumbnail(instance.src)
    
        instance.r128 = SimpleUploadedFile(
            instance.src.name,
            thumb.generate((128, 128)).get_output(),
            content_type='image/png'
        )
    
        instance.r200 = SimpleUploadedFile(
            instance.src.name,
            thumb.generate((200, 200)).get_output(),
            content_type='image/png'
        )
    

    Notice that we only create one instance of Thumbnail using the source, which will open it only once in PIL. Then you can generate as many images as you want from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Can anybody suggest the best image resize script in php? I'm still
Possible Duplicate: Resize a picture to a fixed size How to resize an image
Possible Duplicate: PHP image resize on the fly vs storing resized images I was
Possible Duplicate: How to resize an image using PHP? Here's what I've tried: $image
Possible Duplicate: Android: How to resize a custom view programmatically? What's the best way
Possible Duplicate: UIAlertView not showing message text I am trying to create a simple
Possible Duplicate: Image size got bigger when trying to reduce its size What I
Possible Duplicate: Resize an Image C# How can I programmatically resize an image in
Possible Duplicate: Hiding textarea resize handle in Safari in textarea input the user can
Possible Duplicate: Getting ‘undefined index’ error while trying to use $_FILE in PHP I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.