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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:59:30+00:00 2026-05-19T13:59:30+00:00

import Image image = Image.open(‘images/original.jpg’) width = image.size[0] height = image.size[1] if width >

  • 0
import Image

image  = Image.open('images/original.jpg')
width  = image.size[0]
height = image.size[1]

if width > height:
    difference = width - height
    offset     = difference / 2
    resize     = (offset, 0, width - offset, height)

else:
    difference = height - width
    offset     = difference / 2
    resize     = (0, offset, width, height - offset)

thumb = image.crop(resize).resize((200, 200), Image.ANTIALIAS)
thumb.save('thumb.jpg')

This is my current thumbnail generation script. The way it works is:

If you have an image that is 400×300 and you want a thumbnail that’s 100×100, it will take 50 pixels off the left and right side of the original image. Thus, resizing it to be 300×300. This gives the original image the same aspect ratio as the new thumbnail. After that, it will shrink it down to the required thumbnail size.

The advantages of this is:

  • The thumbnail is taken from the center of the image
  • Aspect ratio doesn’t get screwed up

If you were to shrink the 400×300 image down to 100×100, it will look squished. If you took the thumbnail from 0x0 coordinates, you would get the top left of the image. Usually, the focal point of the image is the center.

What I want to be able to do is give the script a width/height of any aspect ratio. For example, if I wanted the 400×300 image to be resized to 400×100, it should shave 150px off the left and right sides of the image…

I can’t think of a way to do this. Any ideas?

  • 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-19T13:59:31+00:00Added an answer on May 19, 2026 at 1:59 pm

    You just need to compare the aspect ratios – depending on which is larger, that will tell you whether to chop off the sides or the top and bottom. e.g. how about:

    import Image
    
    image  = Image.open('images/original.jpg')
    width  = image.size[0]
    height = image.size[1]
    
    aspect = width / float(height)
    
    ideal_width = 200
    ideal_height = 200
    
    ideal_aspect = ideal_width / float(ideal_height)
    
    if aspect > ideal_aspect:
        # Then crop the left and right edges:
        new_width = int(ideal_aspect * height)
        offset = (width - new_width) / 2
        resize = (offset, 0, width - offset, height)
    else:
        # ... crop the top and bottom:
        new_height = int(width / ideal_aspect)
        offset = (height - new_height) / 2
        resize = (0, offset, width, height - offset)
    
    thumb = image.crop(resize).resize((ideal_width, ideal_height), Image.ANTIALIAS)
    thumb.save('thumb.jpg')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

from PIL import Image im = Image.open(f) #the size is 500x350 box = (0,0,100,100)
My code reads: import Image def generateThumbnail(self, width, height): Generates thumbnails for an image
from PIL import Image img = Image.open('1.png') img.save('2.png') The first image has a transparent
The trivial import Image im = Image.OPEN('C:\abc.bmp') results in the following exception Traceback (most
I am currently using PIL. from PIL import Image try: im = Image.open(filename) #
I have a web project where I must import text and images from a
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
Consider the following code: from PIL import Image, ImageDraw, ImageFont def addText(img, lTxt): FONT_SIZE
from Tkinter import * import socket, sys from PIL import Image, ImageTk root =
I have some strange problem with PIL not resizing the image. from PIL import

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.