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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:49:54+00:00 2026-05-22T19:49:54+00:00

I am trying to create and save a thumbnail image when saving the original

  • 0

I am trying to create and save a thumbnail image when saving the original user image in the userProfile model in my project, below is my code:

def save(self, *args, **kwargs):
    super(UserProfile, self).save(*args, **kwargs)
    THUMB_SIZE = 45, 45
    image = Image.open(join(MEDIA_ROOT, self.headshot.name))

    fn, ext = os.path.splitext(self.headshot.name)
    image.thumbnail(THUMB_SIZE, Image.ANTIALIAS)        
    thumb_fn = fn + '-thumb' + ext
    tf = NamedTemporaryFile()
    image.save(tf.name, 'JPEG')
    self.headshot_thumb.save(thumb_fn, File(open(tf.name)), save=False)
    tf.close()

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

Every thing is working OK, just this one thing.

The problem is that the thumbnail function only sets the width to 45 and doesn’t change the ratio aspect of the image so I am getting an image of 45*35 for the one that I am testing on (short image).

Can any one tell me what am I doing wrong? How to force the aspect ratio I want?

P.S.: I’ve tried all the methods for size: tupal: THUMB_SIZE = (45, 45) and entering the sizes directly to the thumbnail function also.

Another question: what is the deference between resize and thumbnail functions in PIL? When to use resize and when to use thumbnail?

  • 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-22T19:49:55+00:00Added an answer on May 22, 2026 at 7:49 pm

    The image.thumbnail() function will maintain the aspect ratio of the original image.

    Use image.resize() instead.

    UPDATE

    image = image.resize(THUMB_SIZE, Image.ANTIALIAS)        
    thumb_fn = fn + '-thumb' + ext
    tf = NamedTemporaryFile()
    image.save(tf.name, 'JPEG')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm simply trying to create a secure user file that will save basic login
I am trying to create a separate model class for image uploading, which was
I am trying to create a graph and save it as an image. I
Trying to create a user account in a test. But getting a Object reference
Trying to create a an image acquiring application optimized for a fast scanner (which
This code returns a thumbnail of an image loaded from a byte array. I'm
I am trying to create a zip file and save it using DotNetZip library.
I´m trying to create a VIEW in SQL Server 2005. The SQL code is
Hi I'm trying to create a basic data model / layer The idea is
I'm trying to create an admin user as part of my tests.py to check

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.