In a digital world where images tell stories, one Python script embarks
on a quest to transform the ordinary into extraordinary. With PIL as its
wand, it first delicately resizes a bustling photo to fit a snug digital
frame. But, our script dreams bigger — it craves focus. It meticulously
carves out the photo's heart, only to drape it in a dreamy blur, crafting
a surreal backdrop on a new canvas.
But wait, the original tale hasn't faded. Our script, the deft artist it
is, resurrects the initial image, placing it center stage against the
blurred setting. It's a study in contrast, a clear shout amidst a hazy
whisper.
In its final act, the script seals this visual saga. What was once just
an image is now a story, a masterpiece born from a blend of math, art, and
code. In this realm, Python isn't just a programming language; it's a
paintbrush turning pixels into narratives, one transformation at a
time.
1: Defining the Function
def process_image(input_image_path, output_image_path):
2: Opening the Image
original = Image.open(input_image_path)
3: Resizing to Ensure Maximum Dimensions
max_dimensions = (1280, 720)
original.thumbnail(max_dimensions, Image.ANTIALIAS)
4: Calculating Crop Coordinates
left = (original. Width - 720) / 2
top = (original. Height - 720) / 2
right = (original. Width + 720) / 2
bottom = (original. Height + 720) / 2
5: Cropping the Image
cropped_center = original. Crop((left, top, right, bottom))
6: Resizing the Cropped Image
resized_cropped = cropped_center.resize((1280, 1280), Image.ANTIALIAS)
7: Applying Gaussian Blur
blurred_background = resized_cropped.filter(ImageFilter.GaussianBlur(radius=10))
8: Creating a New Blank Image
final_image = Image.new('RGB', (1280, 1280), (0, 0, 0))
9: Calculating Paste Coordinates
paste_x = (final_image.width - original. Width) // 2
paste_y = (final_image.height - original. Height) // 2
10: Pasting Images onto the Final Image
final_image.paste(blurred_background, (0, 0))
final_image.paste(original, (paste_x, paste_y))
11: Saving the Processed Image
final_image.save(output_image_path)
12: Function Call
input_file = '\\\\MoneyTech360\\Blog\\Thumbnail\\Thumbnai5.jpg'
output_file = 'output_image.jpg'
process_image(input_file, output_file)
This code performs several image manipulation tasks like resizing, cropping, blurring, and merging, to achieve a specific visual effect on the input image.
from PIL import Image, ImageFilter
def process_image(input_image_path, output_image_path):
# Open the original image
original = Image.open(input_image_path)
# Ensure the original image is within 1280x720
max_dimensions = (1280, 720)
original.thumbnail(max_dimensions, Image.ANTIALIAS)
# Calculate dimensions to crop the center of the image
left = (original.width - 720) / 2
top = (original.height - 720) / 2
right = (original.width + 720) / 2
bottom = (original.height + 720) / 2
# Crop the center of the image
cropped_center = original.crop((left, top, right, bottom))
# Resize the cropped image to 1280x1280
resized_cropped = cropped_center.resize((1280, 1280), Image.ANTIALIAS)
# Apply Gaussian blur to the background image
blurred_background = resized_cropped.filter(ImageFilter.GaussianBlur(radius=10)) # you can adjust the radius as needed
# Create a new blank image with the desired size (1280x1280)
final_image = Image.new('RGB', (1280, 1280), (0, 0, 0))
# Calculate coordinates to paste the original image in the center of the new image
paste_x = (final_image.width - original.width) // 2
paste_y = (final_image.height - original.height) // 2
# Paste the blurred background and then the original image
final_image.paste(blurred_background, (0, 0))
final_image.paste(original, (paste_x, paste_y))
# Save the result
final_image.save(output_image_path)
# Replace with your actual file paths
input_file = '\\\\MoneyTech360\\Blog\\Thumbnail\\Thumbnai5.jpg'
output_file = 'output_image.jpg'
process_image(input_file, output_file)