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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:32:39+00:00 2026-05-18T07:32:39+00:00

When I resize a picture that is smaller than desired, I want the image

  • 0

When I resize a picture that is smaller than desired, I want the image to NOT get resized…but instead be in the center, with white padding around it.

So, let’s say I have an icon that’s 50×50. But I want to resize it to 100×100. If I pass it to this function, I would like it to return me the same icon that is centered, with white padding 25 pixels on each side.

Of course, I’d like this to work with images of any width/height, and not just a square like the example above.

My current code is below.

I don’t know why I did if height=None: height=width…I think it was just because it worked when I did it before.

def create_thumbnail(f, width=200, height=None):
    if height==None: height=width
    im = Image.open(StringIO(f))
    imagex = int(im.size[0])
    imagey = int(im.size[1])
    if imagex < width or imagey < height:
        return None
    if im.mode not in ('L', 'RGB', 'RGBA'):
        im = im.convert('RGB')
    im.thumbnail((width, height), Image.ANTIALIAS)
    thumbnail_file = StringIO()
    im.save(thumbnail_file, 'JPEG')
    thumbnail_file.seek(0)
    return thumbnail_file

This code may have many bugs in it, not sure.

  • 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-18T07:32:40+00:00Added an answer on May 18, 2026 at 7:32 am

    One option is to create a blank white image of the size you want, then draw the existing image in the center.

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

Sidebar

Related Questions

Actually I wish to do the Image Crop or Resize, etc with my picture
Iam trying to resize the facebook profile picture to large. I understand that in
So I want to take a square image and resize it to 300px *
I'm trying to resize an image that's being uploaded on my php form. The
I want to have a CSS div that has a background image (the full
I am trying to resize a picture that I have pasted into powerpoint from
I am currently trying to resize a picture that I am downloading from the
I have a picture that I need to resize to a specific dimension and
I'm trying to resize an image that is uploaded to Drupal via a form.
I am trying to resize an image that I have uploaded with a form.

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.