Saturday, 27 January 2024

Image Processing in Python using Pillow


Image Processing in Python
#original Image

from PIL import Image
Image.open('clcodingmr.jpg')





1. Image Resizing:

from PIL import Image

def resize_image(image_path, output_path, width, height):
    image = Image.open(image_path)
    resized_image = image.resize((width, height))
    resized_image.save(output_path)

# Example usage:
resize_image('clcodingmr.jpg', 'resized_output.jpg', 300, 200)

# Now, open and show the resized image
Image.open('clcodingmr.jpg')
Image.open('resized_output.jpg')




2. Image Rotation with Pillow:

from PIL import Image
def rotate_image(image_path, output_path, angle):
    image = Image.open(image_path)
    rotated_image = image.rotate(angle)
    rotated_image.save(output_path)
# Example usage:
rotate_image('clcodingmr.jpg', 'rotated_output.jpg', 45)
Image.open('rotated_output.jpg')




3. Image Translation (using crop) with Pillow:

from PIL import Image
def translate_image(image_path, output_path, tx, ty):
    image = Image.open(image_path)
    translated_image = image.crop((tx, ty, image.width, image.height))
    translated_image.save(output_path)
# Example usage:
translate_image('clcodingmr.jpg', 'translated_output.jpg', 50, 30)
Image.open('translated_output.jpg')




4. Image Shearing (using affine transform) with Pillow:
Image.open('sheared_output.jpg')
from PIL import Image, ImageOps
def shear_image(image_path, output_path, shear_factor):
    image = Image.open(image_path)
    shear_matrix = [1, shear_factor, 0, 0, 1, 0]
    sheared_image = image.transform(image.size, Image.AFFINE, shear_matrix)
    sheared_image.save(output_path)
# Example usage:
shear_image('clcodingmr.jpg', 'sheared_output.jpg', 0.2)
Image.open('sheared_output.jpg')




5. Image Normalization (simple contrast adjustment) with Pillow:

from PIL import Image
def normalize_image(image_path, output_path):
    image = Image.open(image_path)
    normalized_image = ImageOps.autocontrast(image)
    normalized_image.save(output_path)
# Example usage:
normalize_image('clcodingmr.jpg', 'normalized_output.jpg')
Image.open('normalized_output.jpg')




6. Image Blurring (using a filter) with Pillow:

from PIL import Image, ImageFilter
def blur_image(image_path, output_path, radius):
    image = Image.open(image_path)
    blurred_image = image.filter(ImageFilter.GaussianBlur(radius))
    blurred_image.save(output_path)
# Example usage:
blur_image('clcodingmr.jpg', 'blurred_output.jpg', 5)
Image.open('blurred_output.jpg')




0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (51) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (22) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses