Assignment - Image Processing

 Today we will do some image processing: Learn to read and display the image

Q: Read an image ( any tour pic of  yours- single ) . Convert it into gray scale and black and white . Display all the three  images. ( use your own functions - do not use built in functions )

share the screenshot in group with your name and roll number

✅ Load and display an image in Jupyter

from PIL import Image from IPython.display import display img = Image.open("path/to/your/image.jpg") display(img)

That will render the image inline in the notebook (no pop-up window).


🔍 Common extras you’ll probably want

Resize

img_resized = img.resize((400, 300)) display(img_resized)

Convert to grayscale

gray = img.convert("L") display(gray)

Check image info

print(img.size) # (width, height) print(img.mode) # RGB, L, etc.


Function to convert the image into black and white
idea is to read each pixel value and find the average pixel value. If the value is less
than 128 we will make it black else white

def blackAndWhite(image):
    """Converts the argument image to black and white."""
    blackPixel = (0, 0, 0)
    whitePixel = (255, 255, 255)
    for y in range(image.getHeight()):
        for x in range(image.getWidth()):
            (r, g, b) = image.getPixel(x, y)
            average = (r + g + b) // 3
            if average < 128:
                image.setPixel(x, y, blackPixel)
            else:
                image.setPixel(x, y, whitePixel)


Function to convert image into gray scale

def grayscale(image):
    """Converts the argument image to grayscale."""
    for y in range(image.getHeight()):
        for x in range(image.getWidth()):
            (r, g, b) = image.getPixel(x, y)
            r = int(r * 0.299)
            g = int(g * 0.587)
            b = int(b * 0.114)
            lum = r + g + b
            image.setPixel(x, y, (lum, lum, lum))

Comments

Popular posts from this blog

Programming in Python CST 362 KTU CS Sixth Semester Elective Notes

Graphical User Interfaces ( GUI)

Turtle Graphics