Transformations

Image Processing in Python

Rebeca Gonzalez

Data Engineer

Why transform images?

  • Preparing images for classification Machine Learning models
  • Optimization and compression of images
  • Save images with same proportion

A cat picture being processed, downscaled

Image Processing in Python

Rotating

2 dogs playing, walking

Photo of 2 dogs playing, walking rotated 90 degrees clockwise

Image Processing in Python

Rotating

2 dogs playing, walking

2 dogs playing, walking rotated 90 degreed anticlockwise

Image Processing in Python

Rotating clockwise

from skimage.transform import rotate

# Rotate the image 90 degrees clockwise image_rotated = rotate(image, -90)
show_image(image, 'Original') show_image(image_rotated, 'Rotated 90 degrees clockwise')

Original photo of dogs next to rotated one

Image Processing in Python

Rotating anticlockwise

from skimage.transform import rotate

# Rotate an image 90 degrees anticlockwise image_rotated = rotate(image, 90)
show_image(image, 'Original') show_image(image_rotated, 'Rotated 90 degrees anticlockwise')

Original photo of dogs next to 90 degrees rotated one

Image Processing in Python

Rescaling

Original image of 2 dogs with original size of 3000 pixels next to downscaled one with 800 pixels

Image Processing in Python

Rescaling

Downgrading
from skimage.transform import rescale

# Rescale the image to be 4 times smaller image_rescaled = rescale(image, 1/4, anti_aliasing=True, multichannel=True)
show_image(image, 'Original image') show_image(image_rescaled, 'Rescaled image')
Image Processing in Python

Rescaling

Original image of 2 dogs with original size of 3000 pixels next to downscaled one with 800 pixels

Image Processing in Python

Aliasing in digital images

Letter A image without aliasing next an image of letter A with aliasing

Image Processing in Python

Aliasing in digital images

Rescaled image of 2 dogs playing with aliasing next to a rescaled one without aliasing

Image Processing in Python

Resizing

Original image with 3000 pixels, next to a resized one with 800 pixels

Image Processing in Python

Resizing

from skimage.transform import resize

# Height and width to resize height = 400 width = 500
# Resize image image_resized = resize(image, (height, width), anti_aliasing=True)
# Show the original and resulting images show_image(image, 'Original image') show_image(image_resized, 'Resized image')
Image Processing in Python

Resizing

Original image of 2 dogs with original size of 3000 pixels next to resized one, not looking proportional

Image Processing in Python

Resizing proportionally

from skimage.transform import resize

# Set proportional height so its 4 times its size height = image.shape[0] / 4 width = image.shape[1] / 4
# Resize image image_resized = resize(image, (height, width), anti_aliasing=True)
show_image(image_resized, 'Resized image')
Image Processing in Python

Resizing proportionally

Original image of 2 dogs with original size of 3000 pixels next to a resized one, this time proportionally with 800 pixels

Image Processing in Python

Let's practice!

Image Processing in Python

Preparing Video For Download...