Resize rectangular image to square, keeping ratio and fill background with black in python

Resize rectangular image to square, keeping ratio and fill background with black in python

To resize a rectangular image to a square while keeping its aspect ratio and filling the background with black, you can use the Python Imaging Library (PIL) or the Pillow library, which is a modern fork of PIL. Here's how you can do it using Pillow:

from PIL import Image, ImageOps def resize_to_square(image_path, output_size): # Open the image original_image = Image.open(image_path) # Create a new square image with black background new_size = (max(original_image.size), max(original_image.size)) squared_image = Image.new("RGB", new_size, (0, 0, 0)) # Paste the original image centered in the new square image offset = ((new_size[0] - original_image.size[0]) // 2, (new_size[1] - original_image.size[1]) // 2) squared_image.paste(original_image, offset) # Resize the squared image to the desired output size resized_image = squared_image.resize(output_size) return resized_image # Path to the input image input_image_path = "input_image.jpg" # Output size (width, height) of the square image output_size = (256, 256) # Resize the image and save resized_image = resize_to_square(input_image_path, output_size) resized_image.save("output_image.jpg") 

In this code, the resize_to_square function resizes the input image to a square by pasting it onto a new square canvas with a black background and then resizing the result to the desired output size.

Remember to replace "input_image.jpg" with the path to your input image and adjust the output_size accordingly. You will also need to have the Pillow library installed (pip install Pillow).

