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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:35:35+00:00 2026-05-14T02:35:35+00:00

I need to resize and crop an image to a specific width and height.

  • 0

I need to resize and crop an image to a specific width and height. I was able to construct a method that will create a square thumbnail, but I’m unsure on how to apply this, when the desired thumbnail is not square.

def rescale(data, width, height):
"""Rescale the given image, optionally cropping it to make sure the result image has the specified width and height."""
from google.appengine.api import images

new_width = width
new_height = height

img = images.Image(data)

org_width, org_height = img.width, img.height

# We must determine if the image is portrait or landscape
# Landscape
if org_width > org_height:
    # With the Landscape image we want the crop to be centered. We must find the
    # height to width ratio of the image and Convert the denominater to a float
    # so that ratio will be a decemal point. The ratio is the percentage of the image
    # that will remain.
    ratio = org_height / float(org_width)
    # To find the percentage of the image that will be removed we subtract the ratio
    # from 1 By dividing this number by 2 we find the percentage that should be
    # removed from each side this is also our left_x coordinate
    left_x = (1- ratio) / 2
    # By subtract the left_x from 1 we find the right_x coordinate
    right_x = 1 - left_x
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG)
    img.crop(left_x, 0.0, right_x, 1.0)
    # resize(image_data, width=0, height=0, output_encoding=images.PNG)
    img.resize(height=height)
# Portrait
elif org_width < org_height:
    ratio = org_width / float(org_height)
    # crop(image_data, left_x, top_y, right_x, bottom_y), output_encoding=images.PNG)
    img.crop(0.0, 0.0, 1.0, ratio)
    # resize(image_data, width=0, height=0, output_encoding=images.PNG)
    img.resize(width=witdh)

thumbnail = img.execute_transforms()
return thumbnail

If there is a better way to do this please let me know. Any help would be greatly appreciated.

Here’s a diagram explaining the desired process.
crop_diagram

Thanks,

Kyle

  • 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-14T02:35:36+00:00Added an answer on May 14, 2026 at 2:35 am

    I had a similar problem (your screenshot was very useful). This is my solution:

    def rescale(img_data, width, height, halign='middle', valign='middle'):
      """Resize then optionally crop a given image.
    
      Attributes:
        img_data: The image data
        width: The desired width
        height: The desired height
        halign: Acts like photoshop's 'Canvas Size' function, horizontally
                aligning the crop to left, middle or right
        valign: Verticallly aligns the crop to top, middle or bottom
    
      """
      image = images.Image(img_data)
    
      desired_wh_ratio = float(width) / float(height)
      wh_ratio = float(image.width) / float(image.height)
    
      if desired_wh_ratio > wh_ratio:
        # resize to width, then crop to height
        image.resize(width=width)
        image.execute_transforms()
        trim_y = (float(image.height - height) / 2) / image.height
        if valign == 'top':
          image.crop(0.0, 0.0, 1.0, 1 - (2 * trim_y))
        elif valign == 'bottom':
          image.crop(0.0, (2 * trim_y), 1.0, 1.0)
        else:
          image.crop(0.0, trim_y, 1.0, 1 - trim_y)
      else:
        # resize to height, then crop to width
        image.resize(height=height)
        image.execute_transforms()
        trim_x = (float(image.width - width) / 2) / image.width
        if halign == 'left':
          image.crop(0.0, 0.0, 1 - (2 * trim_x), 1.0)
        elif halign == 'right':
          image.crop((2 * trim_x), 0.0, 1.0, 1.0)
        else:
          image.crop(trim_x, 0.0, 1 - trim_x, 1.0)
    
      return image.execute_transforms()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to first resize an image proportionally (the width is the important dimension)
I'm trying to create thumbnail and resize image at the same time, so to
Anybody help, I need Image Manager with multi-upload, crop, resize.... And final cost less
I need to perform java image crop and resize without an X server. I
I want to resize photos, crop photos, etc. That's all. But I need to
I have a picture that I need to resize to a specific dimension and
I need to resize/crop images in different resolutions for Android App. I know, that
I need to do two things to an image: resize it, then crop it.
I need to resize and crop to exactly 60x80px from various size and aspect
I need resize this image proportionally heigth 411px. how do this? [HttpPost] public WrappedJsonResult

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.