This script automates image processing tasks using Python's Pillow library. It identifies an image's orientation—square, vertical, or horizontal—and resizes it to specified dimensions, centering the image on a canvas without distorting its aspect ratio. Additionally, it can split a larger image into smaller tiles for efficient loading and display. This is especially useful for web development, graphic design, and content creation, where visual consistency and performance are key. In essence, it's a handy tool for enhancing digital aesthetics and functionality across various applications.
Importing Necessary Libraries:
Python
from PIL import Image import os
These lines import the necessary modules. `PIL` (Python Imaging Library) is a part of the "Pillow" package and is used for opening, manipulating, and saving different image file formats. `os` is a standard Python library that provides a way of using operating system-dependent functionality, like reading or writing to the filesystem.
Determining Image Shape:
Python
def determine_image_shape(width, height):
if width == height:
return "Square"
elif height > width:
return "Vertical"
else:
return "Horizontal"Resizing Image to Fit Canvas:
Python
def resize_image(image, canvas_dims):
img_ratio = image.width / image.height
canvas_ratio = canvas_dims[0] / canvas_dims[1]
if img_ratio > canvas_ratio:
new_width = canvas_dims[0]
new_height = int(new_width / img_ratio)
else:
new_height = canvas_dims[1]
new_width = int(new_height * img_ratio)
return image.resize((new_width, new_height), Image.ANTIALIAS)Resizing and Centering the Original Image:
Python
def resize_and_center_image(image_path):
with Image.open(image_path) as img:
# ... [omitted for brevity]
# Create a white canvas
background = Image.new('RGBA', canvas_dims, (255, 255, 255, 255))
# Resize the image to fit the canvas
img = resize_image(img, canvas_dims)
# Calculate coordinates to center the image
offset = ((canvas_dims[0] - img.width) // 2, (canvas_dims[1] - img.height) // 2)
# Paste the image on the background
background.paste(img, offset, img.convert('RGBA'))
return backgroundSplitting the Image into Tiles:
Python
def split_image(image, directory, tile_width=1000, tile_height=1000):
# If the output directory doesn't exist, create it
if not os.path.exists(directory):
os.makedirs(directory)
img_width, img_height = image.size
counter = 1
for top in range(0, img_height, tile_height):
for left in range(0, img_width, tile_width):
bottom = min(top + tile_height, img_height)
right = min(left + tile_width, img_width)
# Crop the image to the desired tile
tile = image.crop((left, top, right, bottom))
tile.save(f"{directory}/tile_{counter}.png")
counter += 1Executing the Functions:
Python
final_image = resize_and_center_image(r"\\MoneyTech360\Blog\Content\trisha-hot-saree-pics.jpg") split_image(final_image, "output_tiles")
Please replace `'\\MoneyTech360\\Blog\\Content\\trisha-hot-saree-pics.jpg'` with the correct path to your image file, and `"output_tiles"` with your desired output directory. The script processes the image as per the described steps and saves the tiles in the specified directory.
Full Code:
Python
from PIL import Image
import os
def determine_image_shape(width, height):
if width == height:
return "Square"
elif height > width:
return "Vertical"
else:
return "Horizontal"
def resize_image(image, canvas_dims):
img_ratio = image.width / image.height
canvas_ratio = canvas_dims[0] / canvas_dims[1]
if img_ratio > canvas_ratio:
# If image width-to-height ratio is greater than canvas, fit to width
new_width = canvas_dims[0]
new_height = int(new_width / img_ratio)
else:
# If image width-to-height ratio is less than or equal to canvas, fit to height
new_height = canvas_dims[1]
new_width = int(new_height * img_ratio)
return image.resize((new_width, new_height), Image.ANTIALIAS)
def resize_and_center_image(image_path):
# Open an image file
with Image.open(image_path) as img:
width, height = img.size
shape = determine_image_shape(width, height)
# Set new canvas dimensions based on the shape
if shape == "Square":
canvas_dims = (3000, 3000)
elif shape == "Vertical":
canvas_dims = (3000, 5000)
else: # Horizontal
canvas_dims = (3000, 2000)
# Create a white canvas
background = Image.new('RGBA', canvas_dims, (255, 255, 255, 255))
# Resize the image to fit the canvas
img = resize_image(img, canvas_dims)
# Calculate coordinates to center the image
offset = ((canvas_dims[0] - img.width) // 2, (canvas_dims[1] - img.height) // 2)
# Paste the image on the background
background.paste(img, offset, img.convert('RGBA')) # ensure transparency if in the original image
return background
def split_image(image, directory, tile_width=1000, tile_height=1000):
# If the output directory doesn't exist, create it
if not os.path.exists(directory):
os.makedirs(directory)
img_width, img_height = image.size
counter = 1
for top in range(0, img_height, tile_height):
for left in range(0, img_width, tile_width):
bottom = min(top + tile_height, img_height)
right = min(left + tile_width, img_width)
# Crop the image to the desired tile
tile = image.crop((left, top, right, bottom))
tile.save(f"{directory}/name_{counter}.png")
counter += 1
# Replace 'your_image.png' with your image file's name
final_image = resize_and_center_image(r"\\MoneyTech360\Blog\Content\trisha-hot-saree-pics.jpg")
split_image(final_image, "output_tiles")