Examples

  1. Resize rectangular image to square while maintaining aspect ratio

    • To resize a rectangular image to a square while preserving the aspect ratio, resize the image to fit the square and then add black padding.
    pip install pillow 
    from PIL import Image, ImageOps # Load a rectangular image image = Image.open("rectangular_image.jpg") # Desired size for the square image square_size = 300 # Calculate scaling factor aspect_ratio = image.width / image.height if aspect_ratio > 1: # Wider than tall new_width = square_size new_height = int(square_size / aspect_ratio) else: # Taller than wide new_width = int(square_size * aspect_ratio) new_height = square_size # Resize the image resized_image = image.resize((new_width, new_height), Image.LANCZOS) # Create a new square image with black background square_image = Image.new("RGB", (square_size, square_size), (0, 0, 0)) # Paste the resized image in the center of the square offset = ((square_size - new_width) // 2, (square_size - new_height) // 2) square_image.paste(resized_image, offset) square_image.save("square_image.jpg") 
  2. Resize rectangular image to square and fill with black background

    • Reshape an image to fit into a square with a black background while maintaining its aspect ratio.
    from PIL import Image, ImageOps image = Image.open("rectangular_image.jpg") # Set the square size square_size = 300 # Resize image while keeping aspect ratio resized_image = ImageOps.fit(image, (square_size, square_size), method=Image.LANCZOS, centering=(0.5, 0.5)) # Create black square and paste the resized image in the center square_image = Image.new("RGB", (square_size, square_size), "black") square_image.paste(resized_image, ((square_size - resized_image.width) // 2, (square_size - resized_image.height) // 2)) square_image.save("output_square_image.jpg") 
  3. Resize rectangular image and pad with black to create a square

    • Add black padding to an image to create a square with a preserved aspect ratio.
    from PIL import Image, ImageOps image = Image.open("rectangular_image.jpg") # Determine the larger dimension max_dimension = max(image.width, image.height) # Create a new square image with black background square_image = Image.new("RGB", (max_dimension, max_dimension), "black") # Calculate offset for centering x_offset = (max_dimension - image.width) // 2 y_offset = (max_dimension - image.height) // 2 # Paste the image into the square square_image.paste(image, (x_offset, y_offset)) square_image.save("square_with_padding.jpg") 
  4. Add black padding to make rectangular image square while maintaining ratio

    • Create a square image by adding black padding to the shorter side.
    from PIL import Image, ImageOps image = Image.open("rectangular_image.jpg") # Desired square size square_size = 300 # Resize while maintaining aspect ratio resized_image = image.copy() resized_image.thumbnail((square_size, square_size), Image.ANTIALIAS) # Create a square canvas and paste the resized image square_image = Image.new("RGB", (square_size, square_size), "black") offset = ((square_size - resized_image.width) // 2, (square_size - resized_image.height) // 2) square_image.paste(resized_image, offset) square_image.save("padded_square_image.jpg") 
  5. Transform rectangular image into a square with black background

    • To transform a rectangular image into a square with black filling, adjust its aspect ratio while retaining content.
    from PIL import Image, ImageOps image = Image.open("rectangular_image.jpg") # Define the target square size square_size = 400 # Create a square canvas with a black background square_image = Image.new("RGB", (square_size, square_size), "black") # Resize image to fit within the square image.thumbnail((square_size, square_size), Image.ANTIALIAS) # Center the image on the square canvas x_offset = (square_size - image.width) // 2 y_offset = (square_size - image.height) // 2 square_image.paste(image, (x_offset, y_offset)) square_image.save("transformed_square_image.jpg") 
  6. Make rectangular image square with padding to preserve aspect ratio

    • Resize an image to fit a square with black padding to maintain its aspect ratio.
    from PIL import Image, ImageOps # Load the image image = Image.open("rectangular_image.jpg") # Set square size square_size = 300 # Resize while preserving aspect ratio resized_image = image.copy() resized_image.thumbnail((square_size, square_size), Image.ANTIALIAS) # Create a square background with black color square_canvas = Image.new("RGB", (square_size, square_size), "black") # Calculate the offset for centering the image x_offset = (square_size - resized_image.width) // 2 y_offset = (square_size - resized_image.height) // 2 # Paste resized image onto the square background square_canvas.paste(resized_image, (x_offset, y_offset)) square_canvas.save("square_image_with_padding.jpg") 
  7. Convert rectangular image to square with black background in Python

    • Convert an image into a square by resizing and adding black padding to maintain aspect ratio.
    from PIL import Image # Open the rectangular image image = Image.open("rectangular_image.jpg") # Set square dimensions square_dim = max(image.width, image.height) # Create a square background with black color square_image = Image.new("RGB", (square_dim, square_dim), "black") # Calculate offset to center the image x_offset = (square_dim - image.width) // 2 y_offset = (square_dim - image.height) // 2 # Paste the image onto the square background square_image.paste(image, (x_offset, y_offset)) # Save the result square_image.save("square_image.jpg") 
  8. Resize rectangular image to square with centered padding

    • Create a square from a rectangular image with centered black padding to keep the aspect ratio.
    from PIL import Image, ImageOps # Load a rectangular image image = Image.open("rectangular_image.jpg") # Determine square dimensions square_dim = max(image.width, image.height) # Create a new square with a black background square_image = Image.new("RGB", (square_dim, square_dim), "black") # Determine the centering offsets x_offset = (square_dim - image.width) // 2 y_offset = (square_dim - image.height) // 2 # Place the rectangular image into the square background square_image.paste(image, (x_offset, y_offset)) # Save the output square_image.save("output_square_image.jpg") 
  9. Transform rectangular image into a square with black borders

    • Reshape a rectangular image into a square by adding black borders to the shorter dimension.
    from PIL import Image # Load the image image = Image.open("rectangular_image.jpg") # Define the square size square_dim = 300 # Determine if image is portrait or landscape is_portrait = image.height > image.width # Resize the image to fit within the square resized_image = image.copy() if is_portrait: resized_image.thumbnail((square_dim, square_dim), Image.LANCZOS) else: resized_image.thumbnail((square_dim, square_dim), Image.LANCZOS) # Create a square background square_background = Image.new("RGB", (square_dim, square_dim), "black") # Calculate the centering offset offset = ( (square_dim - resized_image.width) // 2, (square_dim - resized_image.height) // 2, ) # Place resized image on the square background square_background.paste(resized_image, offset) # Save the result square_background.save("square_with_black_borders.jpg") 
  10. Resize rectangular image to square with proportional black padding


More Tags

sortedlist rack webclient local-variables jsonpath feature-extraction mat-dialog binary-search android-viewholder re2

More Python Questions

More Geometry Calculators

More Mixtures and solutions Calculators

More Trees & Forestry Calculators

More Biology Calculators