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

  • Home
  • SEARCH
  • 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 3321378
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:03:20+00:00 2026-05-17T23:03:20+00:00

Is there an easy way to do this? In Python, I created a script

  • 0

Is there an easy way to do this?
In Python, I created a script to get a “square box” from an image…based on the center.

However, that killed some of my brain cells. Is there an easy way to do this for a 3×2 (3 width, 2 height), based on the center?

This is my “square box” script, but I don’t feel like modifying it for the 3×2.

def letterbox(f,thumb_w=None,thumb_h=None):
    try:
        im = Image.open(StringIO(f))
        imagex = int(im.size[0])
        imagey = int(im.size[1])
        thumb_size = (thumb_w,thumb_h) #what if it is too small!?? fix it.
        if imagex > imagey:
            setlen = imagey
            left = (imagex - setlen)/2
            top = 0
            height = setlen
            width = setlen
        if imagey > imagex:
            setlen = imagex
            left = 0
            top = (imagey - setlen)/2
            heigth = setlen
            width = setlen
        if imagex == imagey:
            left = 0
            top = 0
            height = imagey
            width = imagex
        box = (left,top,left+width,top+height)
        im = im.crop(box)
        #im.thumbnail(thumb_size,Image.ANTIALIAS)
        new_file = StringIO()
        im.save(new_file,'JPEG')
        new_file.seek(0)
    except Exception, e:
        pass
    return new_file

Is there a script online that can do what I need?

  • 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-17T23:03:20+00:00Added an answer on May 17, 2026 at 11:03 pm

    Use an aspect ratio which is defined as imagex/imagey, so you use 3/2 for 3:2, 16/9 for 16:9 etc.

    def letterbox(f,aspect_ratio=1):
        try:
            im = Image.open(StringIO(f))
            imagex = int(im.size[0])
            imagey = int(im.size[1])
            width = min(imagex, imagey*aspect_ratio)
            height = min(imagex/aspect_ratio, imagey)
            left =(imagex - width)/2
            top = (imagey - height)/2
            box = (left,top,left+width,top+height)
            im = im.crop(box)
            new_file = StringIO()
            im.save(new_file,'JPEG')
            new_file.seek(0)
        except Exception, e:
            pass
        return new_file
    

    You might want to check for roundoff errors at some point, but otherwise this does it.

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

Sidebar

Related Questions

Is there a easy way to do this? Or do I have to parse
In there an easy way to do this in PHP. I want to make
Is there an easy way to modify this code so that the target URL
Is there an easy way to iterate over an associative array of this structure
Is there an easy way to convert a string that contains this: Date: Wed,
There must be an easy way to do this, but somehow I can wrap
There must be an easy way to do this. I build a Flex app
There gotta be an easy way to do this, I can't believe there's none.
Is there an easy way to produce MSDN-style documentation from the Visual Studio XML
Is there an easy way to disable Python egg caching? We have the situation

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.