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 6771477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:26:13+00:00 2026-05-26T15:26:13+00:00

The following code takes an image after it gets saved and makes a thumbnail

  • 0

The following code takes an image after it gets saved and makes a thumbnail out of it:

class Image(models.Model):
    image     = models.ImageField(upload_to='images')
    thumbnail = models.ImageField(upload_to='images/thumbnails', editable=False)

    def save(self, *args, **kwargs):
       super(Image, self).save(*args, **kwargs)
       if self.image:
           from PIL import Image as ImageObj
           from cStringIO import StringIO
           from django.core.files.uploadedfile import SimpleUploadedFile

           try:
               # thumbnail
               THUMBNAIL_SIZE = (160, 160)  # dimensions

               image = ImageObj.open(self.image)

               # Convert to RGB if necessary
               if image.mode not in ('L', 'RGB'): image = image.convert('RGB')

               # create a thumbnail + use antialiasing for a smoother thumbnail
               image.thumbnail(THUMBNAIL_SIZE, ImageObj.ANTIALIAS)

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

               # save it
               file_name, file_ext = os.path.splitext(self.image.name.rpartition('/')[-1])
               suf = SimpleUploadedFile(file_name + file_ext, temp_handle.read(), content_type='image/png')

               self.thumbnail.save(file_name + '.png', suf, save=False)
           except ImportError:
               pass

It’s working fine, the original image + the thumbnail are both being uploaded, and image is being assigned the correct path.

The only problem is thumbnail is not being assigned the path of the newly created thumbnail – it’s empty in the database. I have read the documentation, and it looks like if I save the thumbnail with save=True it should fix my problem:

self.thumbnail.save(file_name + '.png', suf, save=True)

However, doing this is raising the following:

Django Version: 1.3.1
Exception Type: IOError
Exception Value:    
cannot identify image file

I can’t figure out what I’m doing wrong.

  • 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-05-26T15:26:13+00:00Added an answer on May 26, 2026 at 3:26 pm

    I have solved my problem simply by moving:

    super(Image, self).save(*args, **kwargs)
    

    to the end of def save(). I’m still not certain why it’s happening this way, but my only explanation is that save() itself is persisting fields values to the database and hence it needs to be executed at the very end.

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

Sidebar

Related Questions

I have the following code which takes an improperly saved Image from the database
I have the following code to take an image and generate the thumbnail. how
Well I am using the following code to take any old image into a
The following C# code takes 5 minutes to run: int i = 1; string
I have the following code print Starting stage 1<br> # Something that takes about
I'm reloading a web page that has the following code: <label for=showimage>Show Image</label> <input
the following code is a handler that takes in a Percent (percent of the
If my windows is in 32-bit color depth mode, then the following code gets
the following is the code which i am using to capture an image from
Loading the image one time works as done in the following steps (for code

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.