Basic Usage of the Python Pillow Library

The Python Pillow library is an image processing library that extends and updates the original PIL (Python Imaging Library).

Open, View, and Save

from PIL import Image
with Image.open("./res/LenaRGB.bmp") as img:    # size: width and height, e.g., (1920, 1080)    # format: color mode, e.g., RGB, L (grayscale)    # mode: image format, e.g., png, bmp    print(img.size, img.format, img.mode)    img.save('LenaSave.bmp')    img.show()

Resize

from PIL import Image
with Image.open("./res/LenaRGB.bmp") as img:    img_resized = img.resize((256, 256))    img_resized.show()

Crop

from PIL import Image
with Image.open("./res/LenaRGB.bmp") as img:    box = (0, 0, 512, 256)  # cropping area    img_cropped = img.crop(box)    img_cropped.show()

Rotate

from PIL import Image
# Method 1
with Image.open("./res/LenaRGB.bmp") as img:    img_rotated = img.rotate(90)  # rotate 90 degrees counterclockwise    img_rotated.show()
# Method 2
with Image.open("./res/LenaRGB.bmp") as img:    img_rotated = img.transpose(Image.Transpose.ROTATE_90)  # rotate 90 degrees counterclockwise    img_rotated.show()

Convert Color Mode

from PIL import Image
with Image.open("./res/LenaRGB.bmp") as img:    img_gray = img.convert("L")  # convert to grayscale    img_gray.show()

Add Text

from PIL import Image, ImageDraw, ImageFont
with Image.open("./res/LenaRGB.bmp") as img:    draw = ImageDraw.Draw(img)    # The font must be available on the system; Chinese fonts may not be accessible    font = ImageFont.truetype(r'C:\Windows\Fonts\arial.ttf', 36)    draw.text((50, 50), "Hello Pillow", fill="white", font=font)    img.show()

Image Stitching

from PIL import Image
with (Image.open('./res/LenaRGB.bmp') as img1, Image.open('./res/LenaRGB.bmp') as img2):    i1 = img1.resize((128, 128))    i2 = img2.resize((128, 128))    img = Image.new('RGB', (256, 128))  # horizontal stitching    img.paste(i1, (0, 0))    img.paste(i2, (i1.size[0], 0))    img.show()

Split RGB Channels

from PIL import Image
with Image.open("./res/LenaRGB.bmp") as img:    r, g, b = img.split()    img_filter = Image.new('RGB', (img.size[0] * 3, img.size[1]))    img_filter.paste(r)    img_filter.paste(g, (img.size[0], 0))    img_filter.paste(b, (img.size[0] * 2, 0))    img_filter.show()

Slightly Enhance Details

from PIL import Image, ImageFilter
with Image.open("./res/LenaRGB.bmp") as img:    img_detail = img.filter(ImageFilter.DETAIL)    img_detail.show()

Overall Blur

from PIL import Image, ImageFilter
with Image.open("./res/LenaRGB.bmp") as img:    img_blur = img.filter(ImageFilter.BLUR)    img_blur.show()

Local Gaussian Blur

from PIL import Image, ImageFilter
with Image.open("./res/LenaRGB.bmp") as img:    part = img.crop((200, 200, 400, 400))    part = part.filter(ImageFilter.GaussianBlur(12))    img.paste(part, (200, 200))    img.show()

Emboss Effect

from PIL import Image, ImageFilter
with Image.open("./res/LenaRGB.bmp") as img:    img_emboss = img.filter(ImageFilter.EMBOSS)    img_emboss.show()

Leave a Comment