Transforming 16:9 to 1:1 Enhance Visual Appeal with Blurred Backgrounds Using Python PIL

MoneyTech360
By -
0
Transforming 16:9 to 1:1 Enhance Visual Appeal with Blurred Backgrounds Using Python PIL


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):
This line defines a function named `process_image` that takes two arguments: the path to the input image and the path where the processed image should be saved.

2: Opening the Image

original = Image.open(input_image_path)
This line uses the PIL (Pillow) library's `Image.open()` method to open the image file located at `input_image_path`. The opened image is stored in the variable `original`.

3: Resizing to Ensure Maximum Dimensions

max_dimensions = (1280, 720)
original.thumbnail(max_dimensions, Image.ANTIALIAS)
These lines ensure that the image doesn’t exceed a maximum size of 1280x720 pixels. The `thumbnail()` method resizes the image in-place while maintaining aspect ratio. `Image.ANTIALIAS` is a high-quality downsampling filter.

4: Calculating Crop Coordinates

left = (original. Width - 720) / 2
top = (original. Height - 720) / 2
right = (original. Width + 720) / 2
bottom = (original. Height + 720) / 2
These lines calculate the coordinates for cropping the image. The calculation is done so that the cropped area will be centered on the original image and have dimensions of 720x720 pixels.

5: Cropping the Image

cropped_center = original. Crop((left, top, right, bottom))
This line crops the image according to the coordinates calculated in the previous step.

6: Resizing the Cropped Image

resized_cropped = cropped_center.resize((1280, 1280), Image.ANTIALIAS)
This line resizes the cropped image to have new dimensions of 1280x1280 pixels, using antialiasing for better quality.

7: Applying Gaussian Blur

blurred_background = resized_cropped.filter(ImageFilter.GaussianBlur(radius=10))
This line applies a Gaussian blur filter to the resized cropped image, using a radius of 10 for the blur. This creates a blurred background effect.

8: Creating a New Blank Image

final_image = Image.new('RGB', (1280, 1280), (0, 0, 0))
This line creates a new blank image with black background and dimensions of 1280x1280 pixels.

9: Calculating Paste Coordinates

paste_x = (final_image.width - original. Width) // 2
paste_y = (final_image.height - original. Height) // 2
These lines calculate the coordinates where the original image will be pasted onto the new image. The calculations ensure that the original image is centered on the new image.

10: Pasting Images onto the Final Image

final_image.paste(blurred_background, (0, 0))
final_image.paste(original, (paste_x, paste_y))
These lines first paste the blurred background onto the new image and then paste the original image on top of it, using the coordinates calculated in the previous step.

11: Saving the Processed Image

final_image.save(output_image_path)
This line saves the newly created image to the path specified by `output_image_path`.

12: Function Call

input_file = '\\\\MoneyTech360\\Blog\\Thumbnail\\Thumbnai5.jpg'
output_file = 'output_image.jpg'

process_image(input_file, output_file)
These lines specify the input and output file paths and call the `process_image` function to execute the image processing steps on the provided image.

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)


Your feedback and suggestion is very valuable for us.
So please comment your thoughts and request code below



Tags:

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Ok, Go it!) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Ok, Go it!