[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Squarify images in Python

[An image cropped to be square]

Sometimes I want to use an image but it's the wrong shape. It may be too short and wide or too tall and thin when I really want it square. (It's hip to be square!) This example crops images so they are square.

Squarification

The squarification code is actually quite short.

def squarify_image(image): '''Trim the image so it's square''' # See where we need to crop. wid = hgt = min(image.width, image.height) x = (image.width - wid) / 2 y = (image.height - hgt) / 2 box = (x, y, x + wid - 1, y + hgt - 1) # Crop and return the result. return image.crop(box)

This function sets wid and hgt to the minimum of the image's width and height. Those will be the dimensions of the squarified image.

Next, the code figures out where the squarified part of the image is so it is centered on the original image. For example, if the image's width is image.width and the squarified width is wid, then the amount of extra horizontal space is image.width - wid. To center the square image on the original, the code sets x equal to half that.

Having found the X and Y coordinates that center the square, the code makes a box that defines the square's left, top, right, and bottom coordinates. It then simply crops the original image to those coordinates and returns the result.

Showing the Result

The squarify_image function is the heart of the program, but I want to mention one more interesting piece. When you load a new image, the following code is in charge of displaying it.

def show_image(self): '''Display the image squarified and scaled to fit the window.''' if self.raw_image is None: return # Squarify the image. self.squarified_image = squarify_image(self.raw_image) # Resize to fit. self.canvas.update() wid = self.canvas.winfo_width() hgt = self.canvas.winfo_height() sized_image, x, y = fit_image(self.squarified_image, 0, 0, wid, hgt) # Display the image. self.photo_image = ImageTk.PhotoImage(sized_image) self.canvas.delete(tk.ALL) self.canvas.create_image(0, 0, anchor=tk.NW, image=self.photo_image)

This method checks the raw_image value and, if no image is currently loaded, it returns.

If an image is loaded, the method calls squarify_image.

Next, it gets the size of the program's canvas and calls fit_image to resize the squarified image to fit the canvas. For information on fit_image, see my post Fit an image to a target rectangle and center it in Python.

The method then displays the resized image. It converts it into a ImageTk.PhotoImage and saves it so the program has a non-volatile copy of the image that it can use to display it later. It deletes everything from the program's canvas and displays the image there.

Conclusion

This program crops images in a very specific way to solve a very specific problem. In a later post, I'll show how you can crop images to specific aspect ratios more generally. Until then, download this example to experiment with it and to see additional details.
© 2025 - 2026 Rocky Mountain Computer Consulting, Inc. All rights reserved.