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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:24:53+00:00 2026-05-29T05:24:53+00:00

Currently I am using: os.chdir(album.path) images = glob.glob(‘*.*’) # thumbs size size = 80,80

  • 0

Currently I am using:

    os.chdir(album.path)
    images = glob.glob('*.*')

    # thumbs size
    size = 80,80

    for image in images:
        #create thumb
        file, ext = os.path.splitext(image)
        im = Image.open(os.path.join(album.path,image))
        im.thumbnail(size, Image.ANTIALIAS)
        thumb_path = os.path.join(album.path, 'thumbs', file + ".thumb" + ".jpeg")
        im.save(thumb_path)

Although this works, I end up with different sizes images (some are portrait and some are landscape), but I want all of the images to have an exact size. Maybe a sensible cropping?

UPDATE:

I don’t mind cropping a small portion of the image. When I said sensible cropping I mean something like this algorythm:

if image is portrait:
    make width 80px
    crop the height (will be more than 80px)
else if image is landscape:
    make height 80px
    crop the width to 80px (will be more than 80px)
  • 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-29T05:24:54+00:00Added an answer on May 29, 2026 at 5:24 am

    Here is my take on doing a padded fit for an image:

    #!/usr/bin/env python
    
    from PIL import Image, ImageChops
    
    F_IN = "/path/to/image_in.jpg"
    F_OUT = "/path/to/image_out.jpg"
    
    size = (80,80)
    
    image = Image.open(F_IN)
    image.thumbnail(size, Image.ANTIALIAS)
    image_size = image.size
    
    thumb = image.crop( (0, 0, size[0], size[1]) )
    
    offset_x = max( (size[0] - image_size[0]) / 2, 0 )
    offset_y = max( (size[1] - image_size[1]) / 2, 0 )
    
    thumb = ImageChops.offset(thumb, offset_x, offset_y)
    thumb.save(F_OUT)
    

    It first uses the thumbnail operation to bring the image down to within your original bounds and preserving the aspect. Then it crops it back out to actually fill the size of your bounds (since unless the original image was square, it will be smaller now), and we find the proper offset to center the image. The image is offset to the center, so you end up with black padding but no image cropping.

    Unless you can make a really sensible guess at a proper center crop without losing possible important image data on the edges, a padded fit approach will work better.

    Update

    Here is a version that can do either center crop or pad fit.

    #!/usr/bin/env python
    
    from PIL import Image, ImageChops, ImageOps
    
    def makeThumb(f_in, f_out, size=(80,80), pad=False):
    
        image = Image.open(f_in)
        image.thumbnail(size, Image.ANTIALIAS)
        image_size = image.size
    
        if pad:
            thumb = image.crop( (0, 0, size[0], size[1]) )
    
            offset_x = max( (size[0] - image_size[0]) / 2, 0 )
            offset_y = max( (size[1] - image_size[1]) / 2, 0 )
    
            thumb = ImageChops.offset(thumb, offset_x, offset_y)
    
        else:
            thumb = ImageOps.fit(image, size, Image.ANTIALIAS, (0.5, 0.5))
    
        thumb.save(f_out)
    
    
    source = "/path/to/source/image.JPG"
    
    makeThumb(source, "/path/to/source/image_padded.JPG", pad=True)
    makeThumb(source, "/path/to/source/image_centerCropped.JPG", pad=False)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently using PIL. from PIL import Image try: im = Image.open(filename) #
Currently using the HTTPServletRequest class and specifically the .getQueryString method to retrieve an inputted
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using MySQL version 5.1.6 This is my first real world build and so
Currently using Xcode 4.2 and I have two view controllers (1 and 2). I
I'm currently using the validation code listed here in an application. I'd like to
I am currently using a javascript code to make an entire row in my
Right now I currently using transactional replication with updatable subscription. Is there any ways
I am currently using the google maps' reverse geocoding API to convert long/lat received
I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that

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.